http://www.perlmonks.org?node_id=997856


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?

This solution doesn't work since map assigns the return value of clean() to the array. clean() will return the result of the last line which is either 1 or '' for this string. You can make it work by adding $_ in the map block. Not sure if the op wanted the empty final value but they provided a variable for it.

#!/usr/bin/perl use strict; use warnings; my $str = "item1 | item2| item3 |item4| "; my @cleaned = map { clean($_) } split( /\|/, $str ); print ">$_<\n" foreach @cleaned; print "*"x75,"\n"; my @cleaned2 = map { clean($_);$_ } split( /\|/, $str ); print ">$_<\n" foreach @cleaned2; sub clean { chomp($_[0]); $_[0] =~ s/^\s+//; $_[0] =~ s/\s+$//; } >1< >< >1< >< >< ********************************************************************** +***** >item1< >item2< >item3< >item4< ><

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

    Thanks for that, I surely should have thought of it.

    Sorry if my advice was wrong.