|
|
Up |
|
|
  |
Author: YitzleYitzle 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 |
|
  |
Author: Ken FoskeyKen 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 |
|
  |
Author: Mr. Shawn H. CoreyMr. 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 |
|
  |
Author: Paul LalliPaul Lalli Date: Aug 13, 2007 07:47
> 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 |
|
  |
|
|
  |
Author: Ken FoskeyKen Foskey Date: Aug 13, 2007 08:28
On Mon, 2007-08-13 at 07:47 -0700, Paul Lalli 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 |
  |
|
|
|