Re: search and replace on an array



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: VB-101: Passing Arrays ByVal vs ByRef
    ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Need help with textboxes
    ... The JavaScript 1.5 Reference already states: ... All forms and their children are stored in an array ... use dot notation or object literals. ... No. Bracket property accessors allow their argument to be any string value. ...
    (comp.lang.javascript)
  • Re: Garbage Collection Issues in long-standing services
    ... the pinned array should get unpinned or ... The receive case is done quite well, I do create a 4K buffer and reuse it ... and remove the reference to my wrapper socket class, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to give selective access to the methods in a class?
    ... different memory locations at different times. ... The reference still leads to the ... So array resizing ... program pushes certain objects into the circular buffer, ...
    (comp.lang.java.programmer)
  • Re: Perlish map() function
    ... the reference material, the Array constructor was introduced in JavaScript ... evaluation would result in a ReferenceError. ... identifier shows that it is true. ...
    (comp.lang.javascript)