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

Re^3: $# -variable?

by reneeb (Chaplain)
on Jul 27, 2006 at 12:59 UTC ( #564059=note: print w/ replies, xml ) Need Help??

in reply to Re^2: $# -variable?
in thread $# -variable?

If someone plays with $[, and you are using a loop as shown above, you have problems with both expressions $#array and scalar(@array).

So, if you want to ensure that your loop does what you want, you have to use this one:

#!/usr/bin/perl use strict; use warnings; $[ = 3; my @array = qw(1 2 3 4); for my $i($[ .. $[ + (scalar(@array) - 1) ){ print $array[$i],"\n"; }

#!/usr/bin/perl use strict; use warnings; $[ = 3; my @array = qw(1 2 3 4); for my $i($[ .. $#array ){ print $array[$i],"\n"; }

Comment on Re^3: $# -variable?
Select or Download Code
Replies are listed 'Best First'.
Re^4: $# -variable?
by GrandFather (Sage) on Jul 27, 2006 at 19:36 UTC

    I would hope these days that $[ is virtually never used. I can imagine a few places where it may be tempting to use, but none where there are not much better techniques available.

    $['s biggest problem is the surprise factor. Non-zero based arrays are not the norm in Perl and using them is likely to make the code obscure and hard to maintain and its bigest weakness is that it provides nothing that cant be done better differently.

    DWIM is Perl's answer to Gödel

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://564059]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2015-10-07 17:28 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (194 votes), past polls