# Arrays and Hashes processing :pls help

**URL:** <https://www.ruby-forum.com/t/arrays-and-hashes-processing-pls-help/160184>\
**Category:** Ruby\
**Created:** [February 12, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/arrays-and-hashes-processing-pls-help/160184 "2009-02-12T18:00:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [February 12, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/arrays-and-hashes-processing-pls-help/160184/1 "2009-02-12T18:00:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [February 12, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/arrays-and-hashes-processing-pls-help/160184/2 "2009-02-12T18:24:59Z")

</div>

On Fri, 2009-02-13 at 01:58 +0900, Jags R. 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?

---

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [February 12, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/arrays-and-hashes-processing-pls-help/160184/3 "2009-02-12T18:32:10Z")

</div>

On 12.02.2009 17:58, Jags R. wrote:

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

Why in a single line?

Cheers

robert

---

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [February 13, 2009, 6:07am UTC](https://www.ruby-forum.com/t/arrays-and-hashes-processing-pls-help/160184/4 "2009-02-13T06:07:49Z")

</div>

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

-Jags

---

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [February 13, 2009, 9:15am UTC](https://www.ruby-forum.com/t/arrays-and-hashes-processing-pls-help/160184/5 "2009-02-13T09:15:36Z")

</div>

On 2/12/09, Jags R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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("  
")]}

---

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [February 12, 2009, 7:25pm UTC](https://www.ruby-forum.com/t/arrays-and-hashes-processing-pls-help/160184/6 "2009-02-12T19:25:46Z")

</div>

On Feb 12, 10:58 am, Jags R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”]]
