in reply to In place replace, ignoring between quotes

Brute force...
use warnings; use strict; my $str = q(cd / ; /path/to/R/R_latest --vanilla --args "fName='rGSDPl +an';jobCode=682718;jobId=6827181;" < job682718.R > job_6827181.txt); my $new; my $out = 1; for (split //, $str) { $out = ! $out if /"/; $new .= (/;/ and $out) ? '&&' : $_; }

Replies are listed 'Best First'.
Re^2: In place replace, ignoring between quotes
by trippledubs (Deacon) on Oct 25, 2013 at 19:09 UTC
    This is neat. This is an implementation of a state machine, right? In the debugger it looks like $out just undefines and redefines itself for every double quote? I don't understand how you can just say, equal not yourself. How does that work?
      Yes, this is a state machine. I spend a lot of time coding in Verilog, where toggling a bit is a natural part of the language. I guess this would be cleaner in Perl:
      $out = $out ? 0 : 1 if $_ eq '"';

      I do not know why Perl sets $out to undef.

      If $var is true, then !$var is false ('').
      If $var is false, then !$var is true (1).

      That new value is then assigned to $var

      Alternatively, you could say $var ^= 1 to do the same sort of thing, just with 1 and 0 instead of 1 and ''.

        Nice. ^=1 makes more sense to me.

        I guess I have a hard time understanding because if $var = 0 (or "") then you say $var = ! $var, why 1? Why not, 50? I mean practically speaking it works and that is what is important. I guess it doesn't have to exactly translate to English. Thanks for sharing!