Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Printing the last few elements of an array.

by dakkar (Hermit)
on Dec 16, 2002 at 17:29 UTC ( [id://220281]=note: print w/replies, xml ) Need Help??


in reply to Printing the last few elements of an array.

About 'the question you asked' in the second snippet: you got out a number, repeated 5 times, right?

This is because you wrote $line=@record, which means: assign to $line the number of elements in the array @record.

This happens because assigning to a scalar variables sets scalar context for the right-hand side of the assignment, and an array in scalar context evaluates to its lenght.

-- 
        dakkar - Mobilis in mobile

Replies are listed 'Best First'.
Re: Re: Printing the last few elements of an array.
by jonnyfolk (Vicar) on Dec 16, 2002 at 19:42 UTC
    Thanks dakkar - now I understand! Is there any way I could have phrased that to make the $count mechanism work?
      @record=reverse @record; my @record1; my $count=0; my $line; while (($count<5) && ($line=$record[$count])) { push @record1,$line; $count++ } @record1=reverse @record1; print join ',',@record1;

      The last reverse is to get the elements in the original order.

      Just promise me you won't really use this version, please? ;-)))

      -- 
              dakkar - Mobilis in mobile
      
        I really appreciate this, dakkar - I understand what pain it must have caused you to write that. I do promise not to use it - but it's very useful for me to see how it works! Thank you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-23 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found