Loading ...
Sorry, an error occurred while loading the content.

Re: Simple split() question

Expand Messages
  • James E Keenan
    ... my $line from a file read is: xyz abc 12sd pqr stz dfg (delimited by blank char). I m doing my ($par1, $par2, $par3, $par4, $par5) = split(/ /, $line);
    Message 1 of 3 , Jul 1, 2001
      On Sat, 30 Jun 2001 sbasak@... wrote:
      my $line from a file read is:
      xyz abc 12sd "pqr stz" dfg (delimited by blank char).
      I'm doing
      my ($par1, $par2, $par3, $par4, $par5) = split(/ /, $line);
      and I'm getting
      $par4 = "pqr
      $par5 = stz", which I don't want.

      I want $par4 = "pqr stz", & $par5 = dfg

      Assuming the character '_' does not occur within a string such as "pqr stz",
      why not substitute '_' for ' ' first, do the split, then switch back to ' '?

      my $line = '';
      my @pars = ();

      $line = 'xyz abc 12sd "pqr stz" dfg kfc non "test test1"';
      $line =~ s|"(\w+) (\w+)"|"$1_$2"|g;

      @pars = split(/ /, $line);
      foreach (@pars) {
      $_ =~ s|"(\w+)_(\w+)"|"$1 $2"|g;
      print "$_\n";

      Jim Keenan
      Brooklyn, NY
    Your message has been successfully submitted and would be delivered to recipients shortly.