Forum: Ruby Arrays and Hashes processing :pls help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bdee3ba9491da0b6bc0f35dedb5b7a14?d=identicon&s=25 Jags Rao (jags)
on 2009-02-12 18:00

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
C3a0d479bd322818d766a7e869c6c5b2?d=identicon&s=25 Matt Williams (Guest)
on 2009-02-12 18:24
(Received via mailing list)
On Fri, 2009-02-13 at 01:58 +0900, Jags Rao wrote:
>  ["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"]]



>  {"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?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-12 18:32
(Received via mailing list)
On 12.02.2009 17:58, Jags Rao wrote:

>  ["378", "844"]]
>
> how can i convert it in one line to

Why in a single line?

Cheers

  robert
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 unknown (Guest)
on 2009-02-12 19:25
(Received via mailing list)
On Feb 12, 10:58 am, Jags Rao <aquaj...@yahoo.com> wrote:
>  ["378", "842"],
> [{"prefix"=>"838", "scf_id"=>"377"},
>
> [{"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"]]
Bdee3ba9491da0b6bc0f35dedb5b7a14?d=identicon&s=25 Jags Rao (jags)
on 2009-02-13 06:07
hi guys
thank u so much for the help.
no issues with a multi line solution also

-Jags
D7463bd611f227cfb2ef4da4a978a203?d=identicon&s=25 Christopher Dicely (Guest)
on 2009-02-13 09:15
(Received via mailing list)
On 2/12/09, Jags Rao <aquajags@yahoo.com> wrote:
>  ["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("
")]}
This topic is locked and can not be replied to.