Re: trouble with list context assignment for substitution inside File::Find wanted function
Michael Alipio am Mittwoch, 24. Januar 2007 04:21: From: John W. Krahn [EMAIL PROTECTED] Sent: Wednesday, January 24, 2007 10:57:51 AM Yes, the substitution operator (s///) returns true (1) or false ('') in either list or scalar context. To do want you want you have to do the assignment first and then do the substitution: my $newname = $_; $newname =~ s/^\w+-//; Or in one statement: ( my $newname = $_ ) =~ s/^\w+-//; I've already figured that one out. However, I want to use variables for my regexp pattern. So I can replace axis with whatever I my first program argument is. [...] Hi Michael find (\renamefiles, './'); my $name = shift; You initialize $name after the call to find(), so renamefiles() has nothing in $name. Switch these lines (and test the user provided contents of $name) sub renamefiles{ if ($_ =~ /$name/){ if ($_ =~ /\Q$name\E/){ just in case $name contains chars that are special to the regex engine. my $oldname = $_; $_ =~ s/\w+-//; #rename ($oldname, $_) print $oldname will be renamed to $_\n; } } I got many of this: Use of uninitialized value in regexp compilation at test.pl line 11. Dani -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
trouble with list context assignment for substitution inside File::Find wanted function
Hi, I have a directory which contains several files. client1-2006-05-19.log.gz client1-2006-05-20.log.gz client1-2006-07-29.log.gz client1-2006-10-05.log.gz client1-2006-05-21.log.gz I want strip all of axisglobal- in their filenames. What I did was: #!/usr/bin/perl use warnings; use strict; use File::Find; find (\renamefiles, './'); sub renamefiles{ (my $newname) = $_ =~ s/^\w+-//g; #rename ($_, $newname); print $newname; } When I try printing the $newname which supposedly will print only 2006-N-N.log.gz, it instead prints a scalar value of 1, as if parenthesis around my $newname does not exists. And so, uncommenting the rename did do anything to my files. Any explanation to this? Do you have a perl one-liner to rename all files into their filenames with stripped ^\w+... thanks. Thanks. Do you Yahoo!? Everyone is raving about the all-new Yahoo! Mail beta. http://new.mail.yahoo.com
Re: trouble with list context assignment for substitution inside File::Find wanted function
Michael Alipio wrote: Hi, Hello, I have a directory which contains several files. client1-2006-05-19.log.gz client1-2006-05-20.log.gz client1-2006-07-29.log.gz client1-2006-10-05.log.gz client1-2006-05-21.log.gz I want strip all of axisglobal- in their filenames. What I did was: #!/usr/bin/perl use warnings; use strict; use File::Find; find (\renamefiles, './'); sub renamefiles{ (my $newname) = $_ =~ s/^\w+-//g; #rename ($_, $newname); print $newname; } When I try printing the $newname which supposedly will print only 2006-N-N.log.gz, it instead prints a scalar value of 1, as if parenthesis around my $newname does not exists. And so, uncommenting the rename did do anything to my files. Any explanation to this? Yes, the substitution operator (s///) returns true (1) or false ('') in either list or scalar context. To do want you want you have to do the assignment first and then do the substitution: my $newname = $_; $newname =~ s/^\w+-//; Or in one statement: ( my $newname = $_ ) =~ s/^\w+-//; Do you have a perl one-liner to rename all files into their filenames with stripped ^\w+. No. John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: trouble with list context assignment for substitution inside File::Find wanted function
- Original Message From: John W. Krahn [EMAIL PROTECTED] To: Perl Beginners beginners@perl.org Sent: Wednesday, January 24, 2007 10:57:51 AM Subject: Re: trouble with list context assignment for substitution inside File::Find wanted function Yes, the substitution operator (s///) returns true (1) or false ('') in either list or scalar context. To do want you want you have to do the assignment first and then do the substitution: my $newname = $_; $newname =~ s/^\w+-//; Or in one statement: ( my $newname = $_ ) =~ s/^\w+-//; I've already figured that one out. However, I want to use variables for my regexp pattern. So I can replace axis with whatever I my first program argument is. Changed this:1 find (\renamefiles, './'); sub renamefiles{ if ($_ =~ /axis/){ my $oldname = $_; $_ =~ s/\w+-//; #rename ($oldname, $_) print $oldname will be renamed to $_\n; } } To this: find (\renamefiles, './'); my $name = shift; sub renamefiles{ if ($_ =~ /$name/){ my $oldname = $_; $_ =~ s/\w+-//; #rename ($oldname, $_) print $oldname will be renamed to $_\n; } } And if I do a #perl rename.pl axis I got many of this: Use of uninitialized value in regexp compilation at test.pl line 11. Even if I specify axis in my $name instead of shift, I'm getting the same error What's wrong with using variables in regexp patterns?? John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ Yahoo! Music Unlimited Access over 1 million songs. http://music.yahoo.com/unlimited
Re: trouble with list context assignment for substitution inside File::Find wanted function
Do you have a perl one-liner to rename all files into their filenames with stripped ^\w+. No. Yes. /^\w+-/ and rename $_, $' for (glob *) -Jason -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/