For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2007 > Re: trouble with list context assignment for substitution inside









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: trouble with list context assignment for substitution inside
John W. Krahn

2007-01-23, 10:00 pm

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com