Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How to call a function on each item after a split?

by CountZero (Bishop)
on Oct 08, 2012 at 15:59 UTC ( #997847=note: print w/replies, xml ) Need Help??


in reply to How to call a function on each item after a split?

Although using an array is usually a better solution, it can be done as follows:
my ($item1, $item2, $item3, $item4, $item5) = map {clean($_)} split(/\ +|/, $str);

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: How to call a function on each item after a split?
by Lotus1 (Curate) on Oct 08, 2012 at 18:54 UTC

    Actually your code assigns the return values from clean() which are either 1 or the empty string. The function uses the side effect of changing $_ but could be modified to return the trimmed string instead.

      Oops, my bad. I am so used to writing subs that return a meaningful result rather than do an in-place action-at-a-distance. MrSnrub has the right solution: just return $_ in the map.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997847]
help
Chatterbox?
[james28909]: i forgot how to link my scratch pad
[Lady_Aleena]: [pad://]
[james28909]: my scratchpad
[james28909]: and like i say, is just an example, not working code, but hopefully you understand what i mean. :)
[james28909]: and like i say, it would be alot of work to get around renaming 3 subs haha
[Lady_Aleena]: Well, you would have to add the file munging and a lot more.
[james28909]: yeah but it would rename those subs xD
[Lady_Aleena]: I'd say, go back to my scratchpad, and really look at what the 3 subs are doing, then play. Meanwhile, I am going afk for about an hour +/- a few minutes.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2017-05-24 05:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?