in reply to Trying to slice off all array elements but the last one

My fall back plan is to copy the array, pop off the last element and use that.

Is there a reason you don't go with the fall back plan and be done with it? Copy/pop is a solution that's very unlikely to confuse whoever picks up the code next.

  • Comment on Re: Trying to slice off all array elements but the last one

Replies are listed 'Best First'.
Re: Re: Trying to slice off all array elements but the last one
by talexb (Chancellor) on Feb 18, 2004 at 17:38 UTC

    As usual, I'm dealing with a list of objects that came from a database. So I ask for one more than the number I really want so that I can tell if there are more to come. Before I actually display the items, I pop the last one off the list -- except in the case that this is the last page, in which case the last one is a real item.

    Aside: I'm thinking about a different approach -- since I know what offset I'm at, how many objects I show on each page and how many objects are in the total list, I shouldn't have to fall back to 'getting an extra one' -- but that's a comment for another node. And I've also read (somewhere) about a module that takes care of all such list related functions, I jsut haven't used any of them yet. So many modules, so little time.

    So, in my final implementation I actually took the array, popped off the extra one (if I wasn't on the last page) and did the search on that.

    But as a somewhat experienced Perl dude, I should have remembered the $#array bit to provide me with the index of the last member of the array and been able to go from there. Sometimes stuff doesn't come loose when I shake my head. :( That's when I resort to Perl Monks. :)

    Thanks for your response.

    Alex / talexb / Toronto

    Life is short: get busy!