Mailinglist Archive: opensuse (2532 mails)

< Previous Next >
Re: [opensuse] Quick perl question - why are @array[$num] and $array[$num] the same?
  • From: G T Smith <grahamsmith@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
  • Date: Thu, 01 May 2008 09:03:14 +0100
  • Message-id: <48197942.4030600@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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>;

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

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

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

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?


The other (serious) responders are accurate however I suggest you get a
copy of the "Llama", i.e. Learning Perl, Randall L.Schwatz and Tom
Pheonix, O'Reilly Press (I do not know whether there is a newer edition
but I have the 3rd )...

The language has its nuances and this a good starting point to help
avoiding some beginner traps...


- --
==============================================================================
I have always wished that my computer would be as easy to use as my
telephone.
My wish has come true. I no longer know how to use my telephone.

Bjarne Stroustrup
==============================================================================
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFIGXlCasN0sSnLmgIRAnxxAJ9rjlU4evJJOiA51VVIuT6R5q0dtgCfRuai
TxOoAUD81DIjI20Ts8dkHvM=
=bZLL
-----END PGP SIGNATURE-----
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >
References