Re: [mail_lists] Re: regex $1 not updating?
On Friday 04 April 2003 14:34, John W. Krahn wrote: | Jim wrote: | I've never encountered this before but I have to be doing something | wrong. | | Yes, you are. This is the documented behaviour of the numeric variables | and is why we always tell beginners to use them only if the regular | expression matched. Ok. I didn't put tests in my example code. However, I do have tests in my actual code which is what led me to this whole fiasco. In this code I am finding that the first test ($back) pasts...and rightly so. It should. However, in the following regex ($line) it should fail sometimes...but it doesn't. It doesn't because when $back returns $1 as a valid return from the regex then $1 remains a valid rval for the condition for $line. I need to get around that! Here is my code: 76 for $back ( @data ) 77 { 78 $go_on = 1; 79 80 # start infinite loop 81 82 while ( $go_on ) 83 { 84 85 # if we see that there is a net_blk 86 87 $back =~ /.*\(.*\) (.*?) .*/; 88 89 if ( $1 ) 90 { 91 92 # add it to the array 93 94 push(@ret,$1); 95 96 # walk it 97 push(@done,$1); 98 my @more = get_whois($1); 99 100 # repeat process 101 for my $line ( @more ) 102 { 103 $line =~ /.* \(.*\) (.*?) .*/; 104 105 if ( $1 ) 106 { 107 print Pushing net_blk: $1\n; 108 push(@ret,$1); 109 } 110 else 111 { 112 print Go on to next iteration.\n; 113 $go_on = 0; 114 } 115 116 print Pushing $1 to [EMAIL PROTECTED] stack\n; 117 last if ( grep($1,@done) ); 118 } 119 } 120 else 121 { 122 $go_on = 0; 123 } 124 } 125 } | | snippet of code: | | ]$ perl -e ' | | $var = Company Online (Company Systems) NETBLK-COM-5BLK | | (NET-24-256-0-0-1); | | $var =~ /.*? \(.*\) (.*?) \(.*?\)/; | print $1,\n; | | print $1\n if $var =~ /.*? \(.*\) (.*?) \(.*?\)/; | | $var = NetBlock: NETBLK-10H-6BLK; | $var =~ /s\(.*?\) (.*?) \(.*?\)/; | print $1,\n; | | print $1\n if $var =~ /s\(.*?\) (.*?) \(.*?\)/; | | ' | | NETBLK-COM-5BLK | NETBLK-COM-5BLK | | Why isn't $1 getting updated with the next implicit match? It should | fail but its returning the first $1 match. I can't unset $1 because it | is a read-only variable. This doesn't even work if I change the second | $var to $var2 because of course $1 is the same the way through. | | $1, $2, $3, etc. are only set on a successful match otherwise they | retain the value from the previous successful match. | | | John -- - Jim -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [mail_lists] Re: regex $1 not updating?
Go back and re-read John's message. Then look at your conditionals. They are different. His work, yours don't. Yours: 87 $back =~ /.*\(.*\) (.*?) .*/; 88 89 if ( $1 ) 103 $line =~ /.* \(.*\) (.*?) .*/; 104 105 if ( $1 ) His: | print $1\n if $var =~ /.*? \(.*\) (.*?) \(.*?\)/; | print $1\n if $var =~ /s\(.*?\) (.*?) \(.*?\)/; You aren't checking to see if the REGULAR EXPRESSION SUCCEEDED. If the regular expression FAILS, then $1 retains the old value, it isn't reset. /\/\ark -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [mail_lists] Re: regex $1 not updating?
On Friday 04 April 2003 14:54, Bob Showalter wrote: Wow! I've coding in Perl for almost three years now and I didn't this. Anyhoo, Bob's advice was the what I used and it works now. I just moved the regex evals into the if condition statements vs the way I was doing it. Thanks for the assistance guys. - Jim | Jim wrote: | I've never encountered this before but I have to be doing something | wrong. | | snippet of code: | | ]$ perl -e ' | | $var = Company Online (Company Systems) NETBLK-COM-5BLK | (NET-24-256-0-0-1); $var =~ /.*? \(.*\) (.*?) \(.*?\)/; | print $1,\n; | | $var = NetBlock: NETBLK-10H-6BLK; | $var =~ /s\(.*?\) (.*?) \(.*?\)/; | print $1,\n; | ' | | NETBLK-COM-5BLK | NETBLK-COM-5BLK | | | Why isn't $1 getting updated with the next implicit match? It should | fail but its returning the first $1 match. I can't unset $1 because | it is a read-only variable. This doesn't even work if I change the | second $var to $var2 because of course $1 is the same the way through. | | VERY frustrating. | | This is the way the $1, $2, etc. variables work. They are only set if there | is a successful match. Otherwise, they simply retain whatever previous | value they had. | | You either need to test that the match worked: | |if (/(\w+)/) { | # $1 is now set | print $1; |} | | Or, evaluate the match in list context: | |my ($word) = /(\w+)/; # $word will be undef if no match -- - Jim -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [mail_lists] Re: regex $1 not updating?
Hi Jim. I think yu've got a bit lost in your nexted loops. I've tried to understand what your code does and is meant to do and have written my interpretation below. There are a few things that I can't fathom though. Jim wrote: On Friday 04 April 2003 14:34, John W. Krahn wrote: Jim wrote: I've never encountered this before but I have to be doing something wrong. Yes, you are. This is the documented behaviour of the numeric variables and is why we always tell beginners to use them only if the regular expression matched. Ok. I didn't put tests in my example code. However, I do have tests in my actual code which is what led me to this whole fiasco. In this code I am finding that the first test ($back) pasts...and rightly so. It should. However, in the following regex ($line) it should fail sometimes...but it doesn't. It doesn't because when $back returns $1 as a valid return from the regex then $1 remains a valid rval for the condition for $line. I need to get around that! Here is my code: 76 for $back ( @data ) 77 { 78 $go_on = 1; 79 80 # start infinite loop 81 82 while ( $go_on ) 83 { 84 85 # if we see that there is a net_blk 86 87 $back =~ /.*\(.*\) (.*?) .*/; 88 89 if ( $1 ) 90 { 91 92 # add it to the array 93 94 push(@ret,$1); 95 96 # walk it 97 push(@done,$1); 98 my @more = get_whois($1); 99 100 # repeat process 101 for my $line ( @more ) 102 { 103 $line =~ /.* \(.*\) (.*?) .*/; 104 105 if ( $1 ) 106 { 107 print Pushing net_blk: $1\n; 108 push(@ret,$1); 109 } 110 else 111 { 112 print Go on to next iteration.\n; 113 $go_on = 0; 114 } 115 116 print Pushing $1 to [EMAIL PROTECTED] stack\n; 117 last if ( grep($1,@done) ); 118 } 119 } 120 else 121 { 122 $go_on = 0; 123 } 124 } 125 } Take a look at this, which has something like your semantics: foreach my $back ( @data ) { next unless ( $back =~ /\)\s+(.+)\s+\(/ ) { push @ret, $1; push @done, $1; my @more = get_whois($1); foreach my $line ( @more ) { if ( $line =~ /\)\s+(.+)\s+\(/ ) { print Pushing net_blk: $1\n; push @ret, $1; } } print Pushing $1 to [EMAIL PROTECTED] stack\n; last if ( grep($1,@done) ); } The stack pushes are done in the equivalent place, but I think they're not quite right; in particular the push to @ret is happening teice. I could make neither head nor tail of your grep() call: all I know is that it's wrong! I hope this is a better starting point for you. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [mail_lists] Re: regex $1 not updating?
R. Joseph Newton wrote: from your original post raised a red flag for me: $var =~ /.*? \(.*\) (.*?) \(.*?\)/; I saw it, and wondered Well, what is he doing with it?. The matching function is intended to return a true value on success, and that value is assigned to your blandly-named $var, yet it is never used. No, that value is NOT assigned to $var. The expression is in a void context so the result of the expression is not assigned to anything. You need to use the assignment operator '=' if you want to assign the result of the expression to some variable. Try instead: if ($var =~ /.*? \(.*\) (.*?) \(.*?\)/;) ) { ^ That semicolon is a syntax error. It won't compile. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [mail_lists] Re: regex $1 not updating?
John W. Krahn wrote: R. Joseph Newton wrote: from your original post raised a red flag for me: $var =~ /.*? \(.*\) (.*?) \(.*?\)/; I saw it, and wondered Well, what is he doing with it?. The matching function is intended to return a true value on success, and that value is assigned to your blandly-named $var, yet it is never used. No, that value is NOT assigned to $var. The expression is in a void context so the result of the expression is not assigned to anything. You need to use the assignment operator '=' if you want to assign the result of the expression to some variable. Try instead: if ($var =~ /.*? \(.*\) (.*?) \(.*?\)/;) ) { ^ That semicolon is a syntax error. It won't compile. Thanks John, I noticed that on first scan, hence the red flag, and then I misread it while pasting it in. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]