Arrays and Hashes processing :pls help

J

Jags Rao

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
 
M

Matt Williams

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?
 
R

Robert Klemme

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
 
W

w_a_x_man

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

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"]]
 
J

Jags Rao

hi guys
thank u so much for the help.
no issues with a multi line solution also

-Jags
 
C

Christopher Dicely

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(" ")]}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top