Re: search and replace on an array
- From: David Squire <David.Squire@xxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 30 Jun 2006 17:14:21 +0100
ebm wrote:
[top-posting corrected. Please don't do that]
David Squire wrote:David Squire wrote:
Why do you want to do this? Why not just interpolate the directory pathWhoops! That should be:
and filename into a string at the time it is needed, e.g.:
for my $Dir ('c:/path/to', 'd:/new/path') { # note you don't need to use
Windows style slashes
for my $File ('File1', 'File2') {
open my $FileHandle, "$Dir/$File", '<' or die "Can't open
$Dir/$File:$!";
open my $FileHandle, '<', "$Dir/$File" or die "Can't open $Dir/$File:$!";
# ... do stuff
}
}
> Yeah, I know it can be done that way too, but that's not what I'm
> really looking for.
> This is a part of a larger script and I'm not going to rewrite the
> whole this for this.
> do you have any ideas on how to search and replace on an array?
>
Sure. Just use map, as you did in your original post:
----
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @files = ('File1','File2');
my $path = ('c:/path/to/');
my @PathFiles = map { $path . $_ } @files; # Why were you making an array ref here?
print Dumper(\@PathFiles);
my $newPath = 'D:/new/place/';
@PathFiles = map { $_ =~ s/^\Q$path\E/$newPath/; $_ } @PathFiles;
print Dumper(\@PathFiles);
----
Output:
$VAR1 = [
'c:/path/to/File1',
'c:/path/to/File2'
];
$VAR1 = [
'D:/new/place/File1',
'D:/new/place/File2'
];
DS
.
- Follow-Ups:
- Re: search and replace on an array
- From: Uri Guttman
- Re: search and replace on an array
- References:
- search and replace on an array
- From: ebm
- Re: search and replace on an array
- From: David Squire
- Re: search and replace on an array
- From: David Squire
- Re: search and replace on an array
- From: ebm
- search and replace on an array
- Prev by Date: Re: search and replace on an array
- Next by Date: Re: search and replace on an array
- Previous by thread: Re: search and replace on an array
- Next by thread: Re: search and replace on an array
- Index(es):
Relevant Pages
|