Path: utzoo!attcan!uunet!mcsun!ukc!axion!tharr!gustav From: gustav@tharr.UUCP (Paul Moore) Newsgroups: comp.lang.perl Subject: Re: Need help with (\d+) problem (s/...(\d+)/$1/ not working?) Message-ID: <1390@tharr.UUCP> Date: 5 Nov 90 22:50:31 GMT References: <1990Nov4.224658.21882@uvaarpa.Virginia.EDU> Reply-To: gustav@tharr.UUCP (Paul Moore) Organization: Power Microsystems Ltd Lines: 42 In article <1990Nov4.224658.21882@uvaarpa.Virginia.EDU> telxon!ping!gorpong@uunet.uu.net writes: >Something is a little strange either with the way that I am interpreting the >manual page, or with Perl. The mini-script: > > $string = "this is a test of stuff\t95"; > ($a = $string) =~ s/\w+\s+(\d+)/$1/; > print "1 == $1\nA == $a\n+ == $+\nbefore == $`\nafter == $'\n"; > >Prints out: > > 1 == 95 > A == this is a test of 95 > + == 95 > before == this is a test of > after == This is correct. The substitution is editing $a, not replacing it. The expression on the second line works like this. First, you assign $string to $a, setting $a to "this is a test of stuff\t95". This assignment returns an LVALUE, $a. The =~ operator then applies the following substitution to this value. s/\w+\s+(\d+)/$1/ looks through the string for a word (\w+) followed by whitespace (\s+) followed by a number (\d+), finding "stuff\t95" in this case. It then replaces this part of the string with the number ($1), but *leaving the rest of the string as it was*. So, the result is "this is a test of 95", as shown. If you really do want $a to equal "95", you could just say $a = $1 at the end ($1 keeps its value outside the replacement pattern). I'm sure there are better ways, as well. (but I'm too tired to think of one just now...) Hope this helps, Gustav. -- /----------------------------------------------------------------\ | Paul Moore E-Mail: ...!ukc!tharr!gustav | | 10, Mulberry Rise or ...!ukc!cix!pmoore | | Northwich, Cheshire or (BEST) pmoore@cix.compulink.co.uk | <-- tharr *free* public access to Usenet in the UK 0234 261804 -->