Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Ignorance Isnt Always Bliss

by GrandFather (Saint)
on Feb 12, 2008 at 02:27 UTC ( #667494=note: print w/replies, xml ) Need Help??


in reply to Ignorance Isnt Always Bliss

Perl, by default (never change the default) indexes arrays starting at 0. However Perl has a really nice trick where you can use -1 as the index for the last element in the array, -2 for the penultimate element, and so on.

The trap that bit you is that if the array is empty then there is no element at the end of the array so trying to access it using [-1] leads to an unhappy life.

Note that you can use $#array to get the index of the last element in @array. $#array will return -1 if the array is empty. @array in scalar context returns the number of elements in the array and returns 0 for an empty array.


Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^2: Ignorance Isnt Always Bliss
by Baffled (Acolyte) on Feb 12, 2008 at 03:48 UTC
    You can end this string, the problem has been determined a missing / at the end of a url in the lib pointing to the dir that the array was trying to read.

      After applying PerlTidy the following looks interesting:

      open THEFILE, "$config{'basepath'}$key/$file"; my ( $title, $reserve, $inc, $desc, $image1, $image2, $image3, $image4, $thumb1, $thumb2, $thumb3, $thumb4, $dutch, $qty, $bold, $highlight, $feat, $catfeat, $grabber, $relist, $buyit, $gallery, $counter, $ship1, $ship2, $ship3, $ship4, $ship5, $shipcost, $location, $pay1, $pay2, $pay3, $pay4, $pay5, $pay6, $pay7, $pay8, $pay9, $paypal, $idata1, $idata2, $idata3, $idata4, $idata5, $idata6, $idata7, $idata8, $idata9, @bids ) = <THEFILE>; my ( $alias, $email, $bid, $time, $add1, $add2, $add3, $oqty, $qtysold ) = &read_bid ($bids[$#bids]);

      Notice that @bids is set by reading a file (my (..., @bids) = <THEFILE>) and that then, without any checking at all, the presumed to exist last element is accessed. If there are too few lines in the input file then the 'last element access' will fail.

      Looks to me like there is far too much string and bubble gum involved in holding this system together!


      Perl is environmentally friendly - it saves trees
Re^2: Ignorance Isnt Always Bliss
by Anonymous Monk on Feb 12, 2008 at 07:13 UTC
    As an example of Grandfather's comments about $#array and its value for an empty array, and as a duplication of the specific error message you are getting:

    perl -wMstrict -e "my @bids = (); chomp($bids[$#bids])" Modification of non-creatable array value attempted, subscript -1 at -e line 1.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2021-03-04 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (107 votes). Check out past polls.

    Notices?