Re: Next subnet
On 26/05/2013 14:40, shawn wilson wrote: Thank y'all, I got to where I want to be: https://github.com/ag4ve/geocidr ... or grep { ! m%[0-9\.\/]+% } @{$opts-{ip}} or scalar(@{$opts-{ip}}) 1 The '+' in the regexp is superfluous as-is. (your regexp isn't anchored) You probably meant it more like: ... or !@{$opts-{ip}} or grep m{[^0-9./]}, @{$opts-{ip}} -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Next subnet
On May 27, 2013 1:02 PM, Dr.Ruud rvtol+use...@isolution.nl wrote: On 26/05/2013 14:40, shawn wilson wrote: Thank y'all, I got to where I want to be: https://github.com/ag4ve/geocidr ... or grep { ! m%[0-9\.\/]+% } @{$opts-{ip}} or scalar(@{$opts-{ip}}) 1 The '+' in the regexp is superfluous as-is. (your regexp isn't anchored) You probably meant it more like: ... or !@{$opts-{ip}} or grep m{[^0-9./]}, @{$opts-{ip}} You don't want to grep for anything that isn't a number there. I like the !@arr vs my scalar though. And... You're right on anchoring - I should. But as I'm lastly matching for an IP (with a possible subnet, it should probably be more like: m(^[0-9\./]+$) Or better: m(^[0-9\.]+(?:/(?:[0-3])?[0-9])?$) Or more better: m(^(?:(?:[0-2])?[0-9]{1,2}\.){3}(?:[0-2])?[0-9]{1,2}(?:/(?:[0-3])?[0-9])?$) I know there are some edge cases like 256 octets and 32 bit subnets but that's my 'good enough' IP matching regex (written from a phone, in bed because I'm too lazy to get up and masochistic enough to do it so I hope I didn't error any). I'll make this part better and get ip6 in here.
Re: Next subnet
On 27/05/2013 23:55, shawn wilson wrote: On May 27, 2013 1:02 PM, Dr.Ruud rvtol+use...@isolution.nl mailto:rvtol%2buse...@isolution.nl wrote: On 26/05/2013 14:40, shawn wilson wrote: Thank y'all, I got to where I want to be: https://github.com/ag4ve/geocidr ... or grep { ! m%[0-9\.\/]+% } @{$opts-{ip}} or scalar(@{$opts-{ip}}) 1 The '+' in the regexp is superfluous as-is. (your regexp isn't anchored) You probably meant it more like: ... or !@{$opts-{ip}} or grep m{[^0-9./]}, @{$opts-{ip}} You don't want to grep for anything that isn't a number there. Who is this 'You'? You clearly misunderstand m{[^0-9./]}. I like the !@arr vs my scalar though. And... You're right on anchoring - I should. But as I'm lastly matching for an IP (with a possible subnet, it should probably be more like: m(^[0-9\./]+$) Or better: m(^[0-9\.]+(?:/(?:[0-3])?[0-9])?$) Or more better: m(^(?:(?:[0-2])?[0-9]{1,2}\.){3}(?:[0-2])?[0-9]{1,2}(?:/(?:[0-3])?[0-9])?$) There are many much clearer ways to do that, And be aware of octalness. I know there are some edge cases like 256 octets and 32 bit subnets but that's my 'good enough' IP matching regex (written from a phone, in bed because I'm too lazy to get up and masochistic enough to do it so I hope I didn't error any). I'll make this part better and get ip6 in here. Just be properly lazy, and check Regexp::Common, Regexp::IPv6. -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Next subnet
On Fri, May 24, 2013 at 03:18:35PM -0400, shawn wilson wrote: How do I find the next subnet? This should print 192.168.1.0 the second time - it errors: [code deleted] Why should it? The Net::IP documentation doesn't provide any information about actions that cross the subnet boundry. Having said that, it seems it doesn't allow that operation. And in fact, many of the methods don't work after incrementing, which seems wrong to me: #!/usr/bin/perl use strict; use warnings; use Net::IP; my @method_types = qw ( ip short binip intip mask last_ip prefixlen size iptype reverse_ip ); my $ip = Net::IP-new('192.168.0.0/24');# three lines from your code print Start ip [ . $ip-ip . ]\n; print start mask [ . $ip-prefixlen . ]\n; $ip++; print After incrementing by 1\n; show_methods($ip); $ip-set($ip-last_ip) ; $ip++ ; print \nAfter incrementing past last_ip\n; show_methods($ip); sub show_methods { my ($ip) = @_; print now at . $ip-ip ,$/; for my $type ( @method_types) { if( $ip-$type ) { print $type : , $ip-$type(), $/; } else { print no more $type\n; } } } __END__ michael@bivy:~/rmme$ ./tpl Start ip [192.168.0.0] start mask [24] After incrementing by 1 now at 192.168.0.1 ip : 192.168.0.1 short : 192 binip : 110010101001 intip : 3232235521 no more mask last_ip : 192.168.0.255 no more prefixlen size : 255 iptype : PRIVATE no more reverse_ip After incrementing past last_ip Can't call method ip on an undefined value at ./tpl line 29. michael@bivy:~/rmme$ -- Michael Rasmussen, Portland Oregon Be Appropriate Follow Your Curiosity Other Adventures: http://www.jamhome.us/ or http://gplus.to/MichaelRpdx A special random fortune cookie fortune: Only the mediocre are always at their best. ~ Jean Giraudoux, French Novelist (rephrased as Only the mediorcre are at their best all the time. ~ G.M. Ford in Who the hell is Wanda Fuca?) -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Next subnet
Thank y'all, I got to where I want to be: https://github.com/ag4ve/geocidr On Sun, May 26, 2013 at 8:06 AM, Michael Rasmussen mich...@jamhome.us wrote: On Fri, May 24, 2013 at 03:18:35PM -0400, shawn wilson wrote: How do I find the next subnet? This should print 192.168.1.0 the second time - it errors: [code deleted] Why should it? The Net::IP documentation doesn't provide any information about actions that cross the subnet boundry. Having said that, it seems it doesn't allow that operation. And in fact, many of the methods don't work after incrementing, which seems wrong to me: #!/usr/bin/perl use strict; use warnings; use Net::IP; my @method_types = qw ( ip short binip intip mask last_ip prefixlen size iptype reverse_ip ); my $ip = Net::IP-new('192.168.0.0/24');# three lines from your code print Start ip [ . $ip-ip . ]\n; print start mask [ . $ip-prefixlen . ]\n; $ip++; print After incrementing by 1\n; show_methods($ip); $ip-set($ip-last_ip) ; $ip++ ; print \nAfter incrementing past last_ip\n; show_methods($ip); sub show_methods { my ($ip) = @_; print now at . $ip-ip ,$/; for my $type ( @method_types) { if( $ip-$type ) { print $type : , $ip-$type(), $/; } else { print no more $type\n; } } } __END__ michael@bivy:~/rmme$ ./tpl Start ip [192.168.0.0] start mask [24] After incrementing by 1 now at 192.168.0.1 ip : 192.168.0.1 short : 192 binip : 110010101001 intip : 3232235521 no more mask last_ip : 192.168.0.255 no more prefixlen size : 255 iptype : PRIVATE no more reverse_ip After incrementing past last_ip Can't call method ip on an undefined value at ./tpl line 29. michael@bivy:~/rmme$ -- Michael Rasmussen, Portland Oregon Be Appropriate Follow Your Curiosity Other Adventures: http://www.jamhome.us/ or http://gplus.to/MichaelRpdx A special random fortune cookie fortune: Only the mediocre are always at their best. ~ Jean Giraudoux, French Novelist (rephrased as Only the mediorcre are at their best all the time. ~ G.M. Ford in Who the hell is Wanda Fuca?) -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Next subnet
On 24/05/2013 21:18, shawn wilson wrote: How do I find the next subnet? This should print 192.168.1.0 the second time - it errors: #!/usr/bin/env perl use strict; use warnings; use Net::IP; my $ip = Net::IP-new('192.168.0.0/24'); print Start ip [ . $ip-ip . ]\n; print start mask [ . $ip-prefixlen . ]\n; $ip-set($ip-last_ip); $ip++; $ip-set($ip-ip . / . $ip-prefixlen); print Start ip [ . $ip-ip . ]\n; print start mask [ . $ip-prefixlen . ]\n; Or without Net::IP: perl -Mstrict -wle' my $subnet = $ARGV[0]; my ($ip, $bits) = $subnet =~ m{(\S+)/(\S+)}; # parse my $ip_int = unpack N, pack , split /\./, $ip; my $step = 1 (32 - $bits); #$ip_int = (0x ^ ($step - 1)); # normalize $ip_int += $step; # increment my $next_ip = sprintf %vd, pack N, $ip_int; print $next_ip/$bits; ' 192.168.0.42/24 192.168.1.42/24 (you can activate the normalization if wanted) -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: next in foreach loop
2010/8/13 Kryten kryte...@googlemail.com: Hi, Complete newbie. Is there any way to use next from within a foreach loop? Sure. $ perl -le ' for (1..10) { next if $_ == 5; print; } ' 1 2 3 4 6 7 8 9 10 -- Jeff Pang http://home.arcor.de/pangj/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: next in foreach loop
On Aug 12, 2010, at 19:08, Kryten kryte...@googlemail.com wrote: Hi, Complete newbie. Is there any way to use next from within a foreach loop? All the examples I have seen/read use a while loop to demo. Yes, next will work on for/foreach, while, and until loops. So you can say for my $num (1 .. 10) { next if $num % 2; print $num\n; } To print the even numbers between 1 and 10 (inclusive). The next, last, and redo loop control statements will even work on bare blocks: my $i = 0; { print infinite loop: , $i++; sleep 1; redo; } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Next
Geetha Weerasooriya wrote: Dear all, When I was reading a Perl code I found the following line. Can u please explain what it means? !defined($rt_nearest) or $dh$dist or next; Hi Geetha Oh dear, it's not very readable is it! I assume you know what 'next' does. If not, look at perldoc -f next. The statement uses what is called the 'short-circuit' behaviour of the or operator. perldoc perlop says this: :: Binary || performs a short-circuit logical OR operation. That is, :: if the left operand is true, the right operand is not even evaluated. :: Scalar or list context propagates down to the right operand if it is :: evaluated. So if $rt_nearest is defined then nothing else happens. If it is undefined but $dh $dist then again nothing happens. If both are false then the next is executed to go to the next iteration of the loop. It's the same as: next unless !defined($rt_nearest) or $dh$dist; or, more clearly next if defined($rt_nearest) and $dist $dh; which is how it should have been written. It's now clear that the loop is looking for the 'nearest' of a list of objects. If an object has already been found ($rt_nearest is defined) and its distance is less than that of the current object ($dist $dh) then go to the next in the list. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Next
basically code done by a hacker, not a software developer if I'm correct this shall mine if (not( ! defined($rt_nearest) or $dh$dist)) { next; } On 7/6/06, Geetha Weerasooriya [EMAIL PROTECTED] wrote: Dear all, When I was reading a Perl code I found the following line. Can u please explain what it means? !defined($rt_nearest) or $dh$dist or next; Kind regards, Geetha -- Marcos Rebelo http://oleber.awardspace.com/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Next
On Jul 6, 2006, at 6:26, Geetha Weerasooriya wrote: Dear all, When I was reading a Perl code I found the following line. Can u please explain what it means? !defined($rt_nearest) or $dh$dist or next; It means next unless !defined($rt_nearest) or $dh $dist; or, equivalently, next if defined($rt_nearest) and $dh = $dist; The trick in the original code is: the or operator returns as soon as some of the operands evaluates to true, if any. So if !defined($rt_nearest) holds nothing to its right is evaluated. Otherwise we go for $dh $dist If it holds we are done, otherwise we evaluate the following operand next which has the secondary effect of jumping to the next iteration somewhere. Some people dislike using boolean operators to control flow that way, some people don't. -- fxn -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: next if question
On Apr 22, 2004, at 8:54 AM, rmck wrote: hi, I have a while statement that does a next if a match is made against a reg exprerssion of some numbers. data file: Header 10 20 5201 8001 0 80 3802 #!/bin/perl use strict; use warnings; while( ) { #read from stdin one line or record at a time. next if $_ =~ /(20|80|Header|)/; # =~ means match not equal to. print ; } results: bash-2.03$ ./clean.pl data.txt 10 0 bash-2.03$ I would like it to have the results as so: 10 5201 0 8001 3802 How do I force my reg exp to match only 20, and 80. Not 8001 or 5201? next if m/^(?:Header|[28]0)$/; Hope that helps. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: next if question
Both of these work great (thanks): next if $_ =~ /(^20$|^80$|^Header$)/; next if m/^(?:Header|[28]0)$/; So now my results are: bash-2.03$ ./clean.pl data.txt 10 5201 8001 0 3802 bash-2.03$ How can you have the parsed info printed? Can you still use the $_?? So the goal would be: bash-2.03$ ./clean.pl data.txt 10 5201 8001 0 3802 ##The Rest## Header 20 80 bash-2.03$ I thought I could do this: #!/bin/perl use strict; use warnings; while( ) { #read from stdin one line or record at a time next if $_ =~ /(^20$|^80$|^Header$)/; print ; if ($_ == /(^20$|^80$|^Header$)/){ $rest = $_; print ##The Rest##\n; print $rest; } } data.txt: Header 10 20 5201 8001 0 80 3802 BUT lol... Its not working: bash-2.03$ ./clean.pl data.txt 10 5201 8001 0 ##The Rest## 0 3802 bash-2.03$ Help if you can. Thanks up front Rob -Original Message- From: James Edward Gray II [EMAIL PROTECTED] Sent: Apr 22, 2004 6:59 AM To: rmck [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Subject: Re: next if question On Apr 22, 2004, at 8:54 AM, rmck wrote: hi, I have a while statement that does a next if a match is made against a reg exprerssion of some numbers. data file: Header 10 20 5201 8001 0 80 3802 #!/bin/perl use strict; use warnings; while( ) { #read from stdin one line or record at a time. next if $_ =~ /(20|80|Header|)/; # =~ means match not equal to. print ; } results: bash-2.03$ ./clean.pl data.txt 10 0 bash-2.03$ I would like it to have the results as so: 10 5201 0 8001 3802 How do I force my reg exp to match only 20, and 80. Not 8001 or 5201? next if m/^(?:Header|[28]0)$/; Hope that helps. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: next if question
On Apr 22, rmck said: bash-2.03$ ./clean.pl data.txt 10 5201 8001 0 3802 ##The Rest## Header 20 80 bash-2.03$ I thought I could do this: #!/bin/perl use strict; use warnings; while( ) { #read from stdin one line or record at a time next if $_ =~ /(^20$|^80$|^Header$)/; print ; if ($_ == /(^20$|^80$|^Header$)/){ You don't want to use == here, you want to use =~. $rest = $_; print ##The Rest##\n; print $rest; } } Here's how I'd do it: my @rest = (); while () { if (/^(20|80|Header)$/) { push @rest, $_ } else { print } } print ## The Rest ##\n; print @rest; -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ CPAN ID: PINYAN[Need a programmer? If you like my work, let me know.] stu what does y/// stand for? tenderpuss why, yansliterate of course. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: `next LABEL' usage
Harry Putnam [EMAIL PROTECTED] wrote: : : I'm using a next LABEL inside a File::Find : sub wanted {...} loop : : It is further buried in a while loop inside the : `sub wanted()' : : The while loop is while (FILE) on the most recent : found file. I want this `next LABEL' to bring on a : new file... not a new line in while loop. : : So using the `next LABEL' technique how do I : designate the wanted() subroutine as target? : Something like this: : : sub LABEL: wanted { I haven't tested it, but I would think this would fail. : open(FILE,File::Find::name); : while (FILE){ : if(something) { : then do something : }else{ :next LABEL; : } : } : } As I understand this, you want to immediately open a new file found in current file with the current while loop. Is that correct? First, we need to know a few things. Are you wanting to recursively call the entire wanted() subroutine? Or do you just want to call the while loop portion only and continue with the execution after? Is it alright to clobber the currently open file or do we need to continue processing it afterward? Is there any test needed to be certain we are not opening the same file again? HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: `next LABEL' usage
Charles K. Clarkson [EMAIL PROTECTED] writes: Harry Putnam [EMAIL PROTECTED] wrote: [...] Wants to exit a while loop inside a File::Find \wanted sub routine. Is exiting the while loop sufficient.. or does one need to exit from the current file being offered by `sub find()' : So using the `next LABEL' technique how do I : designate the wanted() subroutine as target? : Something like this: : : sub LABEL: wanted { I haven't tested it, but I would think this would fail. : open(FILE,File::Find::name); : while (FILE){ : if(something) { : then do something : }else{ :next LABEL; : } : } : } As I understand this, you want to immediately open a new file found in current file with the current while loop. Is that correct? Yes... First, we need to know a few things. Are you wanting to recursively call the entire wanted() subroutine? I guess not no. I just want to go on to the next file it has found Or do you just want to call the while loop portion only and continue with the execution after? Yes Is it alright to clobber the currently open file or do we need to continue processing it afterward? It needs to be clobbered. This file has been processed to a point where it has already failed tests that show if this file has what we want. Is there any test needed to be certain we are not opening the same file again? I don't think so, but not really sure of the internals of File::Find. I was assuming if I threw down the file it hands me. It would hand me a new different one. The files offered by `wanted()' are filtered thru an `if' clause I left this fact out by accident. sub wanted { if(/^\d+$/){.processing.. open(FILE),$File::Find::name; while(FILE{ if(something){ do something }else{ next LABEL; } } } } Looking at this simplified diagram... I think I may have answered my own question.. I want to exit to just above the first `if' filter. I guess just exiting the while loop will do that. so: LABEL: while(FILE){ processing} is sufficient? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: `next LABEL' usage
Harry Putnam [EMAIL PROTECTED] wrote: : : Charles K. Clarkson [EMAIL PROTECTED] writes: : Harry Putnam [EMAIL PROTECTED] wrote: : : [...] : : Wants to exit a while loop inside a File::Find \wanted : sub routine. Is exiting the while loop sufficient.. or : does one need to exit from the current file being offered : by `sub find()' : : : So using the `next LABEL' technique how do I : : designate the wanted() subroutine as target? : : Something like this: : : : : sub LABEL: wanted { : : I haven't tested it, but I would think this would fail. : : : open(FILE,File::Find::name); : : while (FILE){ : : if(something) { : : then do something : : }else{ : :next LABEL; : : } : : } : : } : : As I understand this, you want to immediately open : a new file found in current file with the current while : loop. Is that correct? : : Yes... : : First, we need to know a few things. : : Are you wanting to recursively call the entire : wanted() subroutine? : : I guess not no. I just want to go on to the next file : it has found Wait a minute! Hold on there Bucko! When I first read your response I thought you wanted to open a new file you found on your own, not one that File::Find would find itself. If that is all you want then you need only return from the subroutine. No LABEL needed. sub wanted { if( /^\d+$/ ) { # always check for success on open open FILE, $File::Find::name or die qq(Cannot open $File::Find::name: $!); while( FILE ) { if( something() ){ # do something } else { # close FILE and exit sub close FILE; return; } } } close FILE; return; } File::Find calls wanted() every time it finds a file. Each time it finds a file it resets $File::Find::name and a bunch of other variables. There is no need for you to do anything extra to make it work again and again. It would be wise not to clobber another file opened as FILE. To do that you would use something like this: sub wanted { # don't clobber open FILE local *FILE; if( /^\d+$/ ) { # always check for success on open open FILE, $File::Find::name or die qq(Cannot open $File::Find::name: $!); while( FILE ) { if( something() ){ # do something } else { # exit sub, FILE closes automatically return; } } } # exit sub, FILE closes automatically return; } Or in perl 5.6.1 or later: sub wanted { if( /^\d+$/ ) { # always check for success on open open my $fh, $File::Find::name or die qq(Cannot open $File::Find::name: $!); while( $fh ) { if( something() ){ # do something } else { # exit sub, $fh closes automatically return; } } } # exit sub, $fh closes automatically return; } HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: `next LABEL' usage
Charles K. Clarkson [EMAIL PROTECTED] writes: HTH, Definitely and thanks for the examples. I think I was making this more complicated that it needed to be. It's slowly sinking in what all a `return' can do. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: `next LABEL' usage
Harry Putnam wrote: Charles K. Clarkson [EMAIL PROTECTED] writes: HTH, Definitely and thanks for the examples. I think I was making this more complicated that it needed to be. It's slowly sinking in what all a `return' can do. Hi Harry, Glad Charles got you squared away. I have to say it again, though--you will make much more progress by focusing on a plain-language description of what you are trying to accomplish, than by thinking in code. Or, if you must express your ideas directly in code, run them in the command line compiler first, to get the immediately available sanity check, before posting. The problem with saying I want to do something like this... then showing guesswork code, is that the code you showed already does whatever it does, or nothing at all if sytax errors preclude compilation. How do we know, by looking at code alone, whether the effect, if any, achieved by the code, is the effect desired? Did you notice how quickly the issue resolved once Charles figured out what you wanted--in words? Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: next if statement....
The slashes are similar to a reg-ex expression on $_ in your example. So basically it checks the line to see if that exists and does something. In your case, for the ip to work, all you would have to do is remove your quotes so the line matches, since the file doesnt have quotes around the ip address. So, your line: next if /NAS-IP-Address = 192.168.0.1/; change to next if /NAS-IP-Address = 192.168.0.1/; and it will skip to the next line if that line ever comes up. -- --- Just Your Friendly Neighborhood _SPIDEY_ Greg Schiedler wrote: OK I'm trying to modify the code below to recognize an additional next if statement. I have included a snip of the file that the code uses an input. Greg next if /Acct-Session-Id = /;This statment works! # I added this statement. I don't understand the /'s in the statement. And my guess is that I need to modify the statement to properly work with an IP over a number or string. next if /NAS-IP-Address = 192.168.0.1/; This statement does not work. next if /User-Name = $userskp/; This statement works! Can anyone tell me why my simple addition will not work as I think it should. Greg :-) Code Snipit ... ... require 'usrmon.cf'; if (-M $file .04) { $date= (localtime)[3]; dbmopen(%hash, $dbfile, 0600); open (FILE, $file) || die; open (FILEBK, $filebk) || die; while (FILE) { $/ = ''; next if /Acct-Session-Id = /; next if /NAS-IP-Address = 192.168.0.1/; next if /User-Name = $userskp/; print FILEBK; if (/Acct-Status-Type = Stop/ || /Acct-Status-Type = Start/) { if (/User-Name = ([^]+)/){ $key= $1; } if (/Acct-Status-Type = Stop/) { if (/Acct-Session-Time = (\d+)/) { $used{$key}+= $1; $uses{$key}++; } } } } close(FILE); open (FILE, $file); print FILE \n; RADIUS Log Snip ... ... Ascend-PreSession-Time = 27 CVX-Modem-Begin-Modulation = V.90 CVX-Modem-Error-Correction = V.42 CVX-Modem-Data-Compression = V.42bis Timestamp = 1075939364 Timestamp = 1075939362 Wed Feb 4 16:03:11 2004 Acct-Status-Type = Start User-Name = [EMAIL PROTECTED] Acct-Session-Id = 062A Framed-IP-Address = 10.0.0.2 NAS-IP-Address = 192.168.0.1 Timestamp = 1075939456 Acct-Delay-Time = 0 Timestamp = 1075939391 Wed Feb 4 16:04:00 2004 Acct-Status-Type = Stop User-Name = [EMAIL PROTECTED] Acct-Session-Id = 062A Acct-Session-Time = 49 Framed-IP-Address = 10.0.0.2 NAS-IP-Address = 192.168.0.1 Timestamp = 1075939505 Acct-Delay-Time = 0 Timestamp = 1075939440 Wed Feb 4 16:04:21 2004 Acct-Status-Type = Start NAS-Identifier = cvx00.domain.com ... ... End RADIUS Log Snipit - The information contained in this message may be privileged, confidential, and protected from disclosure. If the reader of this message is not the intended recipient, or any employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by replying to the message and deleting it from your computer. Thank you. Paychex, Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: next if statement....
12:15pm, Greg Schiedler wrote: OK I'm trying to modify the code below to recognize an additional next if statement. I have included a snip of the file that the code uses an input. Greg next if /Acct-Session-Id = /;This statment works! A 'next if /./' is a short form for: if ($_ =~ m//) { next; } So... whatever is in between the forward slashes is the pattern you are looking for within the string contained in your default variable ($_). A single equals sign is *assignment*, where a double equal sign is a test of numeric equality (and 'eq' is a test to see if two strings are equal). HTH, Paul # I added this statement. I don't understand the /'s in the statement. And my guess is that I need to modify the statement to properly work with an IP over a number or string. next if /NAS-IP-Address = 192.168.0.1/; This statement does not work. next if /User-Name = $userskp/; This statement works! Can anyone tell me why my simple addition will not work as I think it should. Greg :-) Code Snipit ... ... require 'usrmon.cf'; if (-M $file .04) { $date= (localtime)[3]; dbmopen(%hash, $dbfile, 0600); open (FILE, $file) || die; open (FILEBK, $filebk) || die; while (FILE) { $/ = ''; next if /Acct-Session-Id = /; next if /NAS-IP-Address = 192.168.0.1/; next if /User-Name = $userskp/; print FILEBK; if (/Acct-Status-Type = Stop/ || /Acct-Status-Type = Start/) { if (/User-Name = ([^]+)/){ $key= $1; } if (/Acct-Status-Type = Stop/) { if (/Acct-Session-Time = (\d+)/) { $used{$key}+= $1; $uses{$key}++; } } } } close(FILE); open (FILE, $file); print FILE \n; RADIUS Log Snip ... ... Ascend-PreSession-Time = 27 CVX-Modem-Begin-Modulation = V.90 CVX-Modem-Error-Correction = V.42 CVX-Modem-Data-Compression = V.42bis Timestamp = 1075939364 Timestamp = 1075939362 Wed Feb 4 16:03:11 2004 Acct-Status-Type = Start User-Name = [EMAIL PROTECTED] Acct-Session-Id = 062A Framed-IP-Address = 10.0.0.2 NAS-IP-Address = 192.168.0.1 Timestamp = 1075939456 Acct-Delay-Time = 0 Timestamp = 1075939391 Wed Feb 4 16:04:00 2004 Acct-Status-Type = Stop User-Name = [EMAIL PROTECTED] Acct-Session-Id = 062A Acct-Session-Time = 49 Framed-IP-Address = 10.0.0.2 NAS-IP-Address = 192.168.0.1 Timestamp = 1075939505 Acct-Delay-Time = 0 Timestamp = 1075939440 Wed Feb 4 16:04:21 2004 Acct-Status-Type = Start NAS-Identifier = cvx00.domain.com ... ... End RADIUS Log Snipit -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response - Welcome to downtown Coolsville--population: us. - -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: next if statement....
Paul Archer wrote: 12:15pm, Greg Schiedler wrote: OK I'm trying to modify the code below to recognize an additional next if statement. I have included a snip of the file that the code uses an input. Greg next if /Acct-Session-Id = /;This statment works! A 'next if /./' is a short form for: if ($_ =~ m//) { next; } So... whatever is in between the forward slashes is the pattern you are looking for within the string contained in your default variable ($_). A single equals sign is *assignment*, where a double equal sign is a test of numeric equality (and 'eq' is a test to see if two strings are equal). Note, you're not using the equals as an assignment here. You're matching on the literal = in the string. HTH, Paul # I added this statement. I don't understand the /'s in the statement. And my guess is that I need to modify the statement to properly work with an IP over a number or string. next if /NAS-IP-Address = 192.168.0.1/; This statement does not work. In your log, the line to be matched is: NAS-IP-Address = 192.168.0.1 No quotes. It should match if you pull the quotes out of your regex statement, though I'd also suggest adding a \ before the .'s to ensure you match a period and not a wild character... next if /User-Name = $userskp/; This statement works! Can anyone tell me why my simple addition will not work as I think it should. Greg :-) Code Snipit ... ... require 'usrmon.cf'; if (-M $file .04) { $date= (localtime)[3]; dbmopen(%hash, $dbfile, 0600); open (FILE, $file) || die; open (FILEBK, $filebk) || die; while (FILE) { $/ = ''; next if /Acct-Session-Id = /; next if /NAS-IP-Address = 192.168.0.1/; next if /User-Name = $userskp/; print FILEBK; if (/Acct-Status-Type = Stop/ || /Acct-Status-Type = Start/) { if (/User-Name = ([^]+)/){ $key= $1; } if (/Acct-Status-Type = Stop/) { if (/Acct-Session-Time = (\d+)/) { $used{$key}+= $1; $uses{$key}++; } } } } close(FILE); open (FILE, $file); print FILE \n; RADIUS Log Snip ... ... Ascend-PreSession-Time = 27 CVX-Modem-Begin-Modulation = V.90 CVX-Modem-Error-Correction = V.42 CVX-Modem-Data-Compression = V.42bis Timestamp = 1075939364 Timestamp = 1075939362 Wed Feb 4 16:03:11 2004 Acct-Status-Type = Start User-Name = [EMAIL PROTECTED] Acct-Session-Id = 062A Framed-IP-Address = 10.0.0.2 NAS-IP-Address = 192.168.0.1 Timestamp = 1075939456 Acct-Delay-Time = 0 Timestamp = 1075939391 Wed Feb 4 16:04:00 2004 Acct-Status-Type = Stop User-Name = [EMAIL PROTECTED] Acct-Session-Id = 062A Acct-Session-Time = 49 Framed-IP-Address = 10.0.0.2 NAS-IP-Address = 192.168.0.1 Timestamp = 1075939505 Acct-Delay-Time = 0 Timestamp = 1075939440 Wed Feb 4 16:04:21 2004 Acct-Status-Type = Start NAS-Identifier = cvx00.domain.com ... ... End RADIUS Log Snipit -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response - Welcome to downtown Coolsville--population: us. - -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: next if statement....
-Original Message- From: Greg Schiedler [mailto:[EMAIL PROTECTED] Sent: Thursday, March 18, 2004 2:16 PM To: [EMAIL PROTECTED] Subject: next if statement OK I'm trying to modify the code below to recognize an additional next if statement. I have included a snip of the file that the code uses an input. Greg next if /Acct-Session-Id = /;This statment works! # I added this statement. I don't understand the /'s in the statement. And my guess is that I need to modify the statement to properly work with an IP over a number or string. next if /NAS-IP-Address = 192.168.0.1/; This statement does not work. next if /User-Name = $userskp/; This statement works! Can anyone tell me why my simple addition will not work as I think it should. Greg :-) Code Snipit ... ... require 'usrmon.cf'; if (-M $file .04) { $date= (localtime)[3]; dbmopen(%hash, $dbfile, 0600); open (FILE, $file) || die; open (FILEBK, $filebk) || die; while (FILE) { $/ = ''; next if /Acct-Session-Id = /; next if /NAS-IP-Address = 192.168.0.1/; next if /User-Name = $userskp/; print FILEBK; if (/Acct-Status-Type = Stop/ || /Acct-Status-Type = Start/) { if (/User-Name = ([^]+)/){ $key= $1; } if (/Acct-Status-Type = Stop/) { if (/Acct-Session-Time = (\d+)/) { $used{$key}+= $1; $uses{$key}++; } } } } close(FILE); open (FILE, $file); print FILE \n; RADIUS Log Snip ... ... Ascend-PreSession-Time = 27 CVX-Modem-Begin-Modulation = V.90 CVX-Modem-Error-Correction = V.42 CVX-Modem-Data-Compression = V.42bis Timestamp = 1075939364 Timestamp = 1075939362 Wed Feb 4 16:03:11 2004 Acct-Status-Type = Start User-Name = [EMAIL PROTECTED] Acct-Session-Id = 062A Framed-IP-Address = 10.0.0.2 NAS-IP-Address = 192.168.0.1 Timestamp = 1075939456 Acct-Delay-Time = 0 Timestamp = 1075939391 Wed Feb 4 16:04:00 2004 Acct-Status-Type = Stop User-Name = [EMAIL PROTECTED] Acct-Session-Id = 062A Acct-Session-Time = 49 Framed-IP-Address = 10.0.0.2 NAS-IP-Address = 192.168.0.1 Timestamp = 1075939505 Acct-Delay-Time = 0 Timestamp = 1075939440 Wed Feb 4 16:04:21 2004 Acct-Status-Type = Start NAS-Identifier = cvx00.domain.com ... ... End RADIUS Log Snipit -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: next if........ a cleaner way?
This should work... next if $name =~ /Block|nobody|User|www|backup|ftp|httpd|root|netop|sysop|users|bill/; next if $name =~ /^(?:\d|#)/; next if $quota !~ /\d+/; next if $quota = 8; You should also be able to combine the two (just make sure you test my syntax)... next if $name =~ /(?:^(?:\d|#))|Block|nobody|User|www|backup|ftp|httpd|root|netop|sysop|users |bill/; And the last two can be combined as well... next if $quota = 8; The reason this works is because if $quota does not start with a number it will evaluate to 0 in this context. Rob -Original Message- From: chad kellerman [mailto:[EMAIL PROTECTED] Sent: Thursday, March 20, 2003 9:34 AM To: [EMAIL PROTECTED] Subject: next if a cleaner way? Hello everyone, I want to clean this bit of code up. It looks really messy. I am in a mental block. Any suggestions? @userInfo = split /\s+/, $buffer; #insert home users quota in the db foreach $userInfo ( @userInfo ) { ( $name, $quota ) = split /\|/, $userInfo; # get rig of the header info from repquota next if ( ( $name =~ /Block/ ) or ( $name =~ /nobody/ ) or ( $name =~ /User/ ) or ( $name =~ /^\d/ ) or ( $name =~ /www/ ) or ( $name =~ /backup/ ) or ( $name =~ /ftp/ ) or ( $name =~ /httpd/ ) or ( $name =~ /root/ ) or ( $name =~ /netop/ ) or ( $name =~ /sysop/ ) or ( $name =~ /users/ ) or ( $quota !~ /\d+/ ) or ( $name =~ /^#/ ) or ( $quota = 8 ) or ($name =~ /bill/) ); Is there an easier way to loop thru a bunch of regex? Thanks for the help. Chad -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: next if........ a cleaner way?
Hi Chad. Chad Kellerman wrote: Hello everyone, I want to clean this bit of code up. It looks really messy. I am in a mental block. Any suggestions? @userInfo = split /\s+/, $buffer; Use a single space here to split on. @userInfo = split ' ', $buffer; It's a special case that splits on contiguous whitespace like your expression, but discards a null first field if there are leading spaces in the object string. #insert home users quota in the db foreach $userInfo ( @userInfo ) { ( $name, $quota ) = split /\|/, $userInfo; # get rig of the header info from repquota I hope you have used 'strict' and 'warnings'? I haven't seen any declarations! next if ( ( $name =~ /Block/ ) or ( $name =~ /nobody/ ) or ( $name =~ /User/ ) or ( $name =~ /^\d/ ) or ( $name =~ /www/ ) or ( $name =~ /backup/ ) or ( $name =~ /ftp/ ) or ( $name =~ /httpd/ ) or ( $name =~ /root/ ) or ( $name =~ /netop/ ) or ( $name =~ /sysop/ ) or ( $name =~ /users/ ) or ( $quota !~ /\d+/ ) or ( $name =~ /^#/ ) or ( $quota = 8 ) or ($name =~ /bill/) ); Is there an easier way to loop thru a bunch of regex? You could do: next if $name =~ /Block/; next if $name =~ /nobody/; : next if $name =~ /bill/; which looks a lot better (and shouldn't be any slower). Note that you're finding these strings anywhere in the object string. Is that what you want? Also, have you considered whether you need a case-sensitive match? The checks on $quota aren't quite right. The test next if $quota !~ /\d+/ will pass the string 'B52s' no problem, but the test next if $quota = 8 will give you a warning unless you have: no warnings 'numeric'; and will evaluate the object string as zero anyway. You might like: next unless $quota =~ /(\d+)/ and $1 8; which will check that the string contains a number, and if so that it's more than eight, but I don't know what your data looks like. This code should do the trick. foreach my $userInfo ( split ' ', $buffer ) { my ( $name, $quota ) = split /|/, $userInfo; next if grep $name =~ /$_/, qw( ^# ^\d backup bill Block ftp httpd netop nobody root sysop User users www ); next unless $quota =~ /(\d+)/ and $1 8; } but remember the provisos above. I can't help thinking though, that there's probably a better set of criteria to 'get rid of the header info'. Let us see your data format if yu need any further help. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: next if........ a cleaner way?
chad kellerman wrote: Hello everyone, I want to clean this bit of code up. It looks really messy. I am in a mental block. Any suggestions? @userInfo = split /\s+/, $buffer; #insert home users quota in the db foreach $userInfo ( @userInfo ) { ( $name, $quota ) = split /\|/, $userInfo; # get rig of the header info from repquota Right here is where you can start. Once you trim the user name, lowercase it lc($name); that will provide case insensitivity, presuming that was your intent. This should obviate the need to use such an unwieldy regex. I would recommend storing that list of authorized users in a file, reading it into an array, and then looping through that array, using the eq operator. Beyond aesthetics, ugly and clunky structures have a very practical ill effect: they make code harder to track, and thus increase the likelihood of error. next if ( ( $name =~ /Block/ ) or ( $name =~ /nobody/ ) or ( $name =~ /User/ ) or ( $name =~ /^\d/ ) or ( $name =~ /www/ ) or ( $name =~ /backup/ ) or ( $name =~ /ftp/ ) or ( $name =~ /httpd/ ) or ( $name =~ /root/ ) or ( $name =~ /netop/ ) or ( $name =~ /sysop/ ) or ( $name =~ /users/ ) or ( $quota !~ /\d+/ ) or ( $name =~ /^#/ ) or ( $quota = 8 ) or ($name =~ /bill/) ); Rob already gave you the best suggestion for regex alternatives, the '|' operator. Another thing that will help you keep your code neat is using a code editor. there are good code editors avaiable for both Winodws and 'nix. If you use Windows, I recommend Programmer's File Editor, available free at http://www.lancs.ac.uk/people/cpaap/pfe/. You need to get a better understanding of indentation conventions. Programming languages use indentation to mark execution blocks. Statements that are executed in sequence should be kept aligned. When a block, whether a subroutine definition, a conditional, a loop, or any other structures that sets off a set of lines for separate execution sequence, those lines affected should be indented by a specific [your taste, but preferably no more than four] number of *spaces* [tabs don;t translate well]. for (1..10) { do_one_thing(); then_another; if ($some_condition_exists) { take_first_response_step(); follow_up_with_response(); } else { take_alternate_course(); } } Notice something? With absolutely no coded specifics, you know how this program is going to execute, where the effect of any control statement starts and stops, and what the overall flow of the logic will be. Good, consistent indentation habits will make your work much easier. Let's take another crack at your problem here: my @names = qw ( Block nobody User www backup ftp httpd root netop sysop users ) foreach $userInfo ( @userInfo ) { my ($name, $quota ) = split /\|/, $userInfo; # get rig of the header info from repquota my $matched; foreach (@names) { if lc($_) eq lc($name) { matched = 1; last; } } if ($match or ($name =~ /^\d/) or ( $quota !~ /\d+/ ) or ( $name =~ /^#/ ) or ( $quota = 8 ) or ($name =~ /bill/)) { next; } } Note that I pulled out the or'ed conditions that were not really logically parralel. Having them mixed in that way can be a bit dangerous to comprehension. Is there any reason you had the conditions for $quota interleaved with those for $name in this way? Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Next 10 Results Help Needed
In article [EMAIL PROTECTED], [EMAIL PROTECTED] (Randal L. Schwartz) wrote: One of my very first WT columns... http://www.stonehenge.com/merlyn/WebTechniques/col02.html .. too bad I've already written my *last* WT column, to appear in Feb 2002, because the magazine is terminating. :( Cheers Randal, :-) Sorry to hear about that, but I did have one question... is anything stopping you from continuing the series writing them on your own and posting them to your website other than the financial incentive? :) print pack H*, 4a75737420416e6f74686572204d61635065726c204861636b65722c0d; -- Scott R. Godin| e-mail : [EMAIL PROTECTED] Laughing Dragon Services |web : http://www.webdragon.net/ It is not necessary to cc: me via e-mail unless you mean to speak off-group. I read these via nntp.perl.org, so as to get the stuff OUT of my mailbox. :-) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: next step
From: [EMAIL PROTECTED] This is a question for Perl programmers in the field. I would like to hear your advice in my next step. I work presently in a DOS - windows environment. I program Perl in DOS. It is pretty straight Perl scripts. Besides some DBI, and a sprinkle of CGI, it's mainly straight Perl. I learnt Perl, and took an excellent course in NYU in advanced Perl ('use' versus 'require,' type globs, tie scalar, oop, CGI, DBI etc.) I have not mastered them, but it opened me up to the big stuff. My question is how do I stay in Perl, and make myself more marketable. I want to advance financially. Most Perl jobs are not for straight Perl. Do I need to take Unix, Java, JavaScript, HTML, or none of the above (or all). (I know very few languages besides Perl.) Well ... you've seen the adverts, so you know what is wanted ;-) This really depends on what you like and what is your background. You may become an admin with Perl skills (Microsoft certificate on Win2k would help), a web programmer (you should learn HTML, JavaScript, some more database stuff, maybe if you are going to stay on Windows ASPs (just in case), COM (you can write those in Perl)), a database admin/programmer (courses on Oracle or MS SQL). Really there is no single advice. Tell us what would you want to end up doing ... Courses are expensive, so what do you think? (No attachments please.) And do you really need them? I've always learned more from manuals and my own experiments. Of course sometimes the courses may be helpfull especialy if you are starting to learn something totaly new, but I'd always think twise it I need them. On the other hand I have to admit that I've never been to a real course. Even when we were asked to write an application in Oracle and neither me nor my coleague have ever seen it before we made it without the course (and were pretty angry on the management that originaly promised to send us to a course). Jenda === [EMAIL PROTECTED] == http://Jenda.Krynicky.cz == There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain. I can't find it. --- me -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]