M
Mr P
I have an array in which I want to add a counter - for example, change
CAT to CAT #:
....so
CAT
DOG
CAT
MOUSE
EEL
CAT
becomes
CAT 1
DOG
CAT 2
MOUSE
EEL
CAT 3
Obviously I can write a little loop like
my $c = 0;
for ( @a )
{
next unless /^CAT/;
$c++; # wow that's weird
$_ .= " $c";
}
But what I'd prefer is something like
my $c = 1;
map s/^(CAT)/$1 $c++/e, @a;
I read and read on this, and I also tried:
map s/^(CAT)/$1 $c++/e, @a;
since it appeared that within the RHS of the s///, a single + was an
increment operator.
Am I in the ballpark here guys? Everything I've tried results in
interpreter errors.
Thanks,
MP
CAT to CAT #:
....so
CAT
DOG
CAT
MOUSE
EEL
CAT
becomes
CAT 1
DOG
CAT 2
MOUSE
EEL
CAT 3
Obviously I can write a little loop like
my $c = 0;
for ( @a )
{
next unless /^CAT/;
$c++; # wow that's weird
$_ .= " $c";
}
But what I'd prefer is something like
my $c = 1;
map s/^(CAT)/$1 $c++/e, @a;
I read and read on this, and I also tried:
map s/^(CAT)/$1 $c++/e, @a;
since it appeared that within the RHS of the s///, a single + was an
increment operator.
Am I in the ballpark here guys? Everything I've tried results in
interpreter errors.
Thanks,
MP