Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I can't see why you need to avoid using array indices at all.

First of all, you can get the start of the array index (of all arrays, actually) by using the special variable $[. The default value for this variable is 0. You are discouraged to modify this value in your script. Any change to this variable in other files (or packages? Not really sure about that one. See perlvar for more info. It's "files".) such as modules, will be visible in that file only. In short: I find it perfectly reasonable to always assume that the start index is 0 for your script. Always. Unless you, yourself, change it to something else.

Second, the last index for an array @array is $#array. No, $#array is not always 1 less than scalar(@array), that depends on the value for $[; but it is always the last index of your array.

In summary: it's perfectly 100% safe to write:

for my $i ($[ .. $#array) { $foo[$i] = $array[$i]; }
but at least I wouldn't mind at all if you hardcoded a 0 for that $[.

In reply to Re: Array indices by bart
in thread Array indices by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-19 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found