Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does one get length of array of array?

Status
Not open for further replies.

bitplayer

Programmer
Feb 22, 2006
10
US
I am reading a series of lines from a file, each of which has a set of strings separated by commas, and storing each of those strings into arrays. So I have an array of arrays. The first string on line 1 would be $WL[1][1], the second string on line 1 would be $WL[1][2]. The first string of line 2 would be $WL[2][1]. Etc.

$teamnum = 0;
while (<>) { # read in output from CnvBRtxt2wiki.pl
push(@WL, [split(/,/,$_)]); # very cool way to create
$teamnum++; # array of arrays
}
$numteams = @WL; # this works, gives me number of arrays in @WL
print "Number of lines = $numteams\n";
print "Number of teams = $teamnum\n";
for ($team=0; $team<$numteams; $team++) {
$lenoflist = $WL[$team]; # this does not work
# this is really frustrating part. HOW DO I GET LENGTH
# of array within array???
print("lenoflist = $lenoflist\n");
for ($i=0; $i<$lenoflist; $i++) {
print("\[$WL[$team][$i]\]");
}
print "\n";
}
 
Code:
my @array = (
    [ "a", "b", "c" ],
    [ "d", "e", "f" ],
    [ "g", "h", "i", "j", "k", "l" ],
);

my $lines = scalar(@array);
print "Number of lines: $lines\n";

for (my $ln = 0; $ln < $lines; $ln++) {
    my $line = $array[$ln];

    # count the letters
    my $letters = scalar(@{$line});
    print "Line $ln; # of letters: $letters\n";

    for (my $i = 0; $i < $letters; $i++) {
        print "Line $ln letter $i: $array[$ln][$i]\n";
    }
}

Using an array in void context (no quotes) returns the number of elements in the array. scalar(@array) does too, and I personally prefer that syntax cuz it's easy to see what's going on with it.

Your array is actually an array containing references to arrays. So, when you check the length of the embedded arrays, you have to dereference them (@{$line} in my example).

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Kirsle - Do you have any thoughts on the differences pro/con between using scalar(@array) versus $#array ??

I tend to stay with $#array mostly out of familiarity.
 
scalar() just puts the array into scalar context, which would be about the same as having some @array out in the open without quotes around it (returning the number of elements)... $#array works too though. I don't know if there are any speed differences in them (if there is its probably something in the realm of nanoseconds). So I guess it's just about preference which one you use.

I just like scalar(@array) over $#array because 1) there's less line noise, and 2) for me it's easier to know what it's doing just reading it. It's like these two lines:

Code:
die "you didn't give me what I wanted!" unless defined $critical;

defined $critical or die "you didn't give me what I wanted!";

They both do the same thing, but the first form is easier to read (I think there's a better example of this in perlstyle...

Just because you CAN do something a particular way doesn't mean that you SHOULD do it that way. Perl is designed to give you several ways to do anything, so consider picking the most readable one. For instance

open(FOO,$foo) || die "Can't open $foo: $!";

is better than

die "Can't open $foo: $!" unless open(FOO,$foo);

because the second way hides the main point of the statement in a modifier. On the other hand

print "Starting analysis\n" if $verbose;

is better than

$verbose && print "Starting analysis\n";

because the main point isn't whether the user typed -v or not.

So it just boils down to "style". :p

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top