Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

There is more than one way to do it. But not every way is a good one. For example, you mention scalar(@array) - 1 to get the last index. This is not a good way to get the last index. It is a very bad way to get the last index.

Just like in real language, it isn't only about what you say, but also HOW you express that. scalar(@array) does not mean "the last index plus one" or even "the number that will be the index if you add a new element". It says "the number of elements".

It would be sort-of okay to use scalar(@array) - 1 if $#array did not exist. But $#array does exist, and it says "the index of the last element in @array".

$#arrayThe index of the last element
$#array + 1The index of the last element plus one
@arrayThe number of elements
@array - 1The number of elements minus one

For the same reason, $#array + 1 would be WRONG if you want to get the number of elements.

Sometimes, unless is better than if. Sometimes unless is wrong and if is right. Sometimes you need to reverse the expressions (open or die versus die unless open) to get the perfect sentence.

TIMTOWDTI. NYPAGO. (Now You Pick A Good One)

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

P.S. $[ is irrelevant. Yes, it would sort of support my point. But even without $[, using $#array+1 instead of @array, or @array-1 instead of $#array would be wrong when you use code that doesn't express what you meant.


In reply to Re: TIMTOWTDI doesn't mean invent an outlandish approach (usually) by Juerd
in thread TIMTOWTDI doesn't mean invent an outlandish approach (usually) by davido

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 admiring the Monastery: (2)
As of 2024-04-26 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found