Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Re: Re: Returning first element of an array from a function

by Anonymous Monk
on Mar 17, 2004 at 08:43 UTC ( [id://337259]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Returning first element of an array from a function
in thread Returning first element of an array from a function

($foo,undef) = split( /\./, $bar );
That way I explicitly throw away the rest of the list instead of discarding it implicitly.

You're not explicitly throwing away the rest of the list... you're just explicitly ignoring the second element returned and then non-explicitly ignoring the rest of it. Using undef to ignore returned elements is only useful if you're going to capture something after the undef'ed element (ie: ($foo, undef, $bar) = split(/\./, $baz); or (undef, $foo) = split(/\./, $bar);).

Replies are listed 'Best First'.
Re^4: Returning first element of an array from a function
by Anonymous Monk on Mar 17, 2004 at 08:55 UTC
    TIMTOWTDI of course:

    # this: ($foo, undef, $bar) = split(/\./, $baz); # can also be done as: ($foo, $bar) = (split(/\./, $baz))[0,2]; # and this: (undef, $foo) = split(/\./, $bar); # can also be done as: ($foo) = (split(/\./, $bar))[1];

      Oooooohhhh, but that builds up an entire list for the slicing and can damage performance horrendously:

      #!c:/perl/bin/perl -w $|++; use strict; use Benchmark 'timethese'; # make it so we split() into many many elements our $baz = join '', "yes.no.maybe.so." x 5000; timethese(2000, { undef => q{ ($foo, undef, $bar) = split(/\./, $baz); }, splice => q{ ($foo, $bar) = (split(/\./, $baz))[0,2]; } } ); __END__ Benchmark: timing 2000 iterations of splice, undef... splice: 101 wallclock secs (93.86 usr + 0.39 sys = 94.25 CPU) @ +21.22/s (n= 2000) undef: 0 wallclock secs ( 0.14 usr + 0.00 sys = 0.14 CPU) @ 14 +184.40/s ( n=2000) (warning: too few iterations for a reliable count)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-25 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found