Re: RegEx Substitution + Arrays

2007-05-10 Thread Jenda Krynicky
From: Seanie [EMAIL PROTECTED]
 Rob Dixon wrote:
   map(s/$find/$replace/, @arr);
  Haha yes you can, but if you want to write nasty code go for
grep s/$find/$replace/, @arr;
  which also works.
 
 True, but grep implies find stuff, while map implies do stuff, so your 
 nasty code is way, way, nastier than mine - it masks the intention.
 
 Maybe using map in void context is deep-level wrongness, but it'd be nice if 
 somebody would explain WHY, instead of posting don't do it one-liners. 

Well, because it's for that implies do stuff. map means 
transform stuff and give me the results. With a for I expect that 
the work will be done as a side-effect, with map I expect that the 
code block will be side-effect free. That it will get a value and 
produce a value (or values). Sure, it may make sense to produce a 
value and do something more at the same time, but the produced value 
should be the main result.

Jenda
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: RegEx Substitution + Arrays

2007-04-26 Thread Dr.Ruud
yitzle schreef:
 What's the best way to apply a RegEx to an array? For loop?
 @arr = qw/dc2ds reew12dsfa df2fdw/;
 s/$find/$replace/ for(@arr);


Consider:

  s/\Q$find/$replace/ for(@arr);

-- 
Affijn, Ruud

Gewoon is een tijger.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RegEx Substitution + Arrays

2007-04-25 Thread yitzle

What's the best way to apply a RegEx to an array? For loop?
@arr = qw/dc2ds reew12dsfa df2fdw/;
s/$find/$replace/ for(@arr);

Thanks

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: RegEx Substitution + Arrays

2007-04-25 Thread Rob Dixon

yitzle wrote:

What's the best way to apply a RegEx to an array? For loop?
@arr = qw/dc2ds reew12dsfa df2fdw/;
s/$find/$replace/ for(@arr);


Yes. Exactly that.

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: RegEx Substitution + Arrays

2007-04-25 Thread John W. Krahn
yitzle wrote:
 What's the best way to apply a RegEx to an array? For loop?
 @arr = qw/dc2ds reew12dsfa df2fdw/;
 s/$find/$replace/ for(@arr);

Yes, although the parentheses are redundant.


John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: RegEx Substitution + Arrays

2007-04-25 Thread Seanie
yitzle wrote:
 What's the best way to apply a RegEx to an array? For loop?
 @arr = qw/dc2ds reew12dsfa df2fdw/;
 s/$find/$replace/ for(@arr);

Yep, you can do that. Or use map()

map(s/$find/$replace/, @arr);

-- 
[EMAIL PROTECTED] [pgp: 8A8FA6DE]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: RegEx Substitution + Arrays

2007-04-25 Thread Chas Owens

On 4/25/07, Seanie [EMAIL PROTECTED] wrote:

yitzle wrote:
 What's the best way to apply a RegEx to an array? For loop?
 @arr = qw/dc2ds reew12dsfa df2fdw/;
 s/$find/$replace/ for(@arr);

Yep, you can do that. Or use map()

map(s/$find/$replace/, @arr);


You should not use map in a void context, it is bad form.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: RegEx Substitution + Arrays

2007-04-25 Thread Seanie
Chas Owens wrote:
  map(s/$find/$replace/, @arr);
 You should not use map in a void context, it is bad form.

Care to explain?
Neither 'strict' nor 'warnings' complains, and it does what it says on the 
tin, but if I've missed something fundamental here I'd be grateful to know 
about it.

-- 
[EMAIL PROTECTED] [pgp: 8A8FA6DE]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: RegEx Substitution + Arrays

2007-04-25 Thread Rob Dixon

Seanie wrote:

yitzle wrote:

What's the best way to apply a RegEx to an array? For loop?
@arr = qw/dc2ds reew12dsfa df2fdw/;
s/$find/$replace/ for(@arr);


Yep, you can do that. Or use map()

map(s/$find/$replace/, @arr);


Haha yes you can, but if you want to write nasty code go for

 grep s/$find/$replace/, @arr;

which also works.

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: RegEx Substitution + Arrays

2007-04-25 Thread Rob Dixon

Sean King wrote:

Chas Owens wrote:

map(s/$find/$replace/, @arr);

You should not use map in a void context, it is bad form.


Care to explain?
Neither 'strict' nor 'warnings' complains, and it does what it says on the 
tin, but if I've missed something fundamental here I'd be grateful to know 
about it.


map() implements a mapping - a translation of one list into another by
applying a function to each element. Just because it has to apply this
function to every element in the target list isn't a good reason to use it
as a for loop.

Rob



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: RegEx Substitution + Arrays

2007-04-25 Thread Seanie
Rob Dixon wrote:
  map(s/$find/$replace/, @arr);
 Haha yes you can, but if you want to write nasty code go for
   grep s/$find/$replace/, @arr;
 which also works.

True, but grep implies find stuff, while map implies do stuff, so your 
nasty code is way, way, nastier than mine - it masks the intention.

Maybe using map in void context is deep-level wrongness, but it'd be nice if 
somebody would explain WHY, instead of posting don't do it one-liners. 

-- 
[EMAIL PROTECTED] [pgp: 8A8FA6DE]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: RegEx Substitution + Arrays

2007-04-25 Thread John W. Krahn
Seanie wrote:
 Rob Dixon wrote:
 map(s/$find/$replace/, @arr);
 Haha yes you can, but if you want to write nasty code go for
   grep s/$find/$replace/, @arr;
 which also works.
 
 True, but grep implies find stuff, while map implies do stuff, so your 
 nasty code is way, way, nastier than mine - it masks the intention.
 
 Maybe using map in void context is deep-level wrongness, but it'd be nice if 
 somebody would explain WHY, instead of posting don't do it one-liners. 

perldoc perl581delta
[ snip ]
   Miscellaneous Enhancements

   map in void context is no longer expensive. map is now context
   aware, and will not construct a list if called in void context.


So it is not really wrong anymore (for certain values of wrongness.)



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: RegEx Substitution + Arrays

2007-04-25 Thread Chas Owens

On 4/25/07, John W. Krahn [EMAIL PROTECTED] wrote:

Seanie wrote:
 Rob Dixon wrote:
 map(s/$find/$replace/, @arr);
 Haha yes you can, but if you want to write nasty code go for
   grep s/$find/$replace/, @arr;
 which also works.

 True, but grep implies find stuff, while map implies do stuff, so your
 nasty code is way, way, nastier than mine - it masks the intention.

 Maybe using map in void context is deep-level wrongness, but it'd be nice if
 somebody would explain WHY, instead of posting don't do it one-liners.

perldoc perl581delta
[ snip ]
   Miscellaneous Enhancements

   map in void context is no longer expensive. map is now context
   aware, and will not construct a list if called in void context.


So it is not really wrong anymore (for certain values of wrongness.)



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall


It is no longer inefficient; however, it is still an abuse of map and
longer than the for loop version.  It is hard to defend the use of map
in a void context.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/