in reply to Playing with $[

Why are your symbols called calar and rray?

I think you thought $[ is some array index pointer, but it's not. $[ sets the base index, and you should never change it. Its documentation (from perlvar) clearly states what it does:

$[
The index of the first element in an array, and of the first character in a substring. Default is 0, but you could theoretically set it to 1 to make Perl behave more like awk (or Fortran) when subscripting and when evaluating the index() and substr() functions. (Mnemonic: [ begins subscripts.) As of release 5 of Perl, assignment to $[ is treated as a compiler directive, and cannot influence the behavior of any other file. Its use is highly discouraged.
You probably meant something like:
@array = qw(foo bar spam eggs); for ($index = 0; $index < @array; $index++) { print $array[$index]; }
But in that case, I think you should really use the simpler and more efficient for VAR (LIST) BLOCK. And don't forget to use strict.
use strict; my @array = qw(foo bar spam eggs); for my $element (@array) { print $element; }
(Note that the last for loop could also just have been print for @array, or if $, isn't set even print @array.)

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Replies are listed 'Best First'.
Re: Playing with $[
by cLive ;-) (Prior) on Jul 06, 2002 at 15:00 UTC

    Good advice Juerd, but I think "obf.pl" is probably short for obfuscate.pl - hence the touching-things-that-should-be-left-alone-type-thingy...

    .02

    cLive ;-)

    --
    seek(JOB,$$LA,0);

      cLive ;-) is right. The whole point of this exercise is finding fun obfuscation methods. I read through the camel book on a regular basis, I know what the role of $[ is, I definitely know how to iterate through an array; I'm looking for fun and obscene ways to screw with code.

      LAI
      :eof