Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • So I'll have the ID # and a list of other ID #'s and I need to know if it's the smallest.
    You went a very convoluted route to do this:
    if ( $id_number == (sort @ids)[0] )
    Learn about literal slice: (LIST)[INDICIES]. We document it in Learning Perl.
    • Randal L. Schwartz
    • Stonehenge
    • *grin* - yeah, I know. I should have put a * next to "good" - I'm not endorsing that as THE way: TIMTOWTDI and all that. And your approach is definitely cleaner ... I was just thinking about how to incorporate what I learned about scalarizing a list.


  • So I'll have the ID # and a list of other ID #'s and I need to know if it's the smallest.

    That code is very confusing. Sorta like taking a flight from NY to LA by via Johannesburg and Perth. Why not just grab the smallest element of the list?

    use List::Util qw(min);

    if ( $id_number == min(@ids) )

    If you needed to avoid List::Util for some odd reason, I don't know why you would sort a list, reverse it and take the last element (in a most convoluted fashion) when you could just:

    if ( $id_number =

    • *nod* - agreed. See prev. comment - I simply wanted to apply what Cees taught me about scalarizing a list.

      Hmmm -- maybe this won't even see the light of day in my import script now. ;)


  • I thought it was common knowledge that there is no such thing as a list in scalar context. :-)

    The context issues are particularly important when you return things. my $foo = bar(); will produce a different result with a bar function that does return @arr; vs one that does return ( $baz1, $baz2, $baz3 ). This is even more insidious if you do something like return grep quux($_), @arr.

    Always mind your context.

    Your particular example, btw, besides all the issues already pointed out by others, should at l