Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Perl is returning... odd results... from regular expressions. Things matching when they shouldn't, and stuff like that.

by Melly (Hermit)
on Jan 11, 2007 at 10:27 UTC ( #594097=note: print w/ replies, xml ) Need Help??


in reply to Perl is returning... odd results... from regular expressions. Things matching when they shouldn't, and stuff like that.

Well, if you don't want to use a module, you could try something like the following code - it basically breaks the job down into several parts. The only major requirement is that all your quotes should be valid pairs (you should probably add a test to check that you have an even number of quotes and that you have the number of fields per line that you expect).

  1. Pull out the quoted sections
  2. Replace ',' with '_comma_' in the quoted sections
  3. Restore the quoted sections back into position
  4. Safely split on ',' (since quoted commas are now '_comma_')
  5. Replace '_comma_' with ','

Here's the code:

use strict; my @output; while(<DATA>){ chomp; next unless $_ =~ /\S/; # push any quoted stuff (incl. quotes) onto array... (we assume that + all quotes are paired) push my @quoted, ($_ =~ /"([^"]*)"/g); # replace any commas in the array with '_comma_' foreach my $quote(@quoted){ $quote =~ s/,/_comma_/g; } # now replace the ',' versions with the "_comma_" versions $_ =~ s/"[^"]*"/'"' . (shift @quoted) . '"'/ge; # now we can safely split on any commas (quoted commas are now '_com +ma') push @output, [split /,/]; # finally, replace any '_comma_' values with ',' in the latest eleme +nt of output foreach(@{$output[$#output]}){ s/_comma_/,/g; } } # what have we got? foreach(@output){ foreach(@{$_}){ print "$_:"; } print "\n"; } __DATA__ 123,456,"hello, world, goodbye, world",789 123,456,"hello, world, goodbye, world",789,"foo, bar","bar, foo" "hello, world","goodbye, world",123,"foo" "hello" 123,456,"goodbye, world",789
map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
Tom Melly, pm@tomandlu.co.uk


Comment on Re: Perl is returning... odd results... from regular expressions. Things matching when they shouldn't, and stuff like that.
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://594097]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-07-28 23:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (210 votes), past polls