http://qs321.pair.com?node_id=930190


in reply to Re: array within an array
in thread array within an array

I got a bit nearer with
#! /usr/perl/bin use warnings; use strict; use Data::Dumper; use warnings; use strict; my @charges = qw/ch1 ch2 ch3 ch4 ch5 ch6 ch7 ch8 ch1 ch2 ch3 ch4 ch5 ch1 ch2 ch6 ch7 ch4 ch3 ch9 ch2 ch4/; my %bundle1 = map { $_ => 0 } qw(ch3 ch4 ch6); my $i; for (@charges) { if (exists $bundle1{$_}) { $i++; if ($i == 3){ print "chx "; $i = 0; } } else { print "$_ "; } } print "\n"; __END__ ch1 ch2 ch5 chx ch7 ch8 ch1 ch2 ch5 ch1 ch2 chx ch7 ch9 ch2 chx
But it assumes a lot and the last chx is wrong.

Replies are listed 'Best First'.
Re^3: array within an array
by cesear (Novice) on Oct 07, 2011 at 14:16 UTC
    This code is almost what I need, but using a count will not work (actually tried that) :). I only what the elements in @charges removed and replaced with chx if all the keys in %bundle1 are found. So that last element ch4 was replaced and it should not. This is where I am stuck.
      You need to see if you can find all the elements before you start to replace. So you need to hold them in memory somewhere. If you reach to end of the array before finding them, just put them as it is on their respective locations.