>>>>> "NGW" == Noah Garrett Wallach <noah-l...@enabled.com> writes:

  NGW> Jim Gibson wrote:
  >> At 7:22 PM -0700 9/2/09, Noah Garrett Wallach wrote:
  >>> Hi there,
  >>> 
  >>> what is the way to collapse this search/replace to one line?
  >>> 
  >>> my $filename_cmd = $cmd[-1];
  >>> my $filename_cmd =~ s/\s/\./;
  >> 
  >> 
  >> (my $filename_cmd = $cmd[-1]) =~ s/\s/\./;

  NGW> okay a step further - is there a way to make the following a one liner?

  NGW>     (my $filename_cmd = $cmd[-1]) =~ s/\|//g;

deleting chars is faster and better done with tr:

        tr/|//d ;

also no need to escape the | since it is not a metachar in tr///.

  NGW>     $filename_cmd =~ s/\s+/\./g;

no need for the \ before the . since . is just text in the replacement
string (which is usually just a double quoted string, NEVER a regex).

  NGW>     $filename_cmd =~ s/save.*//g;

no. you can't (easily) do multiple s/// ops on one thing without major
contortions. you could merge the first and third ones since they both
substitute the null string globally. but it would look pretty ugly:

(my $filename_cmd = $cmd[-1]) =~ s/\||save.*//g;

that is not good coding style.


and why do you want that? squeezing code into one liners can be neat but
forcing it is not good coding.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to