Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by GrandFather (Sage)
on Oct 08, 2012 at 23:47 UTC ( #997897=note: print w/replies, xml ) Need Help??


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

map aliases $_ to each item in turn in the list it is dealing with. If you edit $_ in the map the original list items are edited. This can be an unwanted and nasty side effect. Note that aliasing in exactly the same way happens in Perl for loops.

Your clean sub doesn't return the cleaned string. It edits the passed in string in place (Perl essentially passes aliases of parameters into subs). Because the doesn't return the cleaned string you need to "return" the edited string from map, hence the '; $_;' bit in the map.

In this case a cleaner solution is to trim the white space in the split:

my $str = "item1 | item2| item3 |item4| "; print ">$_<\n" for split /\s*\|\s*/, $str, -1;

Prints:

>item1< >item2< >item3< >item4< ><
True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997897]
help
Chatterbox?
[PriNet]: i think i found the problem, the "source" array that sets the individual key values isn't being cleared, it's keeping the previous values...
[atcroft]: PriNet: You might consider posting a small, self-contained snippet of that segment of your code, and see if someone else has a better idea.....
[atcroft]: PriNet: Ahhh... interesting.
[PriNet]: small would be a problem *heh*... the program reads all the lines from a pdf and converts the individual colum data to a seperate key/value pair... that's where the "previous" array from the pdf is not being cleared properly before reading the next pdf
[atcroft]: PriNet: It was a thought....
[PriNet]: thanx... actually just having someone to talk to was making me think... *heh*
[PriNet]: a regular array would just be "delete @array" correct? no need for the post "()" ?

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2017-06-28 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (620 votes). Check out past polls.