Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Array read to seperate items
Stuart Clarke
3/31/2009 3:28:00 PM
Hi all,
This might be a stupid question.
I have an array which gets data added to in a loop like so:
a b c d e f
a b c d e f
I am aware calling join("\n")on this array will separate each line of
the array with a new line, but I want to separate the individual items
and load them into there own arrays for example
array a = a, a
array b = b, b
etc etc.
In summary I want to output my data exactly how it is, but because I
want to load into structured columns on my GUI I want the data broken
down so it can be loaded to the relevant columns.
Thanks in advance
--
Posted via
http://www.ruby-...
.
3 Answers
Rob Biedenharn
3/31/2009 3:56:00 PM
0
On Mar 31, 2009, at 11:27 AM, Stuart Clarke wrote:
> Hi all,
>
> This might be a stupid question.
>
> I have an array which gets data added to in a loop like so:
>
> a b c d e f
> a b c d e f
>
> I am aware calling join("\n")on this array will separate each line of
> the array with a new line, but I want to separate the individual items
> and load them into there own arrays for example
>
> array a = a, a
> array b = b, b
>
> etc etc.
>
> In summary I want to output my data exactly how it is, but because I
> want to load into structured columns on my GUI I want the data broken
> down so it can be loaded to the relevant columns.
>
> Thanks in advance
> --
> Posted via
http://www.ruby-...
.
Array#transpose
irb> [[1,2,3,4,5],%w[a b c d e]].transpose
=> [[1, "a"], [2, "b"], [3, "c"], [4, "d"], [5, "e"]]
-Rob
Rob Biedenharn
http://agileconsult...
Rob@AgileConsultingLLC.com
matt
3/31/2009 4:07:00 PM
0
Stuart Clarke <stuart.clarke1986@gmail.com> wrote:
> I have an array which gets data added to in a loop like so:
>
> a b c d e f
> a b c d e f
>
> I am aware calling join("\n")on this array will separate each line of
> the array with a new line, but I want to separate the individual items
> and load them into there own arrays for example
>
> array a = a, a
> array b = b, b
Are you talking about something like this?
arr = ["hey ho hey nonnie no", "bee bop a ding dong", "a b c d e"]
arr = arr.map {|s| s.split(" ")}
arr = arr.shift.zip(*arr)
p arr
#=> [["hey", "bee", "a"], ["ho", "bop", "b"], ["hey", "a", "c"],
["nonnie", "ding", "d"], ["no", "dong", "e"]]
m.
--
matt neuburg, phd = matt@tidbits.com,
http://www.tidbits...
Leopard -
http://www.takecontrolbooks.com/leopard-custom...
AppleScript -
http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart.
http://www.t...
matt
3/31/2009 4:10:00 PM
0
matt neuburg <matt@tidbits.com> wrote:
> Stuart Clarke <stuart.clarke1986@gmail.com> wrote:
>
> > I have an array which gets data added to in a loop like so:
> >
> > a b c d e f
> > a b c d e f
> >
> > I am aware calling join("\n")on this array will separate each line of
> > the array with a new line, but I want to separate the individual items
> > and load them into there own arrays for example
> >
> > array a = a, a
> > array b = b, b
>
> Are you talking about something like this?
>
> arr = ["hey ho hey nonnie no", "bee bop a ding dong", "a b c d e"]
> arr = arr.map {|s| s.split(" ")}
> arr = arr.shift.zip(*arr)
Last line cute but unnecessary, forgot about "transpose" (see msg from
Rob B.):
arr = ["hey ho hey nonnie no", "bee bop a ding dong", "a b c d e"]
arr = arr.map {|s| s.split(" ")}.transpose
p arr
#=> [["hey", "bee", "a"], ["ho", "bop", "b"], ["hey", "a", "c"],
["nonnie", "ding", "d"], ["no", "dong", "e"]]
m.
--
matt neuburg, phd = matt@tidbits.com,
http://www.tidbits...
Leopard -
http://www.takecontrolbooks.com/leopard-custom...
AppleScript -
http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart.
http://www.t...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Array read to seperate items
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password