Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Inserting an element into an array after a certain element

by ikegami (Pope)
on Mar 31, 2005 at 19:07 UTC ( #443933=note: print w/replies, xml ) Need Help??


in reply to Re: Inserting an element into an array after a certain element
in thread Inserting an element into an array after a certain element

Ajusted to only do it once:

my $first = 1; @array = map { if ($first && $_ eq "c") { $first = 0; ($_, "x") } else { $_ } } @array;

hum, ugly. How about:

my $first = 1; @array = map { my @a = $_; push(@a, "x") if ($first && $_ eq "c"); $first ||= @a-1; @a } @array

Nope, still ugly. Well, you could always do:

foreach (0..$#array) { if ($array[$_] eq "c") { splice(@array, $_+1, 0, "x"); last; } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2020-11-28 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?