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

4215Re: [PBML] Simple split() question

Expand Messages
  • James Kralec
    Jul 1, 2001
    • 0 Attachment
      Try this. It has to be changed a little if you expect more than two
      "strings" between the parenthesis, but it should get you started. Of course,
      there may well be a better way to go at this, but this works.
      Mike

      #!/perl/bin/perl.exe

      open(TF, "test.txt") or die "Couldn't open test.txt";

      while ($line = <TF>) {
      $num = 1;
      foreach (split " ", $line) {
      $par = "par" . $num;
      if (m/(\"[\d\w]+)/g) {
      $quoted = $1;
      } elsif (m/([\d\w]+\")/g) {
      $$par = $quoted . " " . $1;
      $num++;
      } else {
      $$par = $_;
      $num++;
      }
      }
      print "$par1, $par2, $par3, $par4, $par5\n";
      }

      close TF;

      exit;

      This prints: xyz, abc, 12sd, "pqr stz", dfg

      sbasak@... wrote:

      > Hi,
      >
      > I want to perform a simple split operation, but can't get the regular
      > expr working. Can anybody help me on this?
      >
      > 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
      >
      > Thanks in advance.
      > -Basak
      >
      >
      >
      > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    • Show all 7 messages in this topic