PetaMem has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

Well ... I mean: both return a list no?

So while it is perfect to write

my $foo = (stat($file))[3];
why can't we (me and Perl) be friends on
my $lf = (sort(<*>))[-1];
Update: I didn't test exactly the code above, but embedded the sort-index within a print (see my post below). That throws a syntax error (which I still don't understand why). The code above works.

Bye
 PetaMem
    All Perl:   MT, NLP, NLU

Replies are listed 'Best First'.
Re: What's the difference between stat and sort?
by beable (Friar) on Jul 15, 2004 at 09:57 UTC
    Huh? Seems to work here. Both of these work for me:
    perl -e 'my $lf = (sort(<*>))[-1];print $lf'
    and
    perl -e 'my $foo = (stat("myfile"))[7];print $foo'
    .
      Aaah. As the germans say: "I get the crisis!"

      my $lf = (sort(<*>))[-1]; print $lf,"\n";
      works. While the (what I tested)
      print (sort(<*>))[-1], "\n";
      throws a
      print (...) interpreted as function at ./template.pl line 6.
      syntax error at ./template.pl line 6, near ")["
      Execution of ./template.pl aborted due to compilation errors.
      

      Bye
       PetaMem
          All Perl:   MT, NLP, NLU

        Oh that's easy, parenthesisation. Try this:
        perl -e 'print((sort(<*>))[1]);'

        Once the expression is fully inside parentheses, there is no syntax error. With the square brackets outside the parens, it must be trying to get the first element of the array returned by print(), which doesn't return an array.

Re: What's the difference between stat and sort?
by BrowserUk (Patriarch) on Jul 15, 2004 at 10:15 UTC

    If you want the lexically last file in the directory, you don't need to sort as glob sorts it output anyway--least on my system it does.

    perl -le"print +(<*>)[-1]" XXX_I386

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
      "At least on my system"? Oh now that's not good enough! perldoc -f glob says: "Beginning with v5.6.0, this operator is implemented using the standard "File::Glob" extension. See File::Glob for details."

      perldoc File::Glob says: ""GLOB_NOSORT" By default, the pathnames are sorted in ascending ASCII order; this flag prevents that sorting (speeding up bsd_glob())."

      So there we go, a definitive answer for Perls greater than 5.6.0. As for previous Perls, upgrade already!

        I didn't need to look it up. I already knew it sorted on my system :)

        Upgrade?


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re: What's the difference between stat and sort?
by Tomte (Priest) on Jul 15, 2004 at 10:00 UTC

    works perfectly:

    [1158]tom@margo scratch $ perl -e 'my $lf = (sort(<*>))[-1]; print $lf +, "\n";' zoom?id=aaaaaaaaaaaawax
    zoom... is (a file wget produced and) the last element in the sorted list.

    What did you expect? What did you get?

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

Re: What's the difference between stat and sort?
by Anonymous Monk on Jul 15, 2004 at 10:05 UTC

    Perhaps there are no files in the directory where you tried you second snippet?

    Try this in any *nix box:

    perl -e 'my $lf = (sort(</bin/*>))[-1];print $lf'