Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Playing with $[

by LAI (Hermit)
on Jul 06, 2002 at 14:37 UTC ( [id://179831]=perlquestion: print w/replies, xml ) Need Help??

LAI has asked for the wisdom of the Perl Monks concerning the following question:

I've been trying to map out the rules that govern manipulation of $[. Basically what I want to do is something like:
@rray = ( 'foo', 'bar', 'spam', 'eggs' ); for ($calar=0; $calar < @rray; $[++) { print "$rray[$calar]"; }
but when I run the above code I get That use of $[ is unsupported at obf.pl line 2. The below code sort of works, but the value of $[ gets reset to 0 for every iteration so I get a console full of foo.
@rray = ( 'foo', 'bar', 'spam', 'eggs' ); for ($calar=0; $calar < @rray; $[ = $[+1) { print "$rray[$calar]"; }
Even if I increment another variable and assign its value to $[, I get the same That use of $[ is unsupported at obf.pl line 2. error as before. What is going on? How can I use $[ to freely play with arrays?

LAI
:eof

edited: Sat Jul 6 16:39:12 2002 by jeffa - title change (was: Playing with <code>$</code>?)

Replies are listed 'Best First'.
Re: Playing with <code>$[</code>?
by Juerd (Abbot) on Jul 06, 2002 at 14:46 UTC

    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.
    

      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
Re: Playing with <code>$[</code>?
by licking9Volts (Pilgrim) on Jul 06, 2002 at 15:45 UTC
    Recently, I too had a question about the $[ variable and got some great responses regarding its use. You might find them useful as well: Special variable - set array base.

Re: Playing with <code>$[</code>?
by broquaint (Abbot) on Jul 06, 2002 at 14:53 UTC
    I believe the use of $[ has been deprecated in recent versions of perl which is why you're getting the error. You can achieve the same effect by taking advantage the for statement modifer
    @array = qw(foo bar spam eggs); print for @array;
    This is infintely safer and saner than mucking about with $[ who's inclusion is mainly for programmer compatibility with AWK. See the perlvar manpage for more info on $[.
    HTH

    _________
    broquaint

Re: Playing with $[
by jsprat (Curate) on Jul 06, 2002 at 18:29 UTC
    The 'use' that perl is complaining about is '$[ = $[+1'. From perlvar,

    As of release 5 of Perl, assignment to $[ is treated as a compiler directive

    You can assign to it at compile time (ie $[ = 5; ), but cannot modify it at runtime (ie $[++; ).

      Okay, I get it now. So I could still do some playing around with
      @rray = ( 'foo', 'bar', 'spam', 'eggs' ); $[=3; $calar = $[; print $rray[$calar] while $calar++<@rray;
      but not
      @rray = ( 'foo', 'bar', 'spam', 'eggs' ); $[=$#rray $calar = $[; print $rray[$calar] while $calar++<@rray;
      Good to know. Thanks, jsprat

      LAI
      :eof

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://179831]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-24 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found