Transforming

Hi,

In my program there is a hash of this form

     a={ "<string_a>" => [<val1>,<val2>,<val3>],
       "<string_b>" => [<val4>,<val5>,<val6>],
       "<string_c>" => [<val7>,<val8>,<val9>]
       }

  I am looking for a short and handy way to produce an array out of
  this, which looks like:

   b=[[<val1>,<val2>,<val3>],[<val4>,<val5>,<val6>],[<val7>,<val8>,<val9>]]

without iterating over the hash.

Currently I have no idea how to do this...

Thank you very much for any ruby in advance ! ;O)

Keep hacking!
Meino

Meino wrote:

In my program there is a hash of this form

     a={ "<string_a>" => [<val1>,<val2>,<val3>],
       "<string_b>" => [<val4>,<val5>,<val6>],
       "<string_c>" => [<val7>,<val8>,<val9>]
       }

  I am looking for a short and handy way to produce an array out of
this, which looks like:

   b=[[<val1>,<val2>,<val3>],[<val4>,<val5>,<val6>],[<val7>,<val8>,<val9>]]

without iterating over the hash.

I presume you mean you just want an array of the *values* in the hash?

  b = a.values

If you want the values sorted according to the order of the keys:

  b = a.keys.sort.map { |k| a[k] }

Cheers,
Gavin

Meino Christian Cramer <Meino.Cramer@gmx.de> wrote in message news:<20040705.090101.07634377.Meino.Cramer@gmx.de>...

Hi,

In my program there is a hash of this form

     a={ "<string_a>" => [<val1>,<val2>,<val3>],
       "<string_b>" => [<val4>,<val5>,<val6>],
       "<string_c>" => [<val7>,<val8>,<val9>]
       }

  I am looking for a short and handy way to produce an array out of
  this, which looks like:

   b=[[<val1>,<val2>,<val3>],[<val4>,<val5>,<val6>],[<val7>,<val8>,<val9>]]

without iterating over the hash.

b = a.values

Ciao

Denis

Meino Christian Cramer <Meino.Cramer@gmx.de> wrote in message news:<20040705.090101.07634377.Meino.Cramer@gmx.de>...

Hi,

In my program there is a hash of this form

     a={ "<string_a>" => [<val1>,<val2>,<val3>],
       "<string_b>" => [<val4>,<val5>,<val6>],
       "<string_c>" => [<val7>,<val8>,<val9>]
       }

  I am looking for a short and handy way to produce an array out of
  this, which looks like:

   b=[[<val1>,<val2>,<val3>],[<val4>,<val5>,<val6>],[<val7>,<val8>,<val9>]]

without iterating over the hash.

Currently I have no idea how to do this...

Thank you very much for any ruby in advance ! ;O)

Keep hacking!
Meino

Hi,

b = a.values

Regards,
Vlad

stmt = '{"a"=>%w(a1 a2 a3), "b"=>%w(b1 b2 b3)}'
puts "h = " + stmt
h = eval stmt
v = h.values

print "h.values => [ "
v.each { |x| print "[" + x.join(", ") + "] " }; puts "]"

puts "h.values => "
n=1
v.each do |x|
    puts "\tValue #{n}: [ #{x.join(", ")} ] (Class=#{x.class})"; n += 1
end

HTH,
Richard

···

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.713 / Virus Database: 469 - Release Date: 7/3/2004

Hi Gavin,

whow! what a fast reply ! Thank you very much ! :slight_smile:

I want an algorithm, which when applied like this

   b=<algo>

it does the same as I would do this by hand:

   b=[[<val1>,<val2>,<val3>],[<val4>,<val5>,<val6>],[<val7>,<val8>,<val9>]]

···

From: "Gavin Sinclair" <gsinclair@soyabean.com.au>
Subject: Re: Transforming...
Date: Mon, 5 Jul 2004 16:07:19 +0900

Meino wrote:

> In my program there is a hash of this form
>
> a={ "<string_a>" => [<val1>,<val2>,<val3>],
> "<string_b>" => [<val4>,<val5>,<val6>],
> "<string_c>" => [<val7>,<val8>,<val9>]
> }
>
> I am looking for a short and handy way to produce an array out of
> this, which looks like:
>
> b=[[<val1>,<val2>,<val3>],[<val4>,<val5>,<val6>],[<val7>,<val8>,<val9>]]
>
> without iterating over the hash.

