[28048] in Perl-Users-Digest
Perl-Users Digest, Issue: 9412 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jun 30 18:10:17 2006
Date: Fri, 30 Jun 2006 15:10:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 30 Jun 2006 Volume: 10 Number: 9412
Today's topics:
search and replace on an array <ebmarquez@gmail.com>
Re: search and replace on an array <David.Squire@no.spam.from.here.au>
Re: search and replace on an array <David.Squire@no.spam.from.here.au>
Re: search and replace on an array <ebmarquez@gmail.com>
Re: search and replace on an array <xicheng@gmail.com>
Re: search and replace on an array <David.Squire@no.spam.from.here.au>
Re: search and replace on an array <jgibson@mail.arc.nasa.gov>
Re: search and replace on an array <mritty@gmail.com>
Re: search and replace on an array <ebmarquez@gmail.com>
Re: search and replace on an array <uri@stemsystems.com>
Re: Single-liner for one-line substitute? <xicheng@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 30 Jun 2006 08:22:07 -0700
From: "ebm" <ebmarquez@gmail.com>
Subject: search and replace on an array
Message-Id: <1151680927.093019.43170@m73g2000cwd.googlegroups.com>
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?
------------------------------
Date: Fri, 30 Jun 2006 16:37:31 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: search and replace on an array
Message-Id: <e83gfr$spq$1@news.ox.ac.uk>
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\\");
>
> @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
}
}
> $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?
>
------------------------------
Date: Fri, 30 Jun 2006 16:40:59 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: search and replace on an array
Message-Id: <e83gmb$spq$2@news.ox.ac.uk>
David Squire wrote:
> 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:$!";
> # ... do stuff
> }
> }
------------------------------
Date: 30 Jun 2006 08:51:00 -0700
From: "ebm" <ebmarquez@gmail.com>
Subject: Re: search and replace on an array
Message-Id: <1151682660.215540.121620@i40g2000cwc.googlegroups.com>
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!
David Squire wrote:
> David Squire wrote:
>
> > 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:$!";
>
> > # ... do stuff
> > }
> > }
------------------------------
Date: 30 Jun 2006 09:09:36 -0700
From: "Xicheng Jia" <xicheng@gmail.com>
Subject: Re: search and replace on an array
Message-Id: <1151683776.421679.212880@i40g2000cwc.googlegroups.com>
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\\");
>
> @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
------------------------------
Date: Fri, 30 Jun 2006 17:14:21 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: search and replace on an array
Message-Id: <e83iku$97$1@news.ox.ac.uk>
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 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:$!";
>>
>>> # ... 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
------------------------------
Date: Fri, 30 Jun 2006 09:27:46 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: search and replace on an array
Message-Id: <300620060927465656%jgibson@mail.arc.nasa.gov>
In article <1151682660.215540.121620@i40g2000cwc.googlegroups.com>, ebm
<ebmarquez@gmail.com> wrote:
> 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?
s/search/replace/ for @array;
------------------------------
Date: 30 Jun 2006 10:02:34 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: search and replace on an array
Message-Id: <1151686954.360002.84880@p79g2000cwp.googlegroups.com>
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
------------------------------
Date: 30 Jun 2006 11:20:38 -0700
From: "ebm" <ebmarquez@gmail.com>
Subject: Re: search and replace on an array
Message-Id: <1151691638.522580.199200@h44g2000cwa.googlegroups.com>
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
------------------------------
Date: Fri, 30 Jun 2006 15:22:07 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: search and replace on an array
Message-Id: <x78xnetpnk.fsf@mail.sysarch.com>
>>>>> "DS" == David Squire <David.Squire@no.spam.from.here.au> writes:
DS> @PathFiles = map { $_ =~ s/^\Q$path\E/$newPath/; $_ } @PathFiles;
that is much cleaner with the for modifier:
s/foo/bar/ for @list ;
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: 30 Jun 2006 09:31:27 -0700
From: "Xicheng Jia" <xicheng@gmail.com>
Subject: Re: Single-liner for one-line substitute?
Message-Id: <1151685087.490186.169530@d30g2000cwa.googlegroups.com>
Xicheng Jia wrote:
> John W. Krahn wrote:
>
> perl -i -0777pe 's/(?:\G|^)(.*?)old/$1new/g' file*
Just make a note: the start of line anchor ^ is redundant in this
pattern when the anchor \G shows up. so it should be:
perl -i -0777pe 's/\G(.*?)old/$1new/g' file*
Xicheng
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 9412
***************************************