[23146] in Perl-Users-Digest
Perl-Users Digest, Issue: 5367 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Aug 15 18:05:48 2003
Date: Fri, 15 Aug 2003 15:05:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 15 Aug 2003 Volume: 10 Number: 5367
Today's topics:
convert seconds to minutes & hours <erik.waibel@cubic.com>
Re: convert seconds to minutes & hours <glex_nospam@qwest.net>
Re: convert seconds to minutes & hours <cat@kill-spam.com>
Re: convert seconds to minutes & hours (Tad McClellan)
Re: convert seconds to minutes & hours <bharn_S_ish@te_P_chnologi_A_st._M_com>
Re: convert seconds to minutes & hours <krahnj@acm.org>
Does inet_aton() work for 'localhost'? <mooseshoes@gmx.net>
Re: Does inet_aton() work for 'localhost'? <ndronen@io.frii.com>
Re: Encrypting a superuser <bharn_S_ish@te_P_chnologi_A_st._M_com>
Re: Encrypting a superuser nobull@mail.com
Finding 'path to perl' of remote server (Vijoy Varghese)
Re: Finding 'path to perl' of remote server <rgarciasuarez@free.fr>
Finding the size of a large file (John N.)
Re: Finding the size of a large file <michael.p.broida@boeing.com>
Re: Finding the size of a large file <ndronen@io.frii.com>
Re: Help! Regular expression on html... (Randal L. Schwartz)
Re: Help! Regular expression on html... <syscjm@gwu.edu>
How to create a Perl module to be used in a CGI script (James)
Re: How to create a Perl module to be used in a CGI sc <bharn_S_ish@te_P_chnologi_A_st._M_com>
How to express "not followed by"? (Yi Mang)
Re: How to express "not followed by"? <tore@aursand.no>
Re: How to express "not followed by"? <noreply@gunnar.cc>
Re: How to express "not followed by"? <spamblock@junkmail.com>
Re: How to express "not followed by"? <xaonon@hotpop.com>
Re: How to express "not followed by"? <noreply@gunnar.cc>
Re: parsing log in multiple passes <krahnj@acm.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 15 Aug 2003 15:09:21 GMT
From: "Erik Waibel" <erik.waibel@cubic.com>
Subject: convert seconds to minutes & hours
Message-Id: <BI6%a.1330$6W2.549@newssvr25.news.prodigy.com>
Hi all,
I know this is a simple question, but I just can't seem to find the answer
anywhere. Could someone please help me to convert seconds into hours :
minutes : seconds...say I have like 1080 seconds, I want this to return 0
hours, 18 minutes, and 0 seconds.
--
Thanks,
Erik Waibel
----------
Perl Hacker v.1.0
------------------------------
Date: Fri, 15 Aug 2003 10:47:50 -0500
From: "J. Gleixner" <glex_nospam@qwest.net>
Subject: Re: convert seconds to minutes & hours
Message-Id: <bf7%a.115$Ci5.13261@news.uswest.net>
Erik Waibel wrote:
> Hi all,
>
> I know this is a simple question, but I just can't seem to find the answer
> anywhere. Could someone please help me to convert seconds into hours :
> minutes : seconds...say I have like 1080 seconds, I want this to return 0
> hours, 18 minutes, and 0 seconds.
>
The modulus (%), multiplication (*), and division (/) should be all you
need. Just convert everything to seconds, e.g. number of seconds in an
hour (60 * 60) , number of seconds in a minute, etc. and use that to
figure out your conversion. Take how you'd figure it out on paper, or
using a calculator, and do the same thing in the script.
------------------------------
Date: Sat, 16 Aug 2003 01:49:13 +1000
From: Cat <cat@kill-spam.com>
Subject: Re: convert seconds to minutes & hours
Message-Id: <3F3D00F9.4F2769B@kill-spam.com>
Erik Waibel wrote:
>
> I know this is a simple question, but I just can't seem to find the answer
> anywhere. Could someone please help me to convert seconds into hours :
> minutes : seconds...say I have like 1080 seconds, I want this to return 0
> hours, 18 minutes, and 0 seconds.
#!/bin/perl
use strict;
use warnings;
my $seconds = 1080;
my $hrs = int( $seconds / (60*60) );
my $min = int( ($seconds - $hrs*60*60) / (60) );
my $sec = int( $seconds - ($hrs*60*60) - ($min*60) );
print("$hrs hours, $min minutes, and $sec seconds.\n");
------------------------------
Date: Fri, 15 Aug 2003 12:40:38 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: convert seconds to minutes & hours
Message-Id: <slrnbjq6om.a41.tadmc@magna.augustmail.com>
[ wasteland Newsgroup removed ]
Erik Waibel <erik.waibel@cubic.com> wrote:
> Could someone please help me to convert seconds into hours :
> minutes : seconds
--------------------------------------
#!/usr/bin/perl
use strict;
use warnings;
my $time = 1080;
my $hours = int ($time / 3600);
$time = $time % 3600;
my $minutes = int ($time / 60);
$time = $time % 60;
my $seconds = $time;
print "$hours:$minutes:$seconds\n";
--------------------------------------
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 15 Aug 2003 18:53:00 GMT
From: Brian Harnish <bharn_S_ish@te_P_chnologi_A_st._M_com>
Subject: Re: convert seconds to minutes & hours
Message-Id: <pan.2003.08.15.18.53.39.800198@te_P_chnologi_A_st._M_com>
On Fri, 15 Aug 2003 15:09:21 +0000, Erik Waibel wrote:
> Hi all,
>
> I know this is a simple question, but I just can't seem to find the answer
> anywhere. Could someone please help me to convert seconds into hours :
> minutes : seconds...say I have like 1080 seconds, I want this to return 0
> hours, 18 minutes, and 0 seconds.
I had to go the fun way on this one.
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my $secs = 1082;
my @words = (hour => 24, minute => 60, second => 60);
my @x = reverse map { my $y=$secs%$_; $secs=int $secs/$_; $y } (reverse grep{/\d
+/}@words);
print join(', ', map { "$x[$_] $words[$_*2]" . ($x[$_]!=1?'s':'') } (0..$#x)),$/
;
__END__
- Brian
------------------------------
Date: Fri, 15 Aug 2003 19:17:41 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: convert seconds to minutes & hours
Message-Id: <3F3D31BE.C7B8181A@acm.org>
Erik Waibel wrote:
>
> I know this is a simple question, but I just can't seem to find the answer
> anywhere. Could someone please help me to convert seconds into hours :
> minutes : seconds...say I have like 1080 seconds, I want this to return 0
> hours, 18 minutes, and 0 seconds.
my ( $sec, $min, $hour ) = gmtime 1080;
John
--
use Perl;
program
fulfillment
------------------------------
Date: 15 Aug 2003 17:49:10 GMT
From: mooseshoes <mooseshoes@gmx.net>
Subject: Does inet_aton() work for 'localhost'?
Message-Id: <bhj6em$bo2@dispatch.concentric.net>
All:
I am writing a UDP server socket and the following code runs but produces
the following output:
My host is: localhost
The ipaddress is:
The port address is:
#-----
#CODE <snip>
use strict;
use warnings;
use Socket;
use Sys::Hostname;
my $server_port = 7082;
my $message = "This is a test message from the server";
socket(SockHandle, PF_INET, SOCK_DGRAM, getprotobyname("udp"))
|| die "socket: $!";
my $host = hostname();
print "My host is: $host\n";
my $ipaddr = inet_aton($host);
print "The ipaddress is: $ipaddr\n";
my $portaddr = sockaddr_in($server_port, $ipaddr);
print "The port address is: $portaddr\n";
send(SockHandle, $message, 0, $portaddr) == length($message)
|| die "cannot send to localhost($portaddr): $!";
# end CODE <snip>
I am curious why inet_aton() doesn't seem to output the desired result in
this case.
Thanks in advance,
Moose
------------------------------
Date: 15 Aug 2003 18:23:16 GMT
From: Nicholas Dronen <ndronen@io.frii.com>
Subject: Re: Does inet_aton() work for 'localhost'?
Message-Id: <3f3d2514$0$193$75868355@news.frii.net>
mooseshoes <mooseshoes@gmx.net> wrote:
m> All:
m> I am writing a UDP server socket and the following code runs but produces
m> the following output:
m> My host is: localhost
m> The ipaddress is:
m> The port address is:
m> #-----
m> #CODE <snip>
m> use strict;
m> use warnings;
m> use Socket;
m> use Sys::Hostname;
m> my $server_port = 7082;
m> my $message = "This is a test message from the server";
m> socket(SockHandle, PF_INET, SOCK_DGRAM, getprotobyname("udp"))
m> || die "socket: $!";
m> my $host = hostname();
m> print "My host is: $host\n";
m> my $ipaddr = inet_aton($host);
m> print "The ipaddress is: $ipaddr\n";
m>
m> my $portaddr = sockaddr_in($server_port, $ipaddr);
m> print "The port address is: $portaddr\n";
m>
m> send(SockHandle, $message, 0, $portaddr) == length($message)
m> || die "cannot send to localhost($portaddr): $!";
m> # end CODE <snip>
m> I am curious why inet_aton() doesn't seem to output the desired result in
m> this case.
Because inet_aton() converts a human-readable hostname or IP
address to network byte order.
#!/usr/bin/perl
$|++;
use strict;
use warnings;
use Socket;
my $ipaddr = inet_aton("www.perl.org") or die "can't resolve hostname\n";
print "The ipaddress is: ", inet_ntoa($ipaddr), "\n";
__END__
$ ./foo
The ipaddress is: 63.251.223.172
Regards,
Nicholas
--
"Why shouldn't I top-post?" http://www.aglami.com/tpfaq.html
"Meanings are another story." http://www.ifas.org/wa/glossolalia.html
------------------------------
Date: Fri, 15 Aug 2003 16:04:29 GMT
From: Brian Harnish <bharn_S_ish@te_P_chnologi_A_st._M_com>
Subject: Re: Encrypting a superuser
Message-Id: <pan.2003.08.15.16.05.12.48452@te_P_chnologi_A_st._M_com>
On Fri, 15 Aug 2003 08:43:16 +0100, JJ (UK) wrote:
> Is it possible to specify a 'superuser' (Domain Admin) account name with the
> password encrypted within a Perl script in order to run a BATCH script with
> the required level of permissions?
>
> I've written the batch scripts (and they're pretty cool!) but for them to
> work the account running them needs Domain Admin rights, however the team
> they've been written for are only Account Operators. You see my problem...
>
> TIA
That's a really bad idea. Try something else, such as "sudo" if on
Unix/Linux. It would be too easy for someone to decrypt the password if
its in the script.
------------------------------
Date: 15 Aug 2003 09:05:01 -0700
From: nobull@mail.com
Subject: Re: Encrypting a superuser
Message-Id: <4dafc536.0308150805.3c99b78b@posting.google.com>
"JJ \(UK\)" <gee@i.cant.think.why.you'd.want.my.email.address.com> wrote in message news:<w80%a.24$mD5.19@newsfep1-gui.server.ntli.net>...
> Is it possible to specify a 'superuser' (Domain Admin) account name with the
> password encrypted within a Perl script in order to run a BATCH script with
> the required level of permissions?
Not in any useful sense. You are looking for a perpetual motion
machine. This has nothing to do with Perl.
> I've written the batch scripts (and they're pretty cool!) but for them to
> work the account running them needs Domain Admin rights, however the team
> they've been written for are only Account Operators. You see my problem...
Yes it is a common problem. You seek a way whereby a user can start a
process that has elevated priviledges. It has to be overcome at the
OS level not the language level. In Unix you have set-uid scripts.
In Windows I don't think there's any directly equivalent concept as
standard and I've spend quite some time looking. The best I've
usually managed is to use a Windows service.
By the way, did I mention that this has nothing to do with Perl.
------------------------------
Date: 15 Aug 2003 10:00:30 -0700
From: viijv@thedifferenZ.com (Vijoy Varghese)
Subject: Finding 'path to perl' of remote server
Message-Id: <4c08aaff.0308150900.3a951b24@posting.google.com>
Hello Group,
I was wondering if it's possible to find the 'path to perl' on a
remote server.
This is what I want. I want to install a CGI script automatically on a
remote webserver using FTP. For this I am going to use the perl module
Net::FTP. After reading the manual of this module I found that almost
everything about installing a CGI script can be automated [Create
directory, put a cgi file, chmod it], but how to get the 'path to
perl' of that remote server? If there is some way to get it then I can
add that as the first line of the cgi file, and thus can achieve my
goal of 'automatic remote CGI/Perl script installation'.
Actually I never checked if all other parts[mkdir, cp, chmod] is
practically possible using this module, because there is no meaning in
doing those unless I can find some way to find the 'path to perl'.
So is 'finding path to perl on remote server' something really
possible? Or is this just a "DREAM"?
Thanking you all in advance
Regards
Vijoy Varghese
------------------------------
Date: 15 Aug 2003 19:26:19 GMT
From: Rafael Garcia-Suarez <rgarciasuarez@free.fr>
Subject: Re: Finding 'path to perl' of remote server
Message-Id: <slrnbjqdaf.csd.rgarciasuarez@dat.local>
Vijoy Varghese wrote in comp.lang.perl.misc :
> I was wondering if it's possible to find the 'path to perl' on a
> remote server.
Perhaps a solution to your problem is to put
#!/usr/bin/env perl
at the 1st line of your perl programs (if you have env(1)). See the
perlrun manpage.
--
Unicycle is not *NIX
------------------------------
Date: 15 Aug 2003 11:18:16 -0700
From: jnokes1@yahoo.com (John N.)
Subject: Finding the size of a large file
Message-Id: <834a05f6.0308151018.72e4cb70@posting.google.com>
I am new to Perl. I have a Sun box running Solaris 8 and the Perl that
comes with the OS.
I pulled some code off of Sun's site that searches a directory tree
and finds big files. When I run the program it works except that it
does not find files over 5 GB.
I have done a few things to trouble shoot the code and I found that it
fails in two places. On the line of "elsif ((-f _) && (! -l $path))"
the file fails the -f switch test. When I do a ls -l on the directory
it shows that the file is a normal file with the same rights as the
other files in the directory. Then I took "-f _" logic check out and
printed the result of the "$size = -s $path;" code and the -s returns
a blank for the size.
Do I have to do differant things with large files? Are there bugs or
limitations on the -f and -s switches? How do I make the code work?
Thanks for your help,
John
#
# scan throught the directory tree
#
&traverse('.');
sub traverse {
local($dir) = shift;
local($path);
unless (opendir(DIR, $dir)) {
warn "Can't open $dir\n";
closedir(DIR);
return;
}
foreach (readdir(DIR)) {
next if $_ eq '.' || $_ eq '..';
$path = "$dir/$_";
if ((-d $path) && (! -l $path)) { # non-symlink dir, enter it
&traverse($path);
} elsif ((-f _) && (! -l $path)) { # plain file, but not a
symlink
$size = -s $path; # get the size in bytes
$ksize = $size / 1000; # convert to megabytes
if ($size > $minsize) {
$age = -A $path; # get the age in days
printf "%9d Kilobytes %4d days
%s\n",$ksize,int($age),$path;
}
}
}
closedir(DIR);
}
------------------------------
Date: Fri, 15 Aug 2003 19:43:30 GMT
From: "Michael P. Broida" <michael.p.broida@boeing.com>
Subject: Re: Finding the size of a large file
Message-Id: <3F3D37E2.3AC29A6C@boeing.com>
"John N." wrote:
>
> I am new to Perl. I have a Sun box running Solaris 8 and the Perl that
> comes with the OS.
>
> I pulled some code off of Sun's site that searches a directory tree
> and finds big files. When I run the program it works except that it
> does not find files over 5 GB.
The largest number that can be held in a 32-bit integer
(unsigned) is 4,294,967,295.
That's 4.29+ Gig. Since you mention 5GB in your post,
I'm thinking that the size is beyond what the code
can handle.
If I'm correct, then it's seeing that 5GB file as
being 5,000,000,000 - 4,294,967,295 = 705,032,705
or about 705MB. If you were searching for files
larger than 700MB, this one should still show up.
But if you're looking for files over 1GB, it won't.
Mike
------------------------------
Date: 15 Aug 2003 20:13:47 GMT
From: Nicholas Dronen <ndronen@io.frii.com>
Subject: Re: Finding the size of a large file
Message-Id: <3f3d3efb$0$191$75868355@news.frii.net>
John N. <jnokes1@yahoo.com> wrote:
JN> I am new to Perl. I have a Sun box running Solaris 8 and the Perl that
JN> comes with the OS.
JN>
JN> I pulled some code off of Sun's site that searches a directory tree
JN> and finds big files. When I run the program it works except that it
JN> does not find files over 5 GB.
JN> I have done a few things to trouble shoot the code and I found that it
JN> fails in two places. On the line of "elsif ((-f _) && (! -l $path))"
JN> the file fails the -f switch test. When I do a ls -l on the directory
JN> it shows that the file is a normal file with the same rights as the
JN> other files in the directory. Then I took "-f _" logic check out and
JN> printed the result of the "$size = -s $path;" code and the -s returns
JN> a blank for the size.
JN> Do I have to do differant things with large files? Are there bugs or
JN> limitations on the -f and -s switches? How do I make the code work?
Is there any mention of large files in the output of perl -V:ccflags?
Regards,
Nicholas
--
"Why shouldn't I top-post?" http://www.aglami.com/tpfaq.html
"Meanings are another story." http://www.ifas.org/wa/glossolalia.html
------------------------------
Date: Fri, 15 Aug 2003 05:33:30 GMT
From: merlyn@stonehenge.com (Randal L. Schwartz)
To: calle@lysator.liu.se
Subject: Re: Help! Regular expression on html...
Message-Id: <8534bdeda79eade6682da761aea664bf@news.teranews.com>
>>>>> "Calle" == Calle Dybedahl <calle@lysator.liu.se> writes:
Calle> #!/usr/bin/perl
Calle> use strict;
Calle> use warnings;
Calle> use XML::LibXML;
Calle> my $parser = XML::LibXML->new;
$parser->recovering(1); # or else <a href="/cgi/foo?a=b&c=d"> fails
Calle> foreach my $doc (@ARGV) {
Calle> my $tree = $parser->parse_html_file($doc);
Calle> my $root = $tree->getDocumentElement;
Calle> foreach my $link ($root->findnodes("//a")) {
Calle> $link->removeChildNodes;
Calle> $link->appendTextNode("(link)");
Calle> }
Calle> print $tree->toStringHTML;
Calle> }
Otherwise, nice solution! Who needs XSLT, when you've got Perl and
LibXML!
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: Fri, 15 Aug 2003 14:59:31 -0400
From: Chris Mattern <syscjm@gwu.edu>
Subject: Re: Help! Regular expression on html...
Message-Id: <3F3D2D93.4060502@gwu.edu>
Paul Neave wrote:
> Thanks guys. I know I should use the XML method, but I think I'll
> stick to the regexp version for now.
>
> Here's what I used, and I had to tweak it a bit:
>
> s/(<\s?a\s{0,2}href=".*?"\s?>).*?(<\s?\/a\s?>)/$1(link)$2/ig;
>
>
> When I took out the \s? parts, it fell over and wouldn't strip my
> tags for some reason. Also, I used $2 not $3 and that fixed it.
>
> The links are server-side generated, so they will always be well
> formed anchor tags. I just needed another script to clean them up.
> So should my version work OK?
>
Trying to parse HTML with regexps is a very, very *bad* idea. There
are packages for parsing HTML, which should be used. Calle Dybedahl
and Randal Schwartz (no less!) have given a good example of how to
do it with XML::LibXML in a different branch of this thread. You
should go look at it.
Chris Mattern
------------------------------
Date: 15 Aug 2003 10:52:03 -0700
From: jwong1@mail.arc.nasa.gov (James)
Subject: How to create a Perl module to be used in a CGI script
Message-Id: <e4d1132a.0308150952.2d5703e8@posting.google.com>
Hi,
I have the following:
filename: Module1.pm
package Module1;
BEGIN {
use Exporter();
@ISA = qw(Exporter);
@EXPORT = qw(&subroutine1);
}
sub subroutine1 {print "Hello!\n";}
return 1;
END { }
---------------------------------------------------
Filename: Trial.cgi running in Tomcat 4.1.27
#!c:\Perl\bin\perl.exe
use CGI
use Module1;
subroutine1();
.
.
.
.
When I run the script from my html page it asks for a file download. What am
I doing wrong?
Thanks, James
------------------------------
Date: Fri, 15 Aug 2003 18:58:41 GMT
From: Brian Harnish <bharn_S_ish@te_P_chnologi_A_st._M_com>
Subject: Re: How to create a Perl module to be used in a CGI script
Message-Id: <pan.2003.08.15.18.59.16.62243@te_P_chnologi_A_st._M_com>
On Fri, 15 Aug 2003 10:52:03 -0700, James wrote:
> Hi,
>
> I have the following:
>
> filename: Module1.pm
>
> package Module1;
>
> BEGIN {
> use Exporter();
> @ISA = qw(Exporter);
> @EXPORT = qw(&subroutine1);
> }
>
> sub subroutine1 {print "Hello!\n";}
> return 1;
> END { }
> ---------------------------------------------------
> Filename: Trial.cgi running in Tomcat 4.1.27
>
> #!c:\Perl\bin\perl.exe
>
> use CGI
> use Module1;
>
> subroutine1();
> .
> .
> .
> .
>
>
> When I run the script from my html page it asks for a file download. What am
> I doing wrong?
>
> Thanks, James
Doesn't look like you outputed any headers. Try at least putting out a
content-type. see "perldoc CGI" for how.
- Brian
------------------------------
Date: 15 Aug 2003 13:49:04 -0700
From: yi_mang@yahoo.com (Yi Mang)
Subject: How to express "not followed by"?
Message-Id: <832301af.0308151249.71aca6e8@posting.google.com>
Hi,
My question is regarding to the regexp "not followed by", for example,
if a string has a number followed by a semi-colon, then pass;
otherwise, print an error. This is what I have constructed:
if ( /\d+([^;]) ) # A number followed by non-semicolon
{
print "error\n";
}
else
{
print "pass\n";
}
I tested this on the following string "abc123;edf", it's supposed to
pass, but it did not. It turned out that the expression thinks 12
followed 3, not semicolon, so it's an error. I get around around this
problem if I constructed the if statement this way:
if ( /\d+([^;0123456789]) )
but I think there should be an easy way to do this. Could you help me
out?
One more related question, what if I want to check the number is not
followed by more than a character? for example, if 123 is followed by
abc, then it's ok; otherwise, print error. So "123abd" will generate
an error message.
Thanks for all your time and help.
Y
------------------------------
Date: Fri, 15 Aug 2003 23:13:38 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: How to express "not followed by"?
Message-Id: <pan.2003.08.15.21.13.38.329532@aursand.no>
On Fri, 15 Aug 2003 13:49:04 -0700, Yi Mang wrote:
> My question is regarding to the regexp "not followed by", for example,
> if a string has a number followed by a semi-colon, then pass;
> otherwise, print an error.
Hmm. If I understand you right, the solution should be pretty
straightforward?
if ( /\d;/ ) {
# Pass
}
else {
# Error
}
Or?
--
Tore Aursand <tore@aursand.no>
------------------------------
Date: Fri, 15 Aug 2003 23:17:30 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: How to express "not followed by"?
Message-Id: <bhjilm$826v$1@ID-184292.news.uni-berlin.de>
Yi Mang wrote:
> My question is regarding to the regexp "not followed by", for
> example, if a string has a number followed by a semi-colon, then
> pass; otherwise, print an error. This is what I have constructed:
> if ( /\d+([^;]) ) # A number followed by non-semicolon
> {
> print "error\n";
> }
> else
> {
> print "pass\n";
> }
>
> I tested this on the following string "abc123;edf", it's supposed
> to pass, but it did not. It turned out that the expression thinks
> 12 followed 3, not semicolon, so it's an error.
Yes, it looks for "one or more digits followed by something else but a
semicolon", and, as you have noticed, it found that.
In this case, I suppose it's easiest to change logic:
print /\d+;/ ? "pass\n" : "error\n";
> One more related question, what if I want to check the number is
> not followed by more than a character? for example, if 123 is
> followed by abc, then it's ok; otherwise, print error. So "123abd"
> will generate an error message.
http://www.perldoc.com/perl5.8.0/pod/perlre.html#(-%3dpattern)
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Fri, 15 Aug 2003 14:26:08 -0700
From: "David Oswald" <spamblock@junkmail.com>
Subject: Re: How to express "not followed by"?
Message-Id: <vjqkfpm0dm0nda@corp.supernews.com>
"Yi Mang" <yi_mang@yahoo.com> wrote in message
news:832301af.0308151249.71aca6e8@posting.google.com...
> Hi,
> My question is regarding to the regexp "not followed by", for example,
> if a string has a number followed by a semi-colon, then pass;
> otherwise, print an error.
The regexp could look something like this:
/\d+(?!;)/
That would match one or more numeric digits as long as they're not followed
by a semicolon.
It could be used like this:
#!/usr/bin/perl
use strict;
use warnings;
my @test_strings = ( "Test1357;" , "Test2468" );
foreach ( @test_strings ) {
if ( $_ =~ /\d+(?!;)/ ) {
print "$_ matched as $&\n";
} else {
print "$_ didn't match.\n";
}
}
If you want to capture the number, it would be:
/(\d+)(?!;)/
And the portion that matched would now be captured in $1. Of course in this
simple example, $& also contains what matched. Lookaheads are never
considered part of the match, they're simply considered part of the criteria
that allowed the match.
Dave
------------------------------
Date: Fri, 15 Aug 2003 21:43:37 GMT
From: Xaonon <xaonon@hotpop.com>
Subject: Re: How to express "not followed by"?
Message-Id: <slrnbjqvh4.1tu.xaonon@xaonon.local>
Ned i bach <832301af.0308151249.71aca6e8@posting.google.com>, Yi Mang
<yi_mang@yahoo.com> teithant i thiw hin:
> My question is regarding to the regexp "not followed by", for example, if
> a string has a number followed by a semi-colon, then pass; otherwise,
> print an error. This is what I have constructed:
>
> if ( /\d+([^;]) ) # A number followed by non-semicolon
> {
> print "error\n";
> }
> else
> {
> print "pass\n";
> }
>
> I tested this on the following string "abc123;edf", it's supposed to
> pass, but it did not.
No, it wouldn't. Think about the pattern you're trying to match: any
positive number of digits, followed by anything that isn't a semicolon. The
match is greedy, so it tries to gobble up as much of the string as possible.
So the digits part matches "12", and the not-a-semicolon part matches "3".
What you should be doing is looking for digits followed by a semicolon, and
produce an error if that *can't* be found. Like this:
if( /\d+;/ )
{
print "pass\n";
}
else
{
print "error";
}
--
Xaonon, EAC Chief of Mad Scientists and informal BAAWA, aa #1821, Kibo #: 1
Visit The Nexus Of All Coolness (i.e. my site) at http://xaonon.dyndns.org/
"Why should I perspire to death on the subway, when I could be flying around
in Dick Cheney's invisible nuclear helicopter or whatever?" -- mnftiu.cc
------------------------------
Date: Fri, 15 Aug 2003 23:50:38 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: How to express "not followed by"?
Message-Id: <bhjkjr$aa4h$1@ID-184292.news.uni-berlin.de>
David Oswald wrote:
> The regexp could look something like this:
>
> /\d+(?!;)/
>
> That would match one or more numeric digits ...
-------------------^^^^^^^^^^^
Yes... And it would match 'abc123;edf' just fine - for the same reason
as OP's /\d+[^;]/ matches, i.e. "one or more".
So your solution cannot work.
If the number of digits is fixed, it's easy: Then both /\d{3}[^;]/ and
/\d{3}(?!;)/ fails.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Fri, 15 Aug 2003 19:37:57 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: parsing log in multiple passes
Message-Id: <3F3D367E.B0A8B266@acm.org>
disco wrote:
>
> Thanks for your ideas and feedback. I have gotten some good ideas and
> learned some new concepts already.
>
> I cannot run either of your examples with warnings turned on and the
> results are not correct. I cut and pasted your code onto my
> workstation. Is this the correct version of your code?
Yes, I copied and pasted working code using your data to test it.
> I would like to get the second example working. This is what I get:
>
> Use of uninitialized value in sort at c:\temp\example2.pl line 19,
> <$fh> chunk 1.
> Use of uninitialized value in sort at c:\temp\example2.pl line 19,
> <$fh> chunk 1.
> Use of uninitialized value in sort at c:\temp\example2.pl line 19,
> <$fh> chunk 1.
> Use of uninitialized value in sort at c:\temp\example2.pl line 19,
> <$fh> chunk 1.
> Use of uninitialized value in hash element at c:\temp\example2.pl
> line 21, <$fh> chunk 1.
> Use of uninitialized value in hash element at c:\temp\example2.pl
> line 21, <$fh> chunk 1.
> $VAR1 = {
> '' => {
> '' => 'Card name',
> 'Firmware Version' => '4.234',
> 'BitBlaster' => 'Firmware Version',
> 'Driver Version' => '7.2345',
> 'Alpha' => 'Port type',
> 'Ethernet' => 'Port speed',
> '5.000' => 'Driver Version',
> '24550' => 'Number of Ports',
> 'Card name' => 'SuperWombat',
> 'Node Id' => '00000',
> 'Number of Ports' => 2,
> '0' => {},
> '1' => {},
> 'Port name' => 'GigE1',
> '10' => '',
> '8.000 rel 5.02' => 'Node Id',
> 'Port type' => 'Ethernet',
> 'Port speed' => '1000',
> 'Port Id' => '10200',
> '1a3c5f' => 'Port name'
> }
> };
> Press any key to continue . . .
It looks like you have a colon in one of the fields. This will parse
the fields correctly:
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
my $file = 'cli.log';
open my $fh, '<', $file or die "Cannot open $file: $!";
local $/ = ''; # paragraph mode
my %data;
while ( <$fh> ) {
my %temp = /^\s*([^:]+?)\s*:\s*(.*\S)/gm;
my ( @keys, @ports );
for my $port ( 1 .. $temp{ 'Number of Ports' } ) {
push @ports, { <$fh> =~ /^\s*([^:]+?)\s*:\s*(.*\S)/gm };
push @keys, $ports[ -1 ]{ 'Port Id' };
}
@keys = sort @keys;
$temp{ 'Node Id' } == 0
? $data{ $keys[ 0 ] }
: $data{ $temp{ 'Node Id' } }
= { %temp, map { $_, $ports[ $_ ] } 0 .. $#ports };
}
print Dumper( \%data );
__END__
John
--
use Perl;
program
fulfillment
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
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.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.
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 V10 Issue 5367
***************************************