Accessing array elements of record

A

Ami

Hi All,
I am quite new to perl and came accross one simple problem, which I
am not able to overcome. I have a record which consists of array. I
want to access array elements one by one but can't do it.
Code snippets is as follows:

$record = {
NAME => "Jason",
EMPNO => 132,
TITLE => "deputy peon",
AGE => 23,
SALARY => 37_000,
PALS => [ "Norbert", "Rhys", "Phineas"],
};

$byname{ $record->{NAME} } = $record;

push @{$byname{"Jason"}->{PALS}}, "Theodore";

@tt = ($byname{"Jason"}->{PALS});

for ($i=0;$i<1;$i++)
{
printf("PALS %s",$tt[$i]);
}

When I run this script, I always get the array reference e.g.
"ARRAY(0x24d5718)" . I want to print array list as
Norbert
Rhys
etc

Any help will be highly appreciated.
Thanks,
 
M

Mirco Wahab

Ami wrote:

my $record = {
NAME => "Jason",
EMPNO => 132,
TITLE => "deputy peon",
AGE => 23,
SALARY => 37_000,
PALS => [ "Norbert", "Rhys", "Phineas"],
};
1: $byname{ $record->{NAME} } = $record;
2: push @{$byname{"Jason"}->{PALS}}, "Theodore";
3: @tt = ($byname{"Jason"}->{PALS});
4: for ($i=0;$i<1;$i++)
5: {
6: printf("PALS %s",$tt[$i]);
7: }

There are some problems here,
1 & 2 are o.k.:

my %byname;
$byname{ $record->{NAME} } = $record;
push @{$byname{"Jason"}{PALS}}, "Theodore";

The next line is wrong, the "array" in the record
is an "array reference":

my $tt = $byname{"Jason"}{PALS};

You can drop the arrows between braces.
By string interpolation of the array
reference, you got your pals:

printf "PALS: @$tt \n";


In a loop, one would write:

for my $t (@$tt) {
printf "PAL: %s, ", $t # or printf "PAL: $t, "
}
Regards

M.
 
P

Paul Lalli

$record = {
NAME => "Jason",
EMPNO => 132,
TITLE => "deputy peon",
AGE => 23,
SALARY => 37_000,
PALS => [ "Norbert", "Rhys", "Phineas"],

};

$byname{ $record->{NAME} } = $record;

push @{$byname{"Jason"}->{PALS}}, "Theodore";

You have the correct syntax here. . .
@tt = ($byname{"Jason"}->{PALS});

But the incorrect syntax here. In both lines, you're trying to access
the array that $byname{"Jason"}->{PALS} references. The syntax is the
same for both.

@tt = @{$byname{"Jason"}->{PALS}};

Paul Lalli
 
A

Ami

Ami wrote:

my $record = {
NAME => "Jason",
EMPNO => 132,
TITLE => "deputy peon",
AGE => 23,
SALARY => 37_000,
PALS => [ "Norbert", "Rhys", "Phineas"],

};
1: $byname{ $record->{NAME} } = $record;
2: push @{$byname{"Jason"}->{PALS}}, "Theodore";
3: @tt = ($byname{"Jason"}->{PALS});
4: for ($i=0;$i<1;$i++)
5: {
6: printf("PALS %s",$tt[$i]);
7: }

There are some problems here,
1 & 2 are o.k.:

my %byname;
$byname{ $record->{NAME} } = $record;
push @{$byname{"Jason"}{PALS}}, "Theodore";

The next line is wrong, the "array" in the record
is an "array reference":

my $tt = $byname{"Jason"}{PALS};

You can drop the arrows between braces.
By string interpolation of the array
reference, you got your pals:

printf "PALS: @$tt \n";

In a loop, one would write:

for my $t (@$tt) {
printf "PAL: %s, ", $t # or printf "PAL: $t, "
}
Regards

M.

Hi Micro and Paul,
Many thanks for your answers. It works great now with your help.
Thanks again,
Regards
 
D

Dave Weaver

@tt = ($byname{"Jason"}->{PALS});

for ($i=0;$i<1;$i++)
{
printf("PALS %s",$tt[$i]);
}

In addition to the other answers here, you could just
leave out your intermediate @tt array, and access the
array directly:

printf("PALS %s", $byname{"Jason"}->{PALS}->[$i]);

Note that the "->" between backets is optional, as are
(generally speaking) the quotes around the hash key,
so it could be written as:

printf("PALS %s", $byname{Jason}{PALS}[$i]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top