search and replace on an array

E

ebm

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\\");

@PathFile = map { [ $path . $_ ] } @files;

$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, $_ );
}

# I want to do something like
# this @newPath =~ s/\Q$path\E/\Q$newPath\E/;


Can somebody give me a hand?
 
D

David Squire

ebm said:
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\\");

@PathFile = map { [ $path . $_ ] } @files;

Why do you want to do this? Why not just interpolate the directory path
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:$!";
# ... do stuff
}
}
 
D

David Squire

David said:
Why do you want to do this? Why not just interpolate the directory path
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:$!";

Whoops! That should be:
open my $FileHandle, '<', "$Dir/$File" or die "Can't open $Dir/$File:$!";
 
E

ebm

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?

Thanks!
 
X

Xicheng Jia

ebm said:
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\\");

@PathFile = map { [ $path . $_ ] } @files;

$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, $_ );
}

# I want to do something like
# this @newPath =~ s/\Q$path\E/\Q$newPath\E/;
try this:

@newPath = map { s/\Q$path\E/\Q$newPath\E/; $_ } @newPath;

(untested)
Xicheng
 
D

David Squire

ebm wrote:

[top-posting corrected. Please don't do that]
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
 
P

Paul Lalli

ebm said:
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
 
E

ebm

Thanks everybody this was what I was looking for.


Paul said:
ebm said:
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
 
U

Uri Guttman

DS> @PathFiles = map { $_ =~ s/^\Q$path\E/$newPath/; $_ } @PathFiles;

that is much cleaner with the for modifier:

s/foo/bar/ for @list ;

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top