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

Re: Passing results of a substitution to a function

by Coruscate (Sexton)
on Jan 20, 2003 at 03:44 UTC ( [id://228276]=note: print w/replies, xml ) Need Help??


in reply to Passing results of a substitution to a function

You could always use grep. Note that I changed the print() in blah() to a return, otherwise you'll end up printing an extra value out.:

my $word = 'BLAHBLAH'; print blah(grep{s/BLAH/COW/}$word); sub blah { my $stuff = shift; return "$stuff\n"; }

Replies are listed 'Best First'.
Re: Re: Passing results of a substitution to a function
by Anonymous Monk on Jan 20, 2003 at 23:24 UTC
    This seems to work quite nicely - but needs a small amount of tweaking to make sure grep doesn't return stuff in scalar context.
Re: Re: Passing results of a substitution to a function
by Anonymous Monk on Jan 20, 2003 at 23:50 UTC
    Ok - I now have something which works a lot more niftily:
    my $word = 'BLAHBLAH' blah(grep {s/BLAH/COW/ || 1 } $word);
    The { s/BLAH/COW/ || 1 } bit is required as I want to also pass in arguments which do not contain 'BLAH' (which grep would usually.. er.. grep away ;) ). I was worried about list context before because I was trying to get around this by using
    blah(grep(s/BLAH/COW/,$word)||$word)

Log In?
Username:
Password:

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

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

    No recent polls found