Mailinglist Archive: opensuse (2532 mails)

< Previous Next >
Re: [opensuse] Quick perl question - why are @array[$num] and $array[$num] the same?
  • From: Sandy Drobic <suse-linux-e@xxxxxxxxxxxxxxxxxxxxxxx>
  • Date: Thu, 01 May 2008 09:46:26 +0200
  • Message-id: <48197552.7030806@xxxxxxxxxxxxxxxxxxxxxxx>
David C. Rankin wrote:
Listmates:

I don't know who I have to thank for this, but I think it was Anders who recommended that I look at perl after one of my last weird bash questions. I have a syntax question as I go though perl.

The situation. I read a file into an array and then go to reference each line.

my $NUM = 1;
open FILE, ".bashrc" or die $!;
my @array = <FILE>;

So far okay.

How is it legal that I can reference the array element either as @array[element] or $array[element]?:

How did you come to that conclusion?!?

while (@array[$NUM]) {
print "$NUM : @array[$NUM]\n";
$NUM++;
}

while ($array[$NUM]) {
print "$NUM : $array[$NUM]\n";
$NUM++;
}

No, the output is definitely not what you want. The answer comes if you follow best practice and insert the following lines at the top of your script:

#!/usr/bin/perl
use strict;
use warnings;

These three lines should be the very first thing you ever write in every perl script.

From what I've read, it seems like the list $array[element] would just return the number of characters in the list $array[element] not the text itself. How should I think about this?

./perltest.pl
Scalar value @array[$NUM] better written as $array[$NUM] at ./perltest.pl line
12.
Scalar value @array[$NUM] better written as $array[$NUM] at ./perltest.pl line
13.

The "use warnings;" gives you the message above. Perl is just lenient in this case, though the output is not what you would expect:


1 @: Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=3.000, Curwin=0

2 @: Def fieldscur=AEHIOQTWKNMbcdfgjplrsuvyzX

3 @: winflags=30009, sortindx=10, maxtasks=0

4 @: summclr=1, msgsclr=1, headclr=3, taskclr=1

5 @: Job fieldscur=ABcefgjlrstuvyzMKNHIWOPQDX

6 @: winflags=62777, sortindx=0, maxtasks=0

7 @: summclr=6, msgsclr=6, headclr=7, taskclr=6

8 @: Mem fieldscur=ANOPQRSTUVbcdefgjlmyzWHIKX

9 @: winflags=62777, sortindx=13, maxtasks=0

10 @: summclr=5, msgsclr=5, headclr=4, taskclr=5

11 @: Usr fieldscur=ABDECGfhijlopqrstuvyzMKNWX

12 @: winflags=62777, sortindx=4, maxtasks=0

13 @: summclr=3, msgsclr=3, headclr=2, taskclr=3


You see that the while loop is executed 13 times, and the first line is missing because the first array element is referenced as $array[0], not $array[1].

This is what you probably want:

foreach my $array_element ( @array) {
print $array_element;


RCfile for "top with windows" # shameless braggin'
Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=3.000, Curwin=0
Def fieldscur=AEHIOQTWKNMbcdfgjplrsuvyzX
winflags=30009, sortindx=10, maxtasks=0
summclr=1, msgsclr=1, headclr=3, taskclr=1
Job fieldscur=ABcefgjlrstuvyzMKNHIWOPQDX
winflags=62777, sortindx=0, maxtasks=0
summclr=6, msgsclr=6, headclr=7, taskclr=6
Mem fieldscur=ANOPQRSTUVbcdefgjlmyzWHIKX
winflags=62777, sortindx=13, maxtasks=0
summclr=5, msgsclr=5, headclr=4, taskclr=5
Usr fieldscur=ABDECGfhijlopqrstuvyzMKNWX
winflags=62777, sortindx=4, maxtasks=0
summclr=3, msgsclr=3, headclr=2, taskclr=3

The number of elements are given by $#array.



#!/usr/bin/perl

use strict;
use warnings;

open FILE, ".toprc" or die $!;
my @array = <FILE>;

print "Number of elements in array: " . eval($#array + 1) . "\n";

my $NUM = 0;

while ($NUM < $#array + 1) {
print "$NUM : $array[$NUM]";
$NUM++;
}

# alternative, much better suited for arrays:

$NUM = 0;
foreach my $array_element ( @array) {
print "$NUM : $array_element";
$NUM++;
}

Though in this case $NUM is just superfluous. The most simple and effective is:

foreach my $array_element ( @array) {
print $array_element;
}

--
Sandy

List replies only please!
Please address PMs to: news-reply2 (@) japantest (.) homelinux (.) com
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >
Follow Ups
References