Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: List-to-Scalar Function?

by perlmonkey (Hermit)
on May 09, 2001 at 03:45 UTC ( #78994=note: print w/ replies, xml ) Need Help??


in reply to List-to-Scalar Function?

To implement it using your syntax you could do something like:

sub operate (&@) { my $sub = shift; local $~; $~ = &$sub($_) for @_; return $~ }


So for your examples you would get:
use strict; sub operate (&@) { my $sub = shift; local $~; $~ = &$sub($_) for @_; return $~ } my @list = (4, 6, 2, 67, 123, 645, 23, 54 ); my $max = operate { $~ > $_ ? $~ : $_ } @list; print $max, "\n"; my $string = operate { $~.$_ } @list; print $string, "\n"; open FILE, $0; my $count = operate { $~ + /operate/ } <FILE>; print "Count = $count\n";


And the results are
max = 645 string = 462671236452354 count = 4


Comment on Re: List-to-Scalar Function?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (14)
As of 2015-07-02 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (41 votes), past polls