Simple perl array question

Simple perl array question

am 20.04.2008 21:05:42 von dfairman16

Well simple if you are not learning Perl. You guessed it, I am a
newbie.

My question is if I have an array like this, actually it is my whole
program.

my @testarray=( [5, [1,3,18,21]], [16, [1,2,3]], [21, [1]]);
print #@$testarray[0][1];
print #@$testarray[1][1];
print #@$testarray[2][1];

What I had expected and what I want is to print out 4, 3, and 1 but it
prints out 1 and 1 and I don't know why. The 4,3, and 1 are the
lengths of the second array in the array of arrays.


If someone could put me right I can move on (to the next problem and
understanding your code).
Thank you
David


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Simple perl array question

am 21.04.2008 00:06:45 von Richard Lee

dfairman16@hotmail.com wrote:
> Well simple if you are not learning Perl. You guessed it, I am a
> newbie.
>
> My question is if I have an array like this, actually it is my whole
> program.
>
> my @testarray=( [5, [1,3,18,21]], [16, [1,2,3]], [21, [1]]);
> print #@$testarray[0][1];
> print #@$testarray[1][1];
> print #@$testarray[2][1];
>
> What I had expected and what I want is to print out 4, 3, and 1 but it
> prints out 1 and 1 and I don't know why. The 4,3, and 1 are the
> lengths of the second array in the array of arrays.
>
>
> If someone could put me right I can move on (to the next problem and
> understanding your code).
> Thank you
> David
>
>
>
I am not sure if this is what you want, but try this,

#!/usr/bin/perl -w

use strict;
use Data::Dumper;

my @testarray=( [5, [1,3,18,21]], [16, [1,2,3]], [21, [1]]);

print Dumper(@testarray);

print "$testarray[0][0]\n"; #first number which is 5
print "$testarray[0][1][0]\n";#first number which is 1
print "@{$testarray[0][1]}\n";#prints entire first VAR, which is 5 , 1,
3 , 18 , 21
[root@server tmp]# ./!$
.././././././././././././././././././somebody.pl
$VAR1 = [
5,
[
1,
3,
18,
21
]
];
$VAR2 = [
16,
[
1,
2,
3
]
];
$VAR3 = [
21,
[
1
]
];
5
1
1 3 18 21

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Simple perl array question

am 21.04.2008 00:20:56 von krahnj

dfairman16@hotmail.com wrote:
> Well simple if you are not learning Perl. You guessed it, I am a
> newbie.
>
> My question is if I have an array like this, actually it is my whole
> program.
>
> my @testarray=( [5, [1,3,18,21]], [16, [1,2,3]], [21, [1]]);
> print #@$testarray[0][1];
> print #@$testarray[1][1];
> print #@$testarray[2][1];
>
> What I had expected and what I want is to print out 4, 3, and 1 but it
> prints out 1 and 1 and I don't know why.

$ perl -MO=Deparse -le'
my @testarray = (
[ 5, [ 1, 3, 18, 21 ] ],
[ 16, [ 1, 2, 3 ] ],
[ 21, [ 1 ] ],
);
print #@$testarray[ 0 ][ 1 ];
print #@$testarray[ 1 ][ 1 ];
print #@$testarray[ 2 ][ 1 ];
'
BEGIN { $/ = "\n"; $\ = "\n"; }
my(@testarray) = ([5, [1, 3, 18, 21]], [16, [1, 2, 3]], [21, [1]]);
print print(print($_));
-e syntax OK


The # character signifies the beginning of a comment so everything after
it is ignored and thus you get "print print(print($_));" where "1" is
what is returned from print()


> The 4,3, and 1 are the
> lengths of the second array in the array of arrays.

What you look like you where trying to accomplish is:

print $#{ $testarray[ 0 ][ 1 ] };
print $#{ $testarray[ 1 ][ 1 ] };
print $#{ $testarray[ 2 ][ 1 ] };


But that doesn't print the lengths of the arrays, for that you need:

print scalar @{ $testarray[ 0 ][ 1 ] };
print scalar @{ $testarray[ 1 ][ 1 ] };
print scalar @{ $testarray[ 2 ][ 1 ] };


> If someone could put me right I can move on (to the next problem and
> understanding your code).

perldoc perldata
perldoc perldsc
perldoc perllol



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/