Re: search and replace on an array
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 30 Jun 2006 10:02:34 -0700
ebm wrote:
I'm looking for a quick way to do a search and replace on an array of
values.
for instance I can do it this way, but is there a better way?
my @files = ("File1","File2");
my $path = ("c:\\path\\to\\");
You don't need double quotes, you don't need double-slashes, and you
don't need bizarre Windows-style slashes.
my @files = ('File1', 'File2');
my $path = 'c:/path/to/';
@PathFile = map { [ $path . $_ ] } @files;
Did you mean to create an array of array references here? Where each
reference is a reference to an array that contains only one element?
If not, remove those [ ]:
my @PathFile = map { $path . $_ } @files;
$newPath = "D:\\new\\place";
Same as above:
my $newPath = 'D:/new/place';
# now I have my path and file names combined and
#I can do some stuff with this. after I've done my work
# with it, is there an easy way to change my path name in
# my @newPath array?
# I can do it this way but is there a better way?
foreach(@newPath){
$_ =~ s/C:\\/D:\\/;
push ( @newArray, $_ );
}
If you want the results to be in a different array from @newPath, as
you've shown above, the map solutions posted by others are your best
options...
# I want to do something like
# this @newPath =~ s/\Q$path\E/\Q$newPath\E/;
If, as the above non-working code suggests, you just want to change the
existing array, use a single statement modified by a for:
s/\Q$path\E/$newPath/ for @newPath;
Paul Lalli
.
- Follow-Ups:
- Re: search and replace on an array
- From: ebm
- Re: search and replace on an array
- References:
- search and replace on an array
- From: ebm
- search and replace on an array
- Prev by Date: Re: Single-liner for one-line substitute?
- Next by Date: Re: A question of speed
- Previous by thread: Re: search and replace on an array
- Next by thread: Re: search and replace on an array
- Index(es):
Relevant Pages
|