From: Mr P on
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
From: Mr P on
On May 10, 3:44 pm, Mr P <misterp...(a)gmail.com> wrote:
> 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

oops I mean I ALSO TRIED s/^(CAT)/$1 $c+/e, @a; # single +
From: Uri Guttman on
>>>>> "P" == P <misterperl(a)gmail.com> writes:

P> my $c = 0;
P> for ( @a )
P> {
P> next unless /^CAT/;
P> $c++; # wow that's weird

wierd in what way?

P> $_ .= " $c";
P> }

P> But what I'd prefer is something like

P> my $c = 1;
P> map s/^(CAT)/$1 $c++/e, @a;

that won't work. the replacement is an expression with /e. you are
thinking it is a string AND an expression. so make an expression
(multiple statements are allowed) with the last one being the
replacement value. something like this: (untested)

s/^(CAT)/$c++ ; "$1 $c:/e foreach @a;

and don't use map without returning a list. foreach modifier is better
for that.

P> I read and read on this, and I also tried:
P> map s/^(CAT)/$1 $c++/e, @a;

P> since it appeared that within the RHS of the s///, a single + was an
P> increment operator.

huh??

P> Am I in the ballpark here guys? Everything I've tried results in
P> interpreter errors.

well, everything you tried is a syntax error. as i said, the code in the
replacement part must be a valid expression under /e.

this by itself is not valid perl:

$1 $c++

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Dr.Ruud on
Mr P wrote:
> 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

perl -MData::Dumper -wle'
my (@r, %c);
push @r, [$_, ++$c{$_}]
for qw/ CAT DOG CAT MOUSE EEL CAT /;
print Dumper(\@r);
'

--
Ruud
From: Dr.Ruud on
Dr.Ruud wrote:
> Mr P wrote:

>> 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
>
> perl -MData::Dumper -wle'
> my (@r, %c);
> push @r, [$_, ++$c{$_}]
> for qw/ CAT DOG CAT MOUSE EEL CAT /;
> print Dumper(\@r);
> '

And use %c to decide whether you want to print the "1".

perl -MData::Dumper -wle'
my (@r, %c, $k);
push @r, [$_, ++$c{$_}] for qw/ CAT DOG CAT MOUSE EEL CAT /;
print $k=$_->[0], $c{$k}>1 ? " $_->[1]" : "" for @r;
'
CAT 1
DOG
CAT 2
MOUSE
EEL
CAT 3

--
Ruud