On Jul 20, Bryan R Harris said:
I'd like to turn array @tmp from:
(1,2,3)
to
(1,"|",2,"|",3)
@tmp = split(' ', join(" | ", @tmp));
... but it seems like a waste to create a string and then split it back up
again.
It would also break if elements of @tmp had spaces in them.
Well, here's one sneaky way. It assumes that no two elements in the array
are references to the same data:
my @array = ( 1 .. 3 );
my @with_pipes = map { \$_ == \$array[-1] ? $_ : ($_, "|") } @array;
The trick it uses is seeing if a reference to the current element is the
same as a reference to the LAST element. If it's not, we insert an
element after it.
You could also do it in two steps:
my @with_pipes = map { ($_, "|") } @array;
pop @with_pipes;
As a sidenote, are questions like this appropriate for the list? I really
don't *need* help with this, I was just hoping to expand my personal toolbox
with a better way to do something. Every time John, Wiggins, Luke, Bob,
Jeff, etc. respond to my emails I know I'm going to learn something.
I don't think there's anything wrong with asking such questions.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>