in reply to deleting commas enclosed by quotation marks

Tokenizer approach:

$input = '7374,726327,"76,237",32324,"21,342,857",23'; print("input: $input\n"); $output = ''; foreach ($input) { if (/\G("[^"]+")/gc) { my $quoted = $1; $quoted =~ s/,//g; $output .= $quoted; redo; } # Unmatched quote. if (/\G"/gc) { $output .= '"'; redo; } if (/\G([^"]+)/gc) { $output .= $1; redo; } } print("output: $output\n");

To avoid reinventing the wheel, I do recommed Text::CSV_XS or similar over my solution. Use it to seperated the fields, then remove the comma from every field with something like s/,//g foreach @fields;.

Replies are listed 'Best First'.
Re^2: deleting commas enclosed by quotation marks
by ambrus (Abbot) on Jun 22, 2005 at 08:56 UTC

    foreach sounds funny here.

      It's using the fact that it aliases $_ so that you don't have to explicitly bind to $input each time. It's iterating over a list, it just happens to only have one element.

      Of course some wonks might say that in addition to the spelled-for-pronounced-foreach and vice versa you've got this spelled-foreach-pronounced-with . . . :)

      --
      We're looking for people in ATL