Accessing array elements of record
Accessing array elements of record
am 25.09.2007 14:17:00 von 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,
Re: Accessing array elements of record
am 25.09.2007 14:36:52 von 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.
Re: Accessing array elements of record
am 25.09.2007 14:37:18 von Paul Lalli
On Sep 25, 8:17 am, Ami wrote:
> $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
Re: Accessing array elements of record
am 26.09.2007 09:14:19 von AMI
On Sep 25, 5:36 pm, Mirco Wahab wrote:
> 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
Re: Accessing array elements of record
am 26.09.2007 09:56:29 von Dave Weaver
On Tue, 25 Sep 2007 05:17:00 -0700, Ami wrote:
>
> @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]);