Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by biosysadmin (Deacon)
on Mar 17, 2004 at 07:50 UTC ( [id://337248]=note: print w/replies, xml ) Need Help??


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

I actually prefer to do the following for the sake of readability:
($foo,undef) = split( /\./, $bar );
That way I explicitly throw away the rest of the list instead of discarding it implicitly.

Replies are listed 'Best First'.
Re: Re: Re: Returning first element of an array from a function
by Anonymous Monk on Mar 17, 2004 at 08:43 UTC
    ($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);).

      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://337248]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-23 07:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found