From: Mr P on
]
>   P>   $c++; # wow that's weird
>
> wierd in what way?
Weird in that the variable name accidentally stumbled on another
language? Just a little bad humor Uri no worries..

>
>   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.
respectfully- this is just another way to specify a for loop-
precisely what I was trying to avoid..


>
>   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??
Maybe I mis-read
>
>   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
-
> ---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com---------

Thanks Uri and everyone, but to summarize , what I'm attempting to do,
apparently, isn't supported..

It seems odd to me that it's not, I want to MAP an incremented value
over an array- sounds like a perfectly logical thing to want to do?

I realize of course from my, and everyone else's contributions, there
are myriad ways to accomplish this with for or foreach. But that's
precisely what I DID NOT want to do. THat's the beauty and elegance of
map- it operates in 2D.
From: Mr P on

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

Thank-You sir but I was looking for a "mapish" one-liner. I like your
style though!
From: Peter J. Holzer on
On 2010-05-11 19:18, Mr P <misterperl(a)gmail.com> wrote:
>>
>> � 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.
> respectfully- this is just another way to specify a for loop-
> precisely what I was trying to avoid..

In some sense map is also just another way to specify a for loop.

The difference is mainly that map returns a result, but for doesn't. So
the rule in perl is: If you are interested in the result, use map.
Otherwise use for.

You don't use the result of the map, so you should use for.
(it does the same thing, but it's more readable)


>> � P> I read and read on this, and I also tried:
>> � P> � map s/^(CAT)/$1 $c++/e, @a;
[...]
>> 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++
>>
>
> Thanks Uri and everyone, but to summarize , what I'm attempting to do,
> apparently, isn't supported..

It is. Uri already gave you one solution. Plus several hints in the
right direction.

I'll give you another hint:

Uri wrote that
$1 $c++
by itself is not valid perl.

You cannot (for example) write
$x = $1 $c++;

How would you write this line, so that it becomes valid Perl and does
what you want? If you know this you also know how to rewrite your
substitution so that it works.

hp
From: Uri Guttman on
>>>>> "P" == P <misterperl(a)gmail.com> writes:

>> � 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> respectfully- this is just another way to specify a for loop-
P> precisely what I was trying to avoid..

huh?? map IS a loop too! you are just using map for the loop and not for
the primary purpose which is to return a list. since you don't return a
list, don't mislead the code reader with map. use foreach modifier which
says i am doing something in a loop and not building up a list.
>>
>> � 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> Maybe I mis-read
>>
>> � 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
P> -
>> --------- �Gourmet Hot Cocoa Mix �---- �http://bestfriendscocoa.com---------

P> Thanks Uri and everyone, but to summarize , what I'm attempting to do,
P> apparently, isn't supported..

no, your brain isn't supporting it. perl can do what you want as long as
you code it correctly. you have SYNTAX errors in your code. as well as a
misunderstanding of the semantics of /e.

P> It seems odd to me that it's not, I want to MAP an incremented value
P> over an array- sounds like a perfectly logical thing to want to do?

stop capitalizing MAP. it is map and you really want foreach modifier as
i keep saying. otherwise you are misleading the code reader. this is not
an obsfucation contest, use the correct feature.

P> I realize of course from my, and everyone else's contributions,
P> there are myriad ways to accomplish this with for or foreach. But
P> that's precisely what I DID NOT want to do. THat's the beauty and
P> elegance of map- it operates in 2D.

no it doesn't. you don't get map vs foreach modifier vs a full
loop. they ALL LOOP. they are different in syntax and semantics but they
are all loops. you need a loop. even a /g on your s/// will be a
LOOP. you can't avoid LOOPS if you want to do something over and over.

and you can even avoid map likely with a /g modifier. but i don't think
i will help you until you learn about perl LOOPS, implied or explicit.

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: Tad McClellan on
Uri Guttman <uri(a)StemSystems.com> wrote:

> but i don't think
> i will help you until


You are slow on the uptake :-)

I've been ignoring this poster for years.

http://groups.google.com/groups/search?as_umsgid=1135692143.665441.112530%40g14g2000cwa.googlegroups.com


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.