in reply to updating an array of strings

Here is one way - say you want to replace 'baz' with 'qux':
use strict; my @array = qw(foo bar bazzle baz); @array = map { s/^baz$/qux/;$_ } @array;
This will replace all occurances of 'baz' in the array however. You didn't specify if this is okay or not.

UPDATE: oops - yet another problem to not solve with map. Thanks for the tip grantm and Anony. :)

So ... let's say that you only wanted to replace the first occurrance of 'baz':
my @array = qw(foo bazzle baz baz); for (@array) { last if s/^baz$/qux/; }
much better, just don't try this with a list:
for (qw(foo bazzle baz baz)) { last if s/^baz$/qux/; }
which will not work, because you are trying to modify read-only values.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: (jeffa) Re: updating an array of strings
by Anonymous Monk on Aug 26, 2002 at 01:35 UTC
    Or even simpler:
    s/^baz$/qux/ for @array;
Re: (jeffa) Re: updating an array of strings
by grantm (Parson) on Aug 26, 2002 at 01:41 UTC

    Oops sorry jeffa, my reply (below, but typed before I saw yours) looks kind of confrontational :-(

    My understanding was that the map in your example would edit each element in place (via the 's/...') and then the assignment to @array would needlessly replace each element with a copy of the resulting string.

Re: (jeffa) Re: updating an array of strings
by kirk123 (Beadle) on Aug 26, 2002 at 01:49 UTC
    jeffa, Thanks for helping me. --kirk