Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Arrays and Hashes processing :pls help

Reply
Thread Tools

Arrays and Hashes processing :pls help

 
 
Jags Rao
Guest
Posts: n/a
 
      02-12-2009


hi friends

i have a long array with structure like this

[["377", "838"],
["377", "990"],
["377", "991"],
["377", "992"],
["378", "840"],
["378", "841"],
["378", "842"],
["378", "843"],
["378", "844"]]

how can i convert it in one line to

[["377", "838 990 991 992"],
["378", "840 841 842 843 844"]]

if thats diificult is the below array processable in one line

[{"prefix"=>"838", "scf_id"=>"377"},
{"prefix"=>"990", "scf_id"=>"377"},
{"prefix"=>"991", "scf_id"=>"377"},
{"prefix"=>"992", "scf_id"=>"377"},
{"prefix"=>"840", "scf_id"=>"378"},
{"prefix"=>"841", "scf_id"=>"378"},
{"prefix"=>"842", "scf_id"=>"378"},
{"prefix"=>"843", "scf_id"=>"378"},
{"prefix"=>"844", "scf_id"=>"378"}]

in this form

[{"prefix"=>"838 990 991 992", "scf_id"=>"377"},
{"prefix"=>"840 841 842 843 844", "scf_id"=>"378"}]

pls help
Jags
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Matt Williams
Guest
Posts: n/a
 
      02-12-2009
On Fri, 2009-02-13 at 01:58 +0900, Jags Rao wrote:
>
> hi friends
>
> i have a long array with structure like this
>
> [["377", "838"],
> ["377", "990"],
> ["377", "991"],
> ["377", "992"],
> ["378", "840"],
> ["378", "841"],
> ["378", "842"],
> ["378", "843"],
> ["378", "844"]]
>
> how can i convert it in one line to
>
> [["377", "838 990 991 992"],
> ["378", "840 841 842 843 844"]]




> if thats diificult is the below array processable in one line
>
> [{"prefix"=>"838", "scf_id"=>"377"},
> {"prefix"=>"990", "scf_id"=>"377"},
> {"prefix"=>"991", "scf_id"=>"377"},
> {"prefix"=>"992", "scf_id"=>"377"},
> {"prefix"=>"840", "scf_id"=>"378"},
> {"prefix"=>"841", "scf_id"=>"378"},
> {"prefix"=>"842", "scf_id"=>"378"},
> {"prefix"=>"843", "scf_id"=>"378"},
> {"prefix"=>"844", "scf_id"=>"378"}]
>
> in this form
>
> [{"prefix"=>"838 990 991 992", "scf_id"=>"377"},
> {"prefix"=>"840 841 842 843 844", "scf_id"=>"378"}]
>



I don't understand the need for one line, but.... Also, why an array of
hashes? why not a hash?

if your array of hashes is referenced by h, then:
out = h.inject({}) {|s,v| x=s[v["scf_id"]] || ""; s[v["scf_id"]]="#{x}
#{v["prefix"]}".trim;s}

that (above) gives you a hash with distinct values of scf_id referencing
prefixes. That said, you can do the following if you *must* have an
array:
out = (h.inject({}) {|s,v| x=s[v["scf_id"]] || ""; s[v["scf_id"]]="#{x}
#{v["prefix"]}".trim;s}).inject([]){|s,v|
s<<{"scf_id"=>v[0],"prefix"=>v[1]};s}

It's ugly. Not maintainable.

Why the need for one line?


> pls help
> Jags



 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      02-12-2009
On 12.02.2009 17:58, Jags Rao wrote:

> i have a long array with structure like this
>
> [["377", "838"],
> ["377", "990"],
> ["377", "991"],
> ["377", "992"],
> ["378", "840"],
> ["378", "841"],
> ["378", "842"],
> ["378", "843"],
> ["378", "844"]]
>
> how can i convert it in one line to


Why in a single line?

Cheers

robert
 
Reply With Quote
 
w_a_x_man@yahoo.com
Guest
Posts: n/a
 
      02-12-2009
On Feb 12, 10:58*am, Jags Rao <aquaj...@yahoo.com> wrote:
> hi friends
>
> i have a long array with structure like this
>
> [["377", "838"],
> *["377", "990"],
> *["377", "991"],
> *["377", "992"],
> *["378", "840"],
> *["378", "841"],
> *["378", "842"],
> *["378", "843"],
> *["378", "844"]]
>
> how can i convert it in one line to
>
> [["377", "838 990 991 992"],
> *["378", "840 841 842 843 844"]]
>
> if thats diificult is the below array processable in one line
>
> [{"prefix"=>"838", "scf_id"=>"377"},
> *{"prefix"=>"990", "scf_id"=>"377"},
> *{"prefix"=>"991", "scf_id"=>"377"},
> *{"prefix"=>"992", "scf_id"=>"377"},
> *{"prefix"=>"840", "scf_id"=>"378"},
> *{"prefix"=>"841", "scf_id"=>"378"},
> *{"prefix"=>"842", "scf_id"=>"378"},
> *{"prefix"=>"843", "scf_id"=>"378"},
> *{"prefix"=>"844", "scf_id"=>"378"}]
>
> in this form
>
> [{"prefix"=>"838 990 991 992", "scf_id"=>"377"},
> *{"prefix"=>"840 841 842 843 844", "scf_id"=>"378"}]
>
> pls help
> Jags
> --
> Posted viahttp://www.ruby-forum.com/.


a = [["377", "838"],
["377", "990"],
["377", "991"],
["377", "992"],
["378", "840"],
["378", "841"],
["378", "842"],
["378", "843"],
["378", "844"]]
h=Hash.new{[]};a.each{|x,y|h[x]+=[y]};h.map{|x,y|[x,y.join(" ")]}
==>[["377", "838 990 991 992"], ["378", "840 841 842 843 844"]]
 
Reply With Quote
 
Jags Rao
Guest
Posts: n/a
 
      02-13-2009
hi guys
thank u so much for the help.
no issues with a multi line solution also

-Jags
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Christopher Dicely
Guest
Posts: n/a
 
      02-13-2009
On 2/12/09, Jags Rao <> wrote:
>
>
> hi friends
>
> i have a long array with structure like this
>
> [["377", "838"],
> ["377", "990"],
> ["377", "991"],
> ["377", "992"],
> ["378", "840"],
> ["378", "841"],
> ["378", "842"],
> ["378", "843"],
> ["378", "844"]]
>
> how can i convert it in one line to
>
> [["377", "838 990 991 992"],
> ["378", "840 841 842 843 844"]]


original_array = [["377", "838"],
["377", "990"],
["377", "991"],
["377", "992"],
["378", "840"],
["378", "841"],
["378", "842"],
["378", "843"],
["378", "844"]]

new_array = original_array.group_by {|x| x[0]}.map {|k,v| [k, v.join(" ")]}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
"Pseudo-hashes are deprecated" error and accessing a hash of hashes ernestm@mindspring.com Perl Misc 3 01-31-2006 04:40 AM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57