Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

$#retval gives the index of the last element in the array. If an array is empty this will be -1. If it is zero then the array contains one element. Here is a demo:

my @retval; print "the last index of \@retval is $#retval\n"; print "the number of *elements* in \@retval is ", scalar @retval, "\n" +; push @retval, 'one value'; print "the last index of \@retval is $#retval\n"; print "the number of *elements* in \@retval is ", scalar @retval, "\n" +; push @retval, 'another value'; print "the last index of \@retval is $#retval\n"; print "the number of *elements* in \@retval is ", scalar @retval, "\n" +;

I fully understand the shorthand BTW ;-) The longhand is for your debugging benefit as your code craps out after this as you demonstrate! Also as noted you do not show the method where the error actually occurs. While using OO perl is a great idea your data structures are poor and do not take full advantage of the usual anonymous hash structure. Here is a very brief demo of how I might approach the problem:

package Item; use Data::Dumper; my @items; # initialise an array of item objects from our data while (<DATA>) { chomp; next unless $_; my $item = new Item; my ($id, $quant ) = split ','; $item->id($id); $item->quant($quant); push @items, $item; } print Dumper \@items; # now sell one of each item for my $item (@items) { $item->quant(-1); } print Dumper \@items; # now print out the id and quantities print "\n\nid quant\n"; for my $item (@items) { print $item->id, "\t", $item->quant, "\n"; } ############################## sub new { my $self = shift; return bless {}, $self; } # get or set id sub id { my ($self, $cid) = @_; $self->{'id'} = $cid if defined $cid; return $self->{'id'}; } # get or set numbers in stock sub quant { my ($self, $change) = @_; $self->{'quant'} += $change if defined $change; return $self->{'quant'} } __DATA__ item 1,10 item 2,20 item 3,30

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Re: Re: bugs... by tachyon
in thread subroutine not returning expected value by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (12)
    As of 2014-09-17 14:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (81 votes), past polls