Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Accessing Arrays and Lists

by Anonymous Monk
on Jun 27, 2002 at 18:28 UTC ( [id://177805]=perlquestion: print w/replies, xml ) Need Help??

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

I'm a fan of brevity and one-liners, so here goes... I've been trying to access one element from an array, and the man pages tell me this:

$time = (stat($file))[8]; print $time;

That works fine and dandy, but then I can't do something like:

print (stat($file))[8];

(I hate temporary variables...) Perl gives me an error (Can't use subscript on scalar at - line 1, near "8]") and I'm not quite too sure why. On the other hand, in also needing to find the length of a hash, the page for scalar tells me I can force list context by doing the following:

print ${[stat($file)]}[8];

Which works fine. So, my question is, what's the difference between the two and why doesn't the first one work? // After a little more reading, I could deduce that the second method is accessing a singular element of a resolved array reference, which is defined by a list.

Replies are listed 'Best First'.
Re: Accessing Arrays and Lists
by demerphq (Chancellor) on Jun 27, 2002 at 18:35 UTC
    Welcome to the pain that prototypes can bring... ;-)
    that would be a different pia that is encountered with print occasionally, thanks to danger for the heads up

    Your problem here is that

    print (stat($file))[8];
    looks to perl just like
    print( stat($file) ) [8];
    its treating the outer pair of parenthesis as the parenthesis for the print function call, and not as get the stat results in list context and only use the 9th element

    This will work just fine in either of the following ways (and proabably a few more)

    print((stat($file))[8]);
    or even better (er IMO)
    print "".(stat($file))[8];
    or also (since stat returns a number)
    print 0+(stat($file))[8];
    and as danger points out below the best would probably be
    print +(stat($file))[8];
    HTH

    Updated Node

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

      Just to clarify. Parens immediately after a function are taken as delimiters for the argument list (as you rightly explained) --- but that has nothing to do with Perl's prototypes:

      sub foo { # no prototype here return ('a','b','c'); } # print (foo)[0]; # same error print +(foo)[0];

      I also show the unary + op as a better generalization of your last example --- it disambiguates without imposing numeric context.

        DOH.

        Of course you're right.

        I was thinking of the situation where the prototyping of print (which is the prototyping I meant, not the prototyping of stat) where it treats a function call as a filehandle. I know I've been nailed by that one before. (Although annoyingly i cant think of an example at the minute. Anybody knows of one please post it for the record)

        Oh and thanks, I had forgotten the + trick.

        Yves / DeMerphq
        ---
        Writing a good benchmark isnt as easy as it might look.

Re: Accessing Arrays and Lists
by Aristotle (Chancellor) on Jun 27, 2002 at 21:56 UTC

    danger++

    It's interesting however that your script even gets that far:
    $ perl -e'print (stat($file))[8];' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.
    ____________
    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found