Home > Archive > PERL Beginners > January 2006 > loop Question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Ron McKeever 2006-01-11, 3:55 am |
| I wanted to print if it matches my range of IP's. I thought I could use the
(#..#) and it would work, but it didn't:
#!/usr/bin/perl -nlw
# Print if Ip's are in
# 111.9.1-18.### or
# 111.9.20-100.###
# range
#
my $a="1-18";
my $b="20-100";
while (<> )
{
chomp $_;
next if ($_ eq "");
my ( $number,$ip,$host ) = split(/,/,$_);
if ( $ip =~ /111.9.\b(1..18)\b.\d/){
print "$ip";
}
}
exit;
Thanks for any input.
rob
| |
| usenet@DavidFilmer.com 2006-01-11, 3:55 am |
| Ron McKeever wrote:
> if ( $ip =~ /111.9.\b(1..18)\b.\d/){
You can't do ranges in regexp's the same as you do in regular code.
Per 'perldoc perlre' and look for the paragraph beginning with "You can
specify a character class." In particular, note the passage which
advises:
[color=darkred]
So you can't (or shouldn't) specify the ranges in the manner you
attempted. Instead, consider something like this:
#!/usr/bin/perl
use strict; use warnings;
while ( my $ip = (<DATA> ) ) {
chomp($ip);
print "$ip\t";
if ( $ip =~ /111\.9\.[1-9]|1[0-8]\.\d+/
|| $ip =~ /111\.9\.2\d\.\d+/
|| $ip =~ /111\.9\.100\.\d+/ ) {
print "Matches\n";
}
else {
print "No_Match\n";
}
}
__DATA__
192.168.1.1
111.9.12.33
111.9.5.33
111.9.25.11
--
http://DavidFilmer.com
| |
| John W. Krahn 2006-01-21, 7:55 am |
| Ron McKeever wrote:
> I wanted to print if it matches my range of IP's. I thought I could use the
> (#..#) and it would work, but it didn't:
>
> #!/usr/bin/perl -nlw
>
> # Print if Ip's are in
> # 111.9.1-18.### or
> # 111.9.20-100.###
> # range
> #
> my $a="1-18";
> my $b="20-100";
>
> while (<> )
You are using the -n switch so you are already inside of a while loop!
> {
> chomp $_;
You are using the -l switch so the contents of $_ have already been chomp()ed!
> next if ($_ eq "");
>
> my ( $number,$ip,$host ) = split(/,/,$_);
> if ( $ip =~ /111.9.\b(1..18)\b.\d/){
UNTESTED.
if ( $ip =~
/\b111\.9\.(?:[1-9]|1[0-8]|[2-9]\d|100)\.(?:\d?\d|1\d\d|2[0-4]\d|25[0-5])\b/ ) {
> print "$ip";
> }
> }
> exit;
John
--
use Perl;
program
fulfillment
|
|
|
|
|