Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!elroy.jpl.nasa.gov!decwrl!mcnc!rti!mozart!sherman From: sherman@unx.sas.com (Chris Sherman) Newsgroups: comp.lang.perl Subject: Re: pattern matching question Message-ID: Date: 24 May 91 03:50:31 GMT References: <1991May22.193037.12166@cherokee.uswest.com> <1991May22.221507.12660@cherokee.uswest.com> <49420@ut-emx.uucp> Sender: news@unx.sas.com (Noter of Newsworthy Events) Organization: SAS Institute Inc. Lines: 74 Nntp-Posting-Host: foster.unx.sas.com In <49420@ut-emx.uucp> dboles@ccwf.cc.utexas.edu (David Boles) writes: >Warning: PERL NOVICE approaching !!! Warning: PERL NOVICE answering!!! >I am trying to do the following file manipulation: >p220 p220 20 5 >p220 p235 20 7 >etc. > ==> >p220 -20 -5 >p220 20 5 >p220 -20 -7 >p235 20 7 >etc. >I am using: >while (<>) { > s/(p\d*) (p\d*) (\d*) (\d*)/$1 -$3 -$4\n$2 $3 $4\n/; > print; >} >and I get: >p220 - - >p220 20 5 >Why aren't $3 and $4 "alive" in the first half of the replacement >string? What am I missing? I think I got it. Perl is taking your test string literally, space for space. The synchronization is lost. I used the following input file with your code: p220 p220 20 5 p220 p235 20 7 and got: p220 -20 -5 p220 20 5 p220 -20 -7 p235 20 7 So then I tried the following code: #!/usr/local/bin/perl while (<>) { s/(p\d*) *(p\d*) *(\d*) *(\d*)/$1 -$3 -$4\n$2 $3 $4\n/; print; } With the following input: p220 p220 20 5 p220 p235 20 7 and got p220 -20 -5 p220 20 5 p220 -20 -7 p235 20 7 Maybe perl pro's can tell me what the '*'s meant exactly, why they are working, and if they would work in every case. (I have my ideas, but they are probably wrong, and I just got lucky. I was hoping to set up a one-or-more-number-of spaces type thing, but I don't think I did that right). -- Chris Sherman .................... sherman@unx.sas.com | ,-----------------------------------------' / Q: How many IBM CPU's does it take to execute a job? | A: Four; three to hold it down, and one to rip its head off.