I dedicate this to those who said it couldn't be done (if anyone said that -- I don't know). These were tested with Perl 5.9.0, but I think they should work with 5.8.0 too.
$str = q{split on whitespace "but leave" quoted "spaces alone"}; @chunks = get_chunks($str); # desired return value: # ('split', 'on', 'whitespace', 'but leave', 'quoted', 'spaces alone') # it's hard to do it with split(), but it CAN be done! # method 1 # note: this leaves the "s in... sub get_chunks { my$s;split/"(?{$s=!$s})(?!)|\s+(?(?{$s})(?!))/,pop } # method 2 # note: this removes the "s (preferred) sub get_chunks { my$s;@_=split/(?(?{$s})"\s*(?{$s=0})|(?:\s*"(?{$s=1})|\s+))/,' '.pop;s +hift;@_ }

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Two Eclectic split()s
by jryan (Vicar) on May 21, 2003 at 07:57 UTC

    Here's another one:

    use Data::Dumper; my $string = '"quoted" foo foo "joe smoe" bar baz quux "trapped inside + here" matrix2:reloaded sucked'; print Dumper [split /\s+("(?:[^"\\]|\\")*"|[^\s]+)(?(?!\s+")\s+)/, $st +ring];
Re: Two Eclectic split()s (another one maybe?)
by Enlil (Parson) on May 21, 2003 at 09:15 UTC
    use Data::Dumper; my $string = '"I think" this fits "the \\"requirements presented\\" th +en" again \\"I could "be wrong"'; print Dumper [split /\s+(?=(?:(?:(?:[^"\\]|\\")*"){2})*(?:\\"|[^"])*$) +/, $string];
    On a side note Method 1 returns the following (on AS 5.8): update:Looks a little nicer if I don't compensate for escaped quotes:
    split /\s+(?=(?:(?:[^"]*"){2})*[^"]*$)/,$string

    -enlil

      Hehe, then there was some split/regex bug that was fixed. The weird 1's are related to the flip-flop variable I've got in my code. Oh well.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Two Eclectic split()s
by Aristotle (Chancellor) on May 21, 2003 at 15:40 UTC
    I'm not sure exactly what is supposed to be impossible here; have you seen my pure-regex solution?

    Makeshifts last the longest.

      What's hard is using split() to get it done. I can modify it a bit to allow for escaped quotes and single quotes and what-not. The point is, split() is not the ideal way to do this, but I wanted to get it done. I make a habit of stress-testing the worst tool for the job. ;)

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

5.9.0?
by toma (Vicar) on May 26, 2003 at 23:02 UTC
    I didn't know that 5.9.0 was out yet. I'm aware that there is a 5.9 development branch, but is there a 5.9.0 release? I don't see it on cpan.

    It should work perfectly the first time! - toma

      There isn't, but bleadperl has been calling itself 5.9.0 for a while now. I think it's time (as in "quite some time has passed since the 5.9.x development track has started", not in "there has been done a lot of new exciting things the last year") we saw a 5.9.0 release. But I doubt that's going to happen anytime soon.

      Abigail