[8769] in Perl-Users-Digest
Perl-Users Digest, Issue: 2386 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Apr 22 13:31:49 1998
Date: Wed, 22 Apr 98 10:25:07 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 22 Apr 1998 Volume: 8 Number: 2386
Today's topics:
Q: Missing Last Character (Simon Steele)
Re: Q: Missing Last Character <sowmaster@juicepigs.com>
Re: Q: Simple RegExp match help required... (John Moreno)
Re: Q: Simple RegExp match help required... <fozz@mail.xmission.com>
Re: Question on exec and system <rootbeer@teleport.com>
Question on: printf %o STDOUT jessicam@llnl.gov
Re: Question on: printf %o STDOUT <rootbeer@teleport.com>
Re: Question on: printf %o STDOUT (Mark-Jason Dominus)
Re: rand($.) (was wc-l in Perl) <rootbeer@teleport.com>
Re: rand($.) (was wc-l in Perl) <johnnie@holly.colostate.edu>
Re: Random Number Generation PERL 5.0 (Craig Berry)
Re: Random Number Generation PERL 5.0 <johnnie@holly.colostate.edu>
recursive file renaming <eshin@mail.law.berkeley.edu>
Re: recursive file renaming <ebohlman@netcom.com>
Re: recursive file renaming (Ilya Zakharevich)
Re: recursive file renaming <Tony.Curtis+usenet@vcpc.univie.ac.at>
Regexp question <road@apdo.com>
Re: Regexp question <lr@hpl.hp.com>
Re: regular expression question (David Oswald)
Re: Regular Expression to match a valid IP address <bennett.aaron.abo@harte-hanks.com>
Re: Regular Expression to match a valid IP address (Fritz Knack)
Re: Regular Expression to match a valid IP address <danboo@negia.net>
Re: Regular Expression to match a valid IP address <Tony.Curtis+usenet@vcpc.univie.ac.at>
Re: Regular Expression to match a valid IP address <tchrist@mox.perl.com>
Re: Regular Expression to match a valid IP address (Huu Da Tran)
Re: Regular Expression to match a valid IP address <jdporter@min.net>
Re: Regular Expression to match a valid IP address <quentin@jihad.amd.com>
Re: Regular Expression to match a valid IP address <tchrist@mox.perl.com>
Remote Process Monitoring <mike@sccs.com>
Re: Remote Process Monitoring <rootbeer@teleport.com>
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 19 Apr 1998 17:19:53 GMT
From: apr98@alternate.demon.co.uk (Simon Steele)
Subject: Q: Missing Last Character
Message-Id: <353a30d0.27088827@127.0.0.1>
I have been learning Perl by reading Perl 5 by Example. I am currently
starting a web-site designed for programmers to use as a resource, and
wanted to create a credits page that was generated by a Perl script.
Eventually I plan to use Perl for a lot more than just the credits
page, but want to get this right first.
The script parses a file called 'credits.dat' which has one piece of
data per line:
SECTION!Section 1
PERSON!Credit 1
PERSON!Credit 2
The script returns HTML with any line beginning in SECTION as a
section header, and any other line as a credit. The problem is that
the script always misses the last character from the file:
Section1
========
Credit 1
Credit
Where the last line should read:
Credit 2
Please can you help me see what I've done wrong, the Perl script is:
#!/usr/local/bin/perl -w
$SectionSplitter = "SECTION";
open(FILE, "<credits.dat") or error("Could not open file
credits.dat");
@lines = <FILE>;
close(FILE);
print("Content Type: text/html\n\n");
print("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n");
print("<html>\n\n");
print("<head>\n");
<snip more HTML Headers>
foreach (@lines) {
chop;
($credType, $credName) = (split(/!/));
$credType = "CGI Error" if !defined($credType);
$credName = "CGI Error" if !defined($credName);
if ($credType eq $SectionSplitter) {print("</p>\n<p
class=\"TOPNEARBODY\" align=\"left\"><b>$credName\:</b><br><img
src=\"barfade1.gif\" Border=\"0\"></p>\n<p
class=\"BOTTOMNEARBODYINDENT\" align=\"left\">")} else
{print($credName . "<br>\n");}
}
print("</p>\n");
print("</font>\n</body>\n</html>");
sub error {
my($message) = @_;
print("Content-type: text/html\n\n");
print("<HTML>\n");
print("<HEAD><TITLE>CGI Error</TITLE></HEAD>\n");
print("<H1>Status: 500 An Error Has Occured</H1>\n");
print("<HR>\n");
print("$message\n");
print("</BODY>\n");
print("</HTML>\n");
}
Thanks in Advance,
Simon Steele.
------------------------------
Date: Sun, 19 Apr 1998 17:46:26 -0400
From: Bob Trieger <sowmaster@juicepigs.com>
To: Simon Steele <apr98@alternate.demon.co.uk>
Subject: Re: Q: Missing Last Character
Message-Id: <353A70B2.5CB7@juicepigs.com>
Simon Steele wrote:
> foreach (@lines) {
> chop;
^^^^
perldoc -f chomp
That will tell you what is happening and the simple fix
HTH
--
Bob Trieger | Titanic: big boat, bigger
sowmaster@juicepigs.com | iceberg, big deal
------------------------------
Date: Tue, 21 Apr 1998 21:02:02 GMT
From: phenix@interpath.com (John Moreno)
Subject: Re: Q: Simple RegExp match help required...
Message-Id: <1d7ufue.1alo6la1duxcblN@roxboro0-025.dyn.interpath.net>
jules <jules@jules.mangled.com> wrote:
> Good afternoon.
>
> I have (what I'm sure is) a very simple RegExp problem and there's a couple
> of possible things standing in the way of my expression from working:
>
> 1. I'm stupid.
>
> 2. I'm new to Perl.
>
> For right now I'm going to assume it's the latter. I'm trying to find lines
> like this in a text file:
>
> ...
> From ???@??? Tue May 14 07:26:36 1996
> ...
> From ???@??? Tue May 14 07:26:38 1996
> ...
> From ???@??? Tue May 21 17:21:46 1996
> ...
> From ???@??? Fri Sep 29 20:44:48 1995
> ...
> And so on...
>
> Here's the line in my script that's giving me grief:
>
> if ( $line = /^From \?\?\?\@\?\?\? */ ) {
>
> I'm reading that as "if $line equals a start of line, a space, three escaped
> question marks, escaped at sign, three more escaped question marks, a space,
> a wild card to catch everything else on the line"
>
> Doesn't catch anything.
>
> Interestingly I can get it to work with
>
> if ( $line =~ /From \?\?\?\@\?\?\? */ ) {
>
> Where am I screwing up? And, so I can learn from this, why am I screwing up?
You are screwing up in two ways - perl has a similar syntax to C and the
first if has several things happening, because you are missing the "~"
after the "=" (it should have been "=~") it is performing a assignment.
Translated out into english the if says: Search the variable $_ (which
is the default for pattern matches if not bound to a specific variable
using the "=~" operator) for (what you typed in) IF IT IS AT THE VERY
BEGINNING OF THE STRING, assign the result of the search (a boolean
value) to the variable $line and if $line is true (i.e. not zero) then
do whatever it is you cut out.
The second example works (but isn't exactly what you want) because you
are binding the pattern match to $line. But it will have false
positives (maybe) because it doesn't have to start at the beginning of a
line.
The string 'Damnit! From ???@??? Tue May 14 07:26:36 1996' would also
match. You'll want to put the "^" back in - and if the variable $line
can have more than one line in it you will want to use the /m option.
--
John Moreno
------------------------------
Date: Tue, 21 Apr 1998 15:34:19 -0600
From: "Doran L. Barton" <fozz@mail.xmission.com>
To: jules <jules@jules.com>
Subject: Re: Q: Simple RegExp match help required...
Message-Id: <353D10DB.F3EBE91D@mail.xmission.com>
jules wrote:
> Here's the line in my script that's giving me grief:
>
> if ( $line = /^From \?\?\?\@\?\?\? */ ) {
>
> I'm reading that as "if $line equals a start of line, a space, three escaped
> question marks, escaped at sign, three more escaped question marks, a space,
> a wild card to catch everything else on the line"
Asterisks in Perl/RegEx (*) are not "wildcards". They are one of several
repetition operators. When you see one, say to your self "repeat the previous
character zero or more times."
In your pattern, the asterisk matches a space zero or more times. If you want it
to serve the purpose of a "wildcard", then you need to use the period (.)- which
means "any single character." A period followed by an asterisk says "any single
character zero or more times." This doesn't mean the same character repeated-
but will match any stream of characters. WARNING: This can lead to problems if
you put .* in the middle of a pattern.
If you're not trying to match anything AFTER the ???@??? then you can just leave
off the "wildcard" all together. Regular expressions can match a line by only a
portion. If all you care about is the first X characters, then you don't have to
worry about representing the characters after them in your pattern.
Next, and I think someone already mentioned this... You probably don't want to
use the =~ operator. This will assign the contents of the $_ variable to $line
if the contents match your pattern. Instead, use the ~ operator.
ULTIMATE ADVICE: Get the "Camel Book."
-Fozz
------------------------------
Date: Wed, 22 Apr 1998 00:36:31 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Yong Huang <yong@shell.com>
Subject: Re: Question on exec and system
Message-Id: <Pine.GSO.3.96.980421173512.21174l-100000@user2.teleport.com>
On Tue, 21 Apr 1998, Yong Huang wrote:
> is it true that if my arguments are straight commands with no
> metacharacters, using exec is faster than system (suppose they're both
> at the end of my script and I don't worry if the execution doesn't come
> back)?
system uses an exec internally (on a Unix-type OS). So, if either would do
the job, exec would be my choice. Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Tue, 21 Apr 1998 18:36:02 -0600
From: jessicam@llnl.gov
Subject: Question on: printf %o STDOUT
Message-Id: <6hjah2$5uq$1@nnrp1.dejanews.com>
How do I get the value from STDOUT and assign it to a variable without
redirecting to a file? I'm trying to get the mode of a file in octal ( i.e.
100640 ) from printf %o and I want to save the STDOUT value without writing it
to a file.
Any suggestion? Thanks!
------- code example --------
# Get the permission mode from the file.
$mode = (stat($filename))[2];
printf "The mode of $filename octal mode is %o\n", $mode;
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
------------------------------
Date: Wed, 22 Apr 1998 01:26:15 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: jessicam@llnl.gov
Subject: Re: Question on: printf %o STDOUT
Message-Id: <Pine.GSO.3.96.980421182515.21174s-100000@user2.teleport.com>
On Tue, 21 Apr 1998 jessicam@llnl.gov wrote:
> How do I get the value from STDOUT and assign it to a variable without
> redirecting to a file?
If you don't want to print it, don't print it! :-)
> I'm trying to get the mode of a file in octal ( i.e. 100640 ) from
> printf %o and I want to save the STDOUT value without writing it to a
> file.
You want sprintf, I suspect. Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: 22 Apr 1998 09:55:34 -0400
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: Question on: printf %o STDOUT
Message-Id: <6hkssm$8lh$1@monet.op.net>
Keywords: Beloit secretariat spitfire warden
In article <6hjah2$5uq$1@nnrp1.dejanews.com>, <jessicam@llnl.gov> wrote:
>How do I get the value from STDOUT and assign it to a variable without
>redirecting to a file? I'm trying to get the mode of a file in octal ( i.e.
>100640 ) from printf %o and I want to save the STDOUT value without writing it
>to a file.
For that you use `sprintf' instead of `printf':
$val = sprintf "%o", $mode;
printf "%xxx", $yyy;
is just short for
print (sprintf "%xxx", $yyy);
------------------------------
Date: Sun, 19 Apr 1998 16:58:42 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Jamie McCarthy <jamie@mccarthy.org>
Subject: Re: rand($.) (was wc-l in Perl)
Message-Id: <Pine.GSO.3.96.980419095649.1173F-100000@user2.teleport.com>
On Fri, 17 Apr 1998, Jamie McCarthy wrote:
> Tom Phoenix <rootbeer@teleport.com> wrote:
>
> > > > open FILE, $name or die "Can't read '$name': $!";
> > > > while (<FILE>) {
> > > > $line = $_ if rand($.) <= 1;
> ^^ should be "<"
> > > > }
> > > > print "One randomly-selected line is $line";
>
> > It _is_ biased, but towards the end of the file (see my explanation at
> > http://cpan.perl.org/doc/FMTEYEWTK/random).
>
> Your explanation is true, but it's also slightly biased because of the
> compare. rand(1) returns a value between 0 and 1: "including 0, but
> excluding 1" according to The Camel. So in a two-line file, the
> second line is ever-so-slightly more likely to be picked than the
> first, if you use "<= 1" instead of "< 1". Of course the effect is
> negligible unless used on files with a whole lot of lines.
That's good that you're paying attention to the details. That should have
been a less-than sign; you're right. Thanks!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Mon, 20 Apr 1998 10:49:52 -0600
From: John Blanco <johnnie@holly.colostate.edu>
Subject: Re: rand($.) (was wc-l in Perl)
Message-Id: <353B7CB0.49612DF@holly.colostate.edu>
Tom Mornini wrote:
> In a discussion about picking a random line from a file Tom Phoenix
> wrote:
>
> > open FILE, $name or die "Can't read '$name': $!";
> > while (<FILE>) {
> > $line = $_ if rand($.) <= 1;
> > }
> > print "One randomly-selected line is $line";
>
> My question is: Why doesn't this hopelessly bias the randomness
> towards
> the beginning of the file, when rand($.) would be more likely to be <=
> 1?
>
Instead of testing the program by finding the line it chooses, adjust
the script so it prints each line number it ever does choose. In a
sample run on a 964 line file my results were:
Line 1 Chosen!
Line 2 Chosen!
Line 4 Chosen!
Line 7 Chosen!
Line 15 Chosen!
Line 66 Chosen!
Line 131 Chosen!
Line 253 Chosen!
Line 374 Chosen!
Line 544 Chosen!
As you can see, and as everyone else said, it is MUCH more likely to
choose a line early on in the file than later -- however, the line you
end up with, nameley 544 in this case, is usually what is chosen because
it will end up being the "dark horse" pick that became the last
selected.
> If fact, I don't believe this code even guarantees that a line is
> picked!
Yes it does. After choosing the first line of a file, rand(1) will
always be <= 1. Therefore, at the very least, you will always have the
first line of the file. That is, of course, assuming your file is not 0
length. :)
- Johnnie B
------------------------------
Date: 21 Apr 1998 22:56:06 GMT
From: cberry@cinenet.net (Craig Berry)
Subject: Re: Random Number Generation PERL 5.0
Message-Id: <6hj866$fkp$1@marina.cinenet.net>
The Curz1 (thecurz1@aol.com) wrote:
: We are in dire need of generating a random number from 0 to <EndOfFile> for use
: on our website and opening day is scheduled for May 1st. We are somewhat new to
: PERL 5.0 but have good knowlege of JavaScript. We already have an application
: written in JavaScript that works well but want one that uses PERL so that it
: will run on all browsers AND the source code won't be visible to our
: competition. Specifically what we are doing is creating a Random Banner Ad
: Generator so that a different (and random) banner is displayed each time. I
: have been able to generate the number based on the localtime (seconds) but
: since we do not have 60 banners, we need a way to prevent the number from being
: larger than the number of banners. Please help if you can.
Three pieces of advice:
* If you can't work out how to use rand, the seconds field of localtime,
or some other source of a number to generate an integer in any desired
range 0..N, you probably don't (yet) have the programming experience (in
Perl) to build anything signficant (that works properly) before May 1.
Your choices are to do it in another language that your staff already
knows, or (quickly!) hire a contractor.
* If you insist on attempting the job yourself, don't reinvent the
wheel. Selena Sol has a random banner generator app that should suit
your needs, see: http://www.extropia.com/Scripts/
* If you insist on reinventing the wheel, use rand rather than
localtime-seconds.
* If you want private help reinventing the wheel, send me email and we'll
discuss consulting arrangements. :)
HTH!
---------------------------------------------------------------------
| Craig Berry - cberry@cinenet.net
--*-- Home Page: http://www.cinenet.net/users/cberry/home.html
| Member of The HTML Writers Guild: http://www.hwg.org/
"Every man and every woman is a star."
------------------------------
Date: Tue, 21 Apr 1998 20:23:14 -0600
From: John Blanco <johnnie@holly.colostate.edu>
To: The Curz1 <thecurz1@aol.com>
Subject: Re: Random Number Generation PERL 5.0
Message-Id: <353D5492.70F0A370@holly.colostate.edu>
The Curz1 wrote:
> We are in dire need of generating a random number from 0 to
> <EndOfFile> for use
> on our website and opening day is scheduled for May 1st. We are
> somewhat new to
> since we do not have 60 banners, we need a way to prevent the number
> from being
> larger than the number of banners. Please help if you can.
Are you asking the simple question I think you are? Either way, if
you're generating a random number that is too high for your needs, you
just need the services of Modulo! If you have a random number
generated from 0 through 59 and it is assigned to $i, just do this:
show_banner($i % $NUM_BANNERS);
Of course, this makes some banners more likely to occur than others.
But that's why you generate
a number from 0 to whatever.
srand time;
rand $NUM_BANNERS;
- Johnnie B
------------------------------
Date: Mon, 20 Apr 1998 20:54:23 -0700
From: Edwin Shin <eshin@mail.law.berkeley.edu>
Subject: recursive file renaming
Message-Id: <353C186F.D35A01AE@mail.law.berkeley.edu>
can anyone offer a quick and easy way to make the following script
recursively plow through a directory tree?
i would like, for instance, to rename all *.shtml files to *.html in a
given directory tree.
tia,
-eddie
#!/usr/bin/perl
print "Enter the file extension you wish to replace (e.g. \"shtml\"): ";
chomp ($oldSuffix = <STDIN>);
print "\nEnter the replacement extension (e.g. \"html\"): ";
chomp ($newSuffix = <STDIN>);
foreach $_ (<*.$oldSuffix>) {
/$oldSuffix/;
rename("$_","$`$newSuffix");
}
--
Edwin Shin
School of Law
University of California at Berkeley
------------------------------
Date: Tue, 21 Apr 1998 05:44:50 GMT
From: Eric Bohlman <ebohlman@netcom.com>
Subject: Re: recursive file renaming
Message-Id: <ebohlmanErr1Aq.4uF@netcom.com>
Edwin Shin <eshin@mail.law.berkeley.edu> wrote:
: can anyone offer a quick and easy way to make the following script
: recursively plow through a directory tree?
: i would like, for instance, to rename all *.shtml files to *.html in a
: given directory tree.
perldoc File::Find
------------------------------
Date: 21 Apr 1998 06:40:13 GMT
From: ilya@math.ohio-state.edu (Ilya Zakharevich)
Subject: Re: recursive file renaming
Message-Id: <6hhf0d$mgr$1@mathserv.mps.ohio-state.edu>
[A complimentary Cc of this posting was sent to Edwin Shin
<eshin@law.berkeley.edu>],
who wrote in article <353C186F.D35A01AE@mail.law.berkeley.edu>:
> can anyone offer a quick and easy way to make the following script
> recursively plow through a directory tree?
>
> i would like, for instance, to rename all *.shtml files to *.html in a
> given directory tree.
pfind . 's/\.shtml$/.html/ and prt'
(assuming *nixish shell, otherwise you need double quotes - or no
quotes at all). Availability: CPAN.
Ilya
------------------------------
Date: 21 Apr 1998 08:53:51 +0200
From: Tony Curtis <Tony.Curtis+usenet@vcpc.univie.ac.at>
To: eshin@mail.law.berkeley.edu
Subject: Re: recursive file renaming
Message-Id: <7xwwcjr7gg.fsf@beavis.vcpc.univie.ac.at>
Re: recursive file renaming, Edwin
<eshin@mail.law.berkeley.edu> said:
Edwin> can anyone offer a quick and easy way to make the
Edwin> following script recursively plow through a directory
Edwin> tree?
Edwin> i would like, for instance, to rename all *.shtml
Edwin> files to *.html in a given directory tree.
use the File::Find module.
--
Tony Curtis, Systems Manager, VCPC, | Tel +43 1 310 93 96 - 12; Fax - 13
Liechtensteinstrasse 22, A-1090 Wien, AT | http://www.vcpc.univie.ac.at/
"You see? You see? Your stupid minds! Stupid! Stupid!" ~ Eros, Plan9 fOS.
------------------------------
Date: Mon, 20 Apr 1998 19:54:30 +0200
From: "A.P." <road@apdo.com>
Subject: Regexp question
Message-Id: <353B8BD5.E20015D3@apdo.com>
I'm learning Perl reading the Llama book. There's an exercise proposing
to find a regexp to match the same word written two or more times in a
row (with possibly varying intervening whitespace). The book proposes
the following answer:
/(^|\s)(\S+)(\s+\2)+(\s|$)/
Why do (^|\s) and (\s|$) match any number of leading/trailing
whitespaces and not just one or none?
It's a really messy one this regexp business!!
Thanks a lot.
------------------------------
Date: Mon, 20 Apr 1998 16:19:06 -0700
From: "Larry Rosler" <lr@hpl.hp.com>
Subject: Re: Regexp question
Message-Id: <6hgl56$j5o@hplntx.hpl.hp.com>
A.P. wrote in message <353B8BD5.E20015D3@apdo.com>...
>I'm learning Perl reading the Llama book. There's an exercise proposing
>to find a regexp to match the same word written two or more times in a
>row (with possibly varying intervening whitespace). The book proposes
>the following answer:
>
>/(^|\s)(\S+)(\s+\2)+(\s|$)/
>
>Why do (^|\s) and (\s|$) match any number of leading/trailing
>whitespaces and not just one or none?
>It's a really messy one this regexp business!!
>Thanks a lot.
Blame it on those vertical bars. (^|\s) matches the beginning of the
string OR a space character; (\s|$) matches a space character OR the end
of the string. Therefore they each apparently "match" any number of
leading/trailing whitespaces. In fact they match the last one (if any)
for (^|\s) and the first one (if any) for (\s|$). Try printing $1 and
$4 to see this.
As the book says, these subexpressions ensure that the stuff inbetween
is contained within word boundaries. Later you will meet that idea
expressed more straigntforwardly as \b.
HTH,
--
Larry Rosler
Hewlett-Packard Laboratories
lr@hpl.hp.com
------------------------------
Date: Tue, 21 Apr 1998 08:55:59 GMT
From: doswald@xmission.com (David Oswald)
Subject: Re: regular expression question
Message-Id: <353c5e93.52036614@news.xmission.com>
On 17 Apr 1998 06:07:49 GMT, cberry@cinenet.net (Craig Berry) wrote:
>John Cartwright (jcartwright@wans.net) wrote:
>: I'm trying to build a regular expression that will return only the
>: signifigant digits to the right of the decimal point. For instance, if my
>: value is "1.01200", what I would like to get back is "012". Although I've
>: tried a number of alternatives, I was using something like:
>:
>: $a = "1.01200";
>: $a =~ s/^(\d+\.) (\d+)0+$/$2/;
>:
>: unfortunately, this returns the whole number "1.012". Can someone help me out?
>
> $a =~ s/.*\.(\d+?)0*$/$1/;
I posted that same regex solution a couple of days ago and someone
wrote back to say that the non-greedy quantifier wasn't necessary.
However, I've read it and re-read it, and agree with you that it is
necessary. I can't imagine a situation where the above regex would
work properly without non-greedy matching of \d+?
------------------------------
Date: Mon, 20 Apr 1998 11:12:03 -0400
From: Aaron Bennett <bennett.aaron.abo@harte-hanks.com>
Subject: Re: Regular Expression to match a valid IP address
Message-Id: <353B65C3.A42B84BD@harte-hanks.com>
I would also suggest looking at "Mastering Regular Expressions," from
O'Reilly Press by Jeffrey Friedl -- on page 123 there is an extended
example dealing with matching ip addresses.
------------------------------
Aaron Bennett
UNIX Systems Administrator
Harte-Hanks Response Management Boston
bennett.aaron.abo@harte-hanks.com
Rob Smith wrote:
>
> I'm definitely new to Perl, and have been working on a project for work.
> I'm at a point where I want to validate an IP entered. Is there a regular
> expression that would help me? I want to validate that the first and last
> octet s between 1 and 254. Of course the 2nd and 3rd from 0-255. I want it
> to also check that there is a dot between the numbers since the input is
> used later on for several routines.
>
> Thanks for any help.
>
> Greatly appreciated. I've looked up several areas for Regular Expressions,
> but they don't explain much about the numeric expressions...
>
> Thanks....
>
> Rob
>
> resmith@uscsu.sc.edu
------------------------------
Date: Mon, 20 Apr 1998 14:16:13 GMT
From: fritz.knack@nospam.POPULUS.net (Fritz Knack)
Subject: Re: Regular Expression to match a valid IP address
Message-Id: <353b5624.5674959@cnews.newsguy.com>
On Mon, 20 Apr 1998 08:46:42 -0400, "Rob Smith" <robsmith@writeme.com>
wrote:
>I'm definitely new to Perl, and have been working on a project for work.
>I'm at a point where I want to validate an IP entered. Is there a regular
>expression that would help me? I want to validate that the first and last
>octet s between 1 and 254. Of course the 2nd and 3rd from 0-255. I want it
>to also check that there is a dot between the numbers since the input is
>used later on for several routines.
>
Still speaking mostly baby talk, I'd probably do something along the
lines of
m#(\d)+.(\d)+.(\d)+.(\d)+#
and then verify ranges for $1, $2, etc. I'm sure there's a more
elegant solution.
Best regards,
Fritz
-------------------------
Sorry 'bout the nospam in the From field. You know how those 'bots are.
------------------------------
Date: Mon, 20 Apr 1998 11:44:46 -0400
From: Dan Boorstein <danboo@negia.net>
Subject: Re: Regular Expression to match a valid IP address
Message-Id: <353B6D6E.6FBB8334@negia.net>
Fritz Knack wrote:
>
> On Mon, 20 Apr 1998 08:46:42 -0400, "Rob Smith" <robsmith@writeme.com>
> wrote:
>
> >I'm definitely new to Perl, and have been working on a project for work.
> >I'm at a point where I want to validate an IP entered. Is there a regular
[SNIP]
> >
> Still speaking mostly baby talk, I'd probably do something along the
> lines of
> m#(\d)+.(\d)+.(\d)+.(\d)+#
> and then verify ranges for $1, $2, etc. I'm sure there's a more
> elegant solution.
well, if you consider a solution that works to be more elegant . . . :-)
having the +'s outside the capturing parens makes for a significant
difference. try running the following.
$_ = '255.255.255.0';
m/(\d)+.(\d)+.(\d)+.(\d)+/;
print "$1, $2, $3, $4\n";
m/(\d+).(\d+).(\d+).(\d+)/;
print "$1, $2, $3, $4\n";
hope this helps,
--
dan boorstein
------------------------------
Date: 20 Apr 1998 18:55:41 +0200
From: Tony Curtis <Tony.Curtis+usenet@vcpc.univie.ac.at>
Subject: Re: Regular Expression to match a valid IP address
Message-Id: <7x67k4l9f6.fsf@beavis.vcpc.univie.ac.at>
Re: Regular Expression to match a valid IP address,
Fritz <fritz.knack@nospam.POPULUS.net> said:
Fritz> Still speaking mostly baby talk, I'd probably do
Fritz> something along the lines of
Fritz> m#(\d)+.(\d)+.(\d)+.(\d)+# and then verify ranges for
That should be \. throughout because `.' is a
meta-character.
--
Tony Curtis, Systems Manager, VCPC, | Tel +43 1 310 93 96 - 12; Fax - 13
Liechtensteinstrasse 22, A-1090 Wien, AT | http://www.vcpc.univie.ac.at/
"You see? You see? Your stupid minds! Stupid! Stupid!" ~ Eros, Plan9 fOS.
------------------------------
Date: 20 Apr 1998 15:54:10 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Regular Expression to match a valid IP address
Message-Id: <6hfr32$o7l$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
Tony Curtis <Tony.Curtis+usenet@vcpc.univie.ac.at> writes:
:An IP address expressed in dotted-quad form consists of 4
:sequences of decimal digits, separated by `.'
Not all valid IP addrs are dotted quads!
--tom
--
Tom Christiansen tchrist@jhereg.perl.com
There ain't nothin' in this world that's worth being a snot over.
--Larry Wall in <1992Aug19.041614.6963@netlabs.com>
------------------------------
Date: Mon, 20 Apr 1998 17:53:28 GMT
From: tranhu@jsp.umontreal.ca (Huu Da Tran)
Subject: Re: Regular Expression to match a valid IP address
Message-Id: <slrn6jn2si.kui.tranhu@derby.jsp.umontreal.ca>
Un jour, Dan Boorstein (danboo@negia.net)
affirmait publiquement que:
| having the +'s outside the capturing parens makes for a significant
| difference. try running the following.
|
| $_ = '255.255.255.0';
| m/(\d)+.(\d)+.(\d)+.(\d)+/;
| print "$1, $2, $3, $4\n";
| m/(\d+).(\d+).(\d+).(\d+)/;
| print "$1, $2, $3, $4\n";
Would it be faster to use this instead?
/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
- --
__________________________________________________________________________
TRAN, Huu Da Universiti de Montrial
tranhu@jsp.umontreal.ca http://www.jsp.umontreal.ca/~tranhu/
Version: PGPfreeware 5.0i for non-commercial use
Charset: noconv
iQEVAwUBNTuLjq8TFco5wIPxAQESXgf/Vy6puOTPd8JFMZ+1AnjNouGP/GGl1ZdK
rRw2AI9PclMEPIvxLJRcVD5G5cnKLwJTXV1eOEfjmgWiczZBq0O7JnjQZq+KCnZq
RXla1JqfZG1gvd6D67OkrBjzuqHz9lqbptKR+WWYosTPakZo/+ttBH5Sw56NrHdL
RHuWlHPifbZk+ElySvvfY7wWK9ZUcNaHU3zD8fjQ7em9L7YS1w/NySss2PhbaJCd
o4nJz0LspPBCjwJaDn4bRa0ifZXigBZMTGNNNPrTk+qSqP3Gb7OK30Mq3p2uCD87
qraxHSGp3BnX2fNUTjg2G9gCVrntZlSwJP9ri+9vUq3jaHb/wV+MqA==
=u7q5
-----END PGP SIGNATURE-----
------------------------------
Date: Mon, 20 Apr 1998 19:07:21 GMT
From: John Porter <jdporter@min.net>
Subject: Re: Regular Expression to match a valid IP address
Message-Id: <353B9E6F.7D34@min.net>
Tony Curtis wrote: [liberally snipped by John Porter]
>
> An IP address expressed in dotted-quad form consists of 4
> sequences of decimal digits, separated by `.'
>
> $ip_address = "192.68.102.44";
> @octet = split(/\./, $ip_address);
>
> Then you can range-check the members of the octet array,
>
> Sanity checks on @octet can include how big it is (e.g. to
> make sure all 4 octets were present).
Tony's is the closest to right so far in this thread (which
btw has been beaten to death in previous threads. check
deja news.)
There are many legal, valid representations of IP addresses
besided "dotted quad". If we label the bytes in the 32-bit
number as ABCD, then you could have A.B.C.D, the traditional
dotted quad. But you can also have A.B.CD, A.BCD, and even ABCD.
That means the range-checking on the last element depends on
how many elements there were.
John Porter
------------------------------
Date: 20 Apr 1998 17:02:36 -0500
From: Quentin Fennessy <quentin@jihad.amd.com>
Subject: Re: Regular Expression to match a valid IP address
Message-Id: <ximpvicdudf.fsf@jihad.amd.com>
>>>>> "HDT" == Huu Da Tran <tranhu@jsp.umontreal.ca> writes:
HDT> Would it be faster to use this instead?
HDT> /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
Possibly faster.
But that would match 999.999.999.999. Not a valid IPv4 address.
Perhaps IPv7. :-)
--
Quentin Fennessy AMD, Austin Texas
Secret hacker rule #11 - hackers read manuals
------------------------------
Date: 22 Apr 1998 02:56:59 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Regular Expression to match a valid IP address
Message-Id: <6hjm9r$jol$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, "Rob Smith" <robsmith@writeme.com> writes:
:I'm definitely new to Perl, and have been working on a project for work.
:I'm at a point where I want to validate an IP entered.
Skip the regex approach. Seize the wheel, don't rebuild it. What you
should do is this:
#!/usr/bin/perl
use Socket;
for $ip ( @ARGV ) {
printf "%s is %s a valid ip address\n", $ip,
defined(inet_aton($ip)) ? "indeed" : "NOT";
}
Anything else will be very complicated, wrong, or most likely both.
--tom
--
Tom Christiansen tchrist@jhereg.perl.com
"If you only have a nail, you tend to see every hammer as a problem."
--Larry Wall
------------------------------
Date: Tue, 21 Apr 1998 19:38:52 -0400
From: Mike Schlienz <mike@sccs.com>
Subject: Remote Process Monitoring
Message-Id: <353D2E0C.8FFB940F@sccs.com>
Does anyone know of a way to monitor processes running on a remote/network
server? Thanx in advance for any info.
------------------------------
Date: Wed, 22 Apr 1998 01:24:33 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Mike Schlienz <mike@sccs.com>
Subject: Re: Remote Process Monitoring
Message-Id: <Pine.GSO.3.96.980421182307.21174r-100000@user2.teleport.com>
On Tue, 21 Apr 1998, Mike Schlienz wrote:
> Does anyone know of a way to monitor processes running on a
> remote/network server?
Yes. If you want to do this, I recommend that you determine a suitable
protocol, then implement it. There are some modules on CPAN which may be
useful. Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V8 Issue 2386
**************************************