I presume you mean you just want an array of the *values* in the hash?

  b = a.values

If you want the values sorted according to the order of the keys:

  b = a.keys.sort.map { |k| a[k] }

Cheers,
Gavin

Meino wrote:

whow! what a fast reply ! Thank you very much ! :slight_smile:

I want an algorithm, which when applied like this

   b=<algo>

it does the same as I would do this by hand:

   b=[[<val1>,<val2>,<val3>],[<val4>,<val5>,<val6>],[<val7>,<val8>,<val9>]]

Sorry, Meino, I don't understand what you want. You want to transform 'a'
into 'b' (in your original posting), right? Maybe a more specific example
would help.

Gavin

How about ...

  a = { "x" => [1, 2, 3], "y" => [4, 5, 6], "z" => [7, 8, 9] }

  b = a.keys.map { |k| a[k] }

The only problem is that, since "a" is a hash, the order of "a.keys" not necessarily the order you want. If you want the values in key order, you could do

  b = a.keys.sort.map { |k| a[k] }

Does that give what you're after?

"Meino Christian Cramer" <Meino.Cramer@gmx.de> schrieb im Newsbeitrag
news:20040705.091922.03982506.Meino.Cramer@gmx.de...

From: "Gavin Sinclair" <gsinclair@soyabean.com.au>
Subject: Re: Transforming...
Date: Mon, 5 Jul 2004 16:07:19 +0900

Hi Gavin,

whow! what a fast reply ! Thank you very much ! :slight_smile:

I want an algorithm, which when applied like this

   b=<algo>

it does the same as I would do this by hand:

b=[[<val1>,<val2>,<val3>],[<val4>,<val5>,<val6>],[<val7>,<val8>,<val9>]]

That's exactly what Gavin gave you. What are you after?

    robert

> Meino wrote:
>
> > In my program there is a hash of this form
> >
> > a={ "<string_a>" => [<val1>,<val2>,<val3>],
> > "<string_b>" => [<val4>,<val5>,<val6>],
> > "<string_c>" => [<val7>,<val8>,<val9>]
> > }
> >
> > I am looking for a short and handy way to produce an array out of
> > this, which looks like:
> >
> >

b=[[<val1>,<val2>,<val3>],[<val4>,<val5>,<val6>],[<val7>,<val8>,<val9>]]

···

> >
> > without iterating over the hash.
>
> I presume you mean you just want an array of the *values* in the hash?
>
> b = a.values
>
> If you want the values sorted according to the order of the keys:
>
> b = a.keys.sort.map { |k| a[k] }
>
> Cheers,
> Gavin
>
>
>
>
>

Robert Klemme wrote:

That's exactly what Gavin gave you. What are you after?

Yes, and if I'd read Gavin's whole message, I'd have realised I was just repeating what he said, too :slight_smile: ... except that I didn't realise there was a #values method!

Hi Gavin, Hi Bob, Hi Harry !

...oh,oh,oh...I think I have started a great Ruby-BigBang...
Due to my limited English I have spend much confusion here...I
think...sorry for that...

In the meanwhile I have irbed the algo, which you sent me, Gavin, and
it does exactly what I want. THANK YOU :slight_smile:

So, before I spend a lot more confusion: A good algo show more than
a thousand words (of limited English ;)...if you want to know, what my
confusion postings meant before, better take a look at Gavin's algo
than at my postings.... :wink:

Again, thank you very much for all your effort !

Meino

···

From: "Gavin Sinclair" <gsinclair@soyabean.com.au>
Subject: Re: Transforming...
Date: Mon, 5 Jul 2004 16:31:58 +0900

Meino wrote:

> whow! what a fast reply ! Thank you very much ! :slight_smile:
>
> I want an algorithm, which when applied like this
>
> b=<algo>
>
> it does the same as I would do this by hand:
>
> b=[[<val1>,<val2>,<val3>],[<val4>,<val5>,<val6>],[<val7>,<val8>,<val9>]]
>

Sorry, Meino, I don't understand what you want. You want to transform 'a'
into 'b' (in your original posting), right? Maybe a more specific example
would help.

Gavin