Array to Hash
  Home FAQ Contact Sign in
perl.beginners only
 
Advanced search
POPULAR GROUPS

more...

perl.beginners Profile…
 Up
Array to Hash         


Author: Yitzle
Date: Aug 12, 2007 19:55

I got an array of values where the order is relevent, eg the ages of
Alice, Bob and Charles, and I want to make a hash out of it. I got
this code that does it:
my %%ages = (alice => $r[0], bob => $r[1], charles => $r[2]);
Is there a more elegent way to do it?
4 Comments
Re: Array to Hash         


Author: Ken Foskey
Date: Aug 13, 2007 06:40

On Sun, 2007-08-12 at 22:55 -0400, yitzle wrote:
> I got an array of values where the order is relevent, eg the ages of
> Alice, Bob and Charles, and I want to make a hash out of it. I got
> this code that does it:
> my %%ages = (alice => $r[0], bob => $r[1], charles => $r[2]);
> Is there a more elegent way to do it?

This is not elegant in this example but it possibly is the answer you
are looking for.

my %%ages{ 'alice', 'bob', 'charles'} = @r;

--
Ken Foskey
FOSS developer
no comments
Re: Array to Hash         


Author: Mr. Shawn H. Corey
Date: Aug 13, 2007 07:20

Ken Foskey wrote:
> On Sun, 2007-08-12 at 22:55 -0400, yitzle wrote:
>> I got an array of values where the order is relevent, eg the ages of
>> Alice, Bob and Charles, and I want to make a hash out of it. I got
>> this code that does it:
>> my %%ages = (alice => $r[0], bob => $r[1], charles => $r[2]);
>> Is there a more elegent way to do it?
>
> This is not elegant in this example but it possibly is the answer you
> are looking for.
>
> my %%ages{ 'alice', 'bob', 'charles'} = @r;
>

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my @people = ( 'alice', 'bob', 'charles' );
my @list = ( 1, 2, 3 );
Show full article (0.87Kb)
no comments
Re: Array to Hash         


Author: Paul Lalli
Date: Aug 13, 2007 07:47

On Aug 13, 9:40 am, fos...@optushome.com.au (Ken Foskey) wrote:
> On Sun, 2007-08-12 at 22:55 -0400, yitzle wrote:
>> I got an array of values where the order is relevent, eg the ages of
>> Alice, Bob and Charles, and I want to make a hash out of it. I got
>> this code that does it:
>> my %%ages = (alice => $r[0], bob => $r[1], charles => $r[2]);
>> Is there a more elegent way to do it?
>
> This is not elegant in this example but it possibly is the answer you
> are looking for.
>
> my %%ages{ 'alice', 'bob', 'charles'} = @r;

I rather doubt the OP is looking for an "answer" that doesn't even
compile.

I can only assume you meant:
my %%ages;
@ages{'alice', 'bob', 'charles'} = @r;

Paul Lalli
no comments
Re: Array to Hash         


Author: Ken Foskey
Date: Aug 13, 2007 08:28

On Mon, 2007-08-13 at 07:47 -0700, Paul Lalli wrote:
> On Aug 13, 9:40 am, fos...@optushome.com.au (Ken Foskey) wrote:
>> On Sun, 2007-08-12 at 22:55 -0400, yitzle wrote:
>>> I got an array of values where the order is relevent, eg the ages of
>>> Alice, Bob and Charles, and I want to make a hash out of it. I got
>>> this code that does it:
>>> my %%ages = (alice => $r[0], bob => $r[1], charles => $r[2]);
>>> Is there a more elegent way to do it?
>>
>> This is not elegant in this example but it possibly is the answer you
>> are looking for.
>>
>> my %%ages{ 'alice', 'bob', 'charles'} = @r;
>
> I rather doubt the OP is looking for an "answer" that doesn't even
> compile.
>
> I can only assume you meant:
> my %%ages;
> @ages{'alice', 'bob', 'charles'} = @r; ...
Show full article (0.94Kb)
no comments

RELATED THREADS
SubjectArticles qty Group
how to join array into arraycomp.lang.perl.misc ·
Re: Does passing an array make a copy of the array in memory?comp.lang.labview ·
Re: Traversing a hash with array refs as keys?comp.lang.perl.misc ·