For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2007 > Adding numbers of vector, in hash table









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Adding numbers of vector, in hash table
Rodrigo Tavares

2007-10-22, 7:59 am

Hello,

I would like, put element's array in hash table.
How I can do it ?


my %numbers = (
zero => '0',
one => '1',
two => '2',
three => '3',
four => '4',
five => '5',
six => '6',
seven => '7',
eigth => '8',
nine => '9',
);

my @array = qw(11 12 13 14 15);

Best regards,

Faria


Abra sua conta no Yahoo! Mail, o único sem limite de espaço para armazenamento!
http://br.mail.yahoo.com/
Jeff Pang

2007-10-22, 7:59 am

On 10/22/07, Rodrigo Tavares <digolinopage@yahoo.com.br> wrote:
> Hello,
>
> I would like, put element's array in hash table.
> How I can do it ?
>


Hi,

use AoH,like,

my @array = (1,2,3,4);
my %hash = (key1 => \@array);

then you can access the array's first element by,

$hash{key1}->[0];

the second element by,

$hash{key1}->[1];

and so on.

see `perldoc perldata` for details.
Jenda Krynicky

2007-10-22, 7:01 pm

From: "Jeff Pang" <pangj@earthlink.net>
> On 10/22/07, Rodrigo Tavares <digolinopage@yahoo.com.br> wrote:
>
> Hi,
>
> use AoH,like,
>
> my @array = (1,2,3,4);
> my %hash = (key1 => \@array);
>
> then you can access the array's first element by,
>
> $hash{key1}->[0];
>
> the second element by,
>
> $hash{key1}->[1];
>
> and so on.


You can actually skip the ->.

$hash{key1}->[0] == $hash{key1}[0]


On the other hand if you have a reference to a hash in a variable you
can't skip the first -> :
$hashref->{key}->[0]
is equivalent to
$hashref->{key}[0]
but not to
$hashref{key}[0]
!

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com