in reply to Improved regexp sought

Can anybody suggest a change to the second line, and take me further along the path?
Sure, change the second line to:
my @fields = qw(0010 2 O'Reilly);
But if you want more help, you'll need to help us by describing what you want in words, not just have us second-guess your regex.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: Improved regexp sought
by myomancer (Novice) on Oct 27, 2004 at 14:32 UTC

    Point taken merlyn.

    I have a file with multiple lines in. Each line consists of a variable number of variable-length fields separated by a + character. Each line is terminated by a ' character. Sometimes, a field might have a ' character in it - if so, the ' is preceeded by a question-mark. Here are some example lines:

    0010+2+O'Reilly' 023++++234+35+White+++17+' g?'day mate+++'
    I want to break each line up into its constituent fields. I can do it with brute force, but would prefer elegance.

    Thanks
    Myomancer

      Mayhap you want to take a multi-step approach.

      $string =~ s/'$//; $string =~ s/\?'/'/g; @fields = split /\+/, $string;
      I want to break each line up into its constituent fields. I can do it with brute force, but would prefer elegance.

      I usually choose "working" over "not working" :-)

      use Text::CSV_XS. I guessed that your lines were terminated with apostrophe + newline. Alter the code to fit.

      use Text::CSV_XS; my $parser = Text::CSV_XS->new( { eol => "'\n", escape_char => "'", sep_char => "+" } ); while ( my $line = <$fh> ) { $parser->parse( $line ); print join( ", ", $parser->fields ) . "\n"; }
      myomancer,
      I can do it with brute force, but would prefer elegance

      I hope you aren't confusing conciseness with elegance. There are not always related. See the following:

      my $str = "0010+2+O?'Reilly'"; my @field = map {s/\?'/'/g; $_ } split /\+/ , substr($str,0, (length $ +str) - 1); print "[$_]$field[$_]\n" for 0 .. $#field;
      IMO, the code would be more elegant broken out into multiple lines - perhaps with comments.

      Cheers - L~R