Re: search and replace on an array



Thanks everybody this was what I was looking for.


Paul Lalli wrote:
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

.



Relevant Pages

  • Re: Another Dereference Post by me
    ... Paul Lalli wrote: ... you're creating an anonymous array reference. ... You said you want to sort by the fifth column. ...
    (comp.lang.perl.misc)
  • Re: code explanation please...
    ... This makes perfect sense. ... I appreciate the explanation. ... Paul Lalli wrote: ... This is using what is known as an array slice. ...
    (perl.beginners)
  • Re: Best way to read a large file in line by line?
    ... Paul Lalli wrote: ... Why do you think you need the file into an array at all? ... lines in memory at the same time. ... If the current line's identifier has already been seen, ...
    (perl.beginners)
  • Re: Bug in debugger (or my code): Bizarre copy of ARRAY...
    ... >> the array @foo. ... > a hard reference in Perl? ... A hard reference is a variable that actually contains a reference to ... Paul Lalli ...
    (comp.lang.perl.misc)
  • Re: search and replace on an array
    ... You don't need double quotes, you don't need double-slashes, and you ... Did you mean to create an array of array references here? ... reference is a reference to an array that contains only one element? ...
    (comp.lang.perl.misc)