Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

perl equivalent of python's underscore?

by Anonymous Monk
on Oct 27, 2010 at 15:54 UTC ( [id://867759]=perlquestion: print w/replies, xml ) Need Help??

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

Hi --

I was wondering if perl has a throw-away variable so that I can toss unwanted values in list assignment, e.g.,

my ($x, $_, $z) = split(",",",'7,8,9');

The above almost works, except, I am cautious about assignment to $_, and I am wondering what it means if I declare $_ to be my-scoped.

Thanks!

Replies are listed 'Best First'.
Re: perl equivalent of python's underscore?
by BrowserUk (Patriarch) on Oct 27, 2010 at 15:57 UTC

    Use my ($x, undef, $z) = split(",",",'7,8,9');


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: perl equivalent of python's underscore?
by toolic (Bishop) on Oct 27, 2010 at 16:18 UTC
    Another way is to use an array slice:
    use strict; use warnings; my ($x, $z) = (split /,/, '7,8,9')[0,2]; print "$x $z\n"; __END__ 7 9

    I had to change your code because it did not compile for me. Take a look at split.

Re: perl equivalent of python's underscore?
by ig (Vicar) on Oct 27, 2010 at 16:28 UTC

    You can use a slice to get the elements you want, then you don't need a dummy variable.

    my ($x, $z) = (split(",",'7,8,9'))[0,2];

    I sometimes do the following:

    my ($day, $month, $year) = (localtime())[3,4,5];

    Alternatives seem too distracting:

    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); (undef,undef,undef,$mday,$mon,$year) = localtime();
Re: perl equivalent of python's underscore?
by mojotoad (Monsignor) on Oct 28, 2010 at 07:44 UTC
    Your question has more to do with scoping and syntax.
    Scope:
    Python scoping is not like Perl's. It's similar, but forget about 'brackets'. It's about function defs and where they happen.
    Syntax:
    The underscore '_' in Python is not special, it just happens to be a valid variable name. In conjunction with scoping, this has become short-hand for for a 'disposable' variable. Given the 'fill the slots' nature of assignment in Python, this turns out to be pretty useful, and therefore an idiomatic pythonic thing to do.

    And yes, I've had to go play on the dark side here and there.

    Cheers,
    Matt

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2024-04-25 00:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found