Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Special variable - set array base

by licking9Volts (Pilgrim)
on Jul 03, 2002 at 20:43 UTC ( #179308=perlquestion: print w/replies, xml ) Need Help??
licking9Volts has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm fairly new to Perl, and lately I've been converting my old awk scripts to Perl by using a2p. I'm running version 5.004_04 on Irix. (I know there's a newer version but our admins are tied down by corporate red tape when it comes to installing software). Anyway, I noticed that it always inserts these three lines in the generated Perl code:
$[ = 1; # set array base to 1 $, = ' '; # set output field separator $\ = "\n"; # set output record separator
I'm puzzled by the $[ variable. I read in Programming Perl that it's set to 1:
" make Perl behave more like awk...when evaluating the index and substr functions".
I don't mind using 0 as the array base so I started tinkering around with this variable just to see what would happen. Well, nothing happened when I changed it. Shouldn't it have some effect on arrays like this?:
... while (<FILE>) { chomp; # strip record separator @in_data = split(/ +/, $_); ...
I changed the array base to 0, 1, 5, 20, etc. and it had no effect on my program. So I read further in Programming Perl, (the next line as a matter of fact =), and it says:
"Because it was found to be so dangerous, assignment to $[ is now treated as a lexically scoped compiler directive and cannot influence the behavior of any other file."
Well I have no clue what that means. I was wondering if any of you could shed some light on what this variable does. I tried Super Search, but it keeps giving me errors. Thanks!

Replies are listed 'Best First'.
Re: Special variable - set array base
by Jim Morrison (Novice) on Jul 03, 2002 at 20:53 UTC
    "perldoc perlvar" command will say about such funny variables in detail, but in short why you did not noticed any distinctions when changing array bound base -- you did not relied on that in your script!

    Namely, you filled entire array with values, and after that you may be output that array out entirely, without specifying any bound at all.

    You'll see a diffenrence when you'll do some implicit indexing, such as $in_data[0] or somesuch.

    And remember once again, changing $[ is deprecated.

    update:: fixed a typo ($[ instead of $])

    Have a nice day,

Re: Special variable - set array base
by licking9Volts (Pilgrim) on Jul 03, 2002 at 21:17 UTC
    Jim, you're right. I forgot to show another important piece of my program. Here's a better snippet:
    #!/usr/sbin/perl -w use strict; $[ = 7; # set array base to 1 $, = ' '; # set output field separator $\ = "\n"; # set output record separator ... while (<FILE>) { chomp; # strip record separator @in_data = split(/ +/, $_); $out_raw = sprintf(" %8.0f %8.0f", $in_data[1], $in_data[2]); ...
    My program runs fine, I'm just curious about the $_ variable and whether or not it is still in use.

    Update: I checked perlvar, but it says pretty much the same thing as Programming Perl. It did add that the use of $[ is discouraged.
      Beleive me or not, I got different results when set $[ to 1, 7 and 0 running your piece of code. (slightly modified, but you did not provided exact code to execute. and I'll show you my logs if needed.)

      About usage of "$_" variable -- beware, your "while(<FILE>)" - this loop corrupts "$_", and "local $_;" statement somewhere before that loop will be a good idea, just to preserve its value.

      Sins of Perl Revisited will get you an idea what I mean. Here's an excelent explanation about power plant... well, read on, this certainly worth it!


Re: Special variable - set array base
by emilford (Friar) on Jul 03, 2002 at 20:53 UTC
    You might want to check this node for a start.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://179308]
Approved by emilford
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2017-10-20 13:39 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (263 votes). Check out past polls.