[18129] in Perl-Users-Digest
Perl-Users Digest, Issue: 289 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 14 14:10:49 2001
Date: Wed, 14 Feb 2001 11:10:23 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <982177822-v10-i289@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 14 Feb 2001 Volume: 10 Number: 289
Today's topics:
Re: How to scan spaces from a string of characters? <joe+usenet@sunstarsys.com>
Re: How to scan spaces from a string of characters? (Anno Siegel)
Re: How to scan spaces from a string of characters? <cindyann@mbox3.singnet.com.sg>
Re: How to scan spaces from a string of characters? <mischief@velma.motion.net>
Re: Is there a tutorial in XML::Parser egwong@netcom.com
Re: Is there a tutorial in XML::Parser <gerard@NOSPAMlanois.com>
Re: links in cgi? nobull@mail.com
Re: localtime -> reverse -> localtime <bart.lateur@skynet.be>
Newbie : Help <stan@olc.win.net>
Newbie: How do I make my home Win2k IIS system run Perl <DavePerkins@bigfoot.com>
OT:status of the reorg of The Perl Journal <mothra@nowhereatall.com>
Re: print << END; <bart.lateur@skynet.be>
Re: Problems with ActiveState Perl, please help. <henroc@mindspring.com>
regex experts <ducateg@info.bt.co.uk>
Re: regex experts <uri@sysarch.com>
Re: regex experts (Mahesh A)
Re: regex experts nobull@mail.com
remote IP Address (David W LeJeune)
Re: remote IP Address (Abigail)
Re: Replacing single-letter words doesn't work <uri@sysarch.com>
Re: setting degree of parallelism (Anno Siegel)
split Qustion <duxiao@ufr-info-p7.jussieu.fr>
Re: split Qustion <uri@sysarch.com>
Re: split Qustion nobull@mail.com
Re: Strings and pointers in Perl? <bart.lateur@skynet.be>
using getc to input user response from keyboard <bradgwinn@hotmail.com>
Re: using getc to input user response from keyboard <aqumsieh@hyperchip.com>
Re: using getc to input user response from keyboard <gus@black.hole-in-the.net>
Re: Why can't I grab this URL? (Randal L. Schwartz)
XS -- "array subscript is not an integer" <jfeuerst@eecs.tufts.edu>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 14 Feb 2001 09:20:34 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: How to scan spaces from a string of characters?
Message-Id: <m3lmr9ibwt.fsf@mumonkan.sunstarsys.com>
"cindy" <cindyann@mbox3.singnet.com.sg> writes upside down:
[ In the future, please put your reply under the suitably
trimmed article you are responding to. Thanks. ]
> I have tried wrap but it break it in the following way.
>
> Output:
> Line1 = "How are you today? Have you ta"
> Line2 = "ken lunch?"
>
> Input:
> How are you today? Have you taken lunch?
>
> Therefore, I need something to tell me that it break in a
> full words and not "ta" in the 1st line and "ken" in the 2nd line.
Have you considered using format/write/formline? Here's an
excerpt from perlform:
% man perlform
...
Or to make an swrite() subroutine, which is to write()
what sprintf() is to printf(), do this:
use Carp;
sub swrite {
croak "usage: swrite PICTURE ARGS" unless @_;
my $format = shift;
$^A = "";
formline($format,@_);
return $^A;
}
Try
print swrite <<END, $_ = "How are you today? Have you taken lunch?";
^<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
END
Output:
How are you today? Have you
taken lunch?
HTH
Joe Schaefer
--
print in_str(shift, shift); #prints number of times $ARGV[1] occurs in $ARGV[0]
use Inline C => q{ int in_str(SV *x, SV *y){int cnt=0; char *hay=SvPV(x,PL_na);
char *ndl=SvPV(y,PL_na);if(!SvPOK(x)||!SvPOK(y)){return 0;}hay=strstr(hay,ndl);
while( hay != NULL ){ ++cnt; hay = strstr( hay + 1, ndl ); } return( cnt ); } }
------------------------------
Date: 14 Feb 2001 14:38:08 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: How to scan spaces from a string of characters?
Message-Id: <96e58g$edm$2@mamenchi.zrz.TU-Berlin.DE>
[jeopardy posting rectified]
cindy <cindyann@mbox3.singnet.com.sg> wrote in comp.lang.perl.misc:
>Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote in article
><96e190$8f5$1@mamenchi.zrz.TU-Berlin.DE>...
>> cindy <cindyann@mbox3.singnet.com.sg> wrote in comp.lang.perl.misc:
>> >hi,
>> >
>> >How do I break a string into two lines using Perl and my line will.
>> >not appear like the following.
>> >
>> >Line1 = "How are you today? Have you ta"
>> >Line2 = "ken lunch?"
>>
>> We frequently see problem descriptions (here, and without doubt in
>> other technical newsgroups as well) that try to get away with giving
>> an example of the input and the output. This usually leaves a lot
>> of guesswork to the unfortunate reader. Here we have a problem
>> description that gives no input and an example of what the output
>> should not look like. Well, lets start guessing...
>>
>> The Text::Wrap module is probably what you're looking for.
>I have tried wrap but it break it in the following way.
>
>Output:
>Line1 = "How are you today? Have you ta"
>Line2 = "ken lunch?"
>
>Input:
>How are you today? Have you taken lunch?
>
>Therefore, I need something to tell me that it break in a
>full words and not "ta" in the 1st line and "ken" in the 2nd line.
Which "wrap" have you tried, and how did you call it?
If you want to get along here, please observe two points:
Post your replies interleaved with and after what you are replying to.
Also, trim the quoted text so that it contains only what is really
necessary for context.
Dumping your comments at the beginning of the posting and letting the
reader sort it out isn't received well around here.
Secondly, be specific in what you say. Your claim that wrap() treats
the text the way you indicate either means that you have used a
different "wrap" function from that in the module Text::Wrap, or
you have found a way to call Text::Wrap::wrap() so that it produces
your result. The latter may be possible, but it is by no means
trivial. If you had shown the way you called wrap() in a short
code example we would now be a step further.
Anyway, here is what Text::Wrap does to your line if used in the
straightforward manner:
#!/usr/bin/perl
use strict; use warnings; $| = 1;
use Text::Wrap qw( wrap $columns);
$columns = 30;
my $str = "How are you today? Have you taken lunch?\n";
print wrap( '', '', $str);
__END__
This prints:
How are you today? Have you
taken lunch?
Anno
------------------------------
Date: 14 Feb 2001 16:57:17 GMT
From: "cindy" <cindyann@mbox3.singnet.com.sg>
Subject: Re: How to scan spaces from a string of characters?
Message-Id: <01c096a8$aa2382e0$2cb115a5@default>
hi Joe,
You know what happen? The two output lines that I have are supposed to
contain only 30 characters each. Therefore, by using
format/write/formline/wrap
cannot solve my problem. They may be able to break the line properly
but they can't break it in a way that it fit into the two output lines that
contain
only 30 characters.
And I don't understand why you say that I wrote upside down. My e_mail tool
is the one that pushes all the old message down.
Thanks.
Cindy
Joe Schaefer <joe+usenet@sunstarsys.com> wrote in article
<m3lmr9ibwt.fsf@mumonkan.sunstarsys.com>...
> "cindy" <cindyann@mbox3.singnet.com.sg> writes upside down:
>
> [ In the future, please put your reply under the suitably
> trimmed article you are responding to. Thanks. ]
>
> > I have tried wrap but it break it in the following way.
> >
> > Output:
> > Line1 = "How are you today? Have you ta"
> > Line2 = "ken lunch?"
> >
> > Input:
> > How are you today? Have you taken lunch?
> >
> > Therefore, I need something to tell me that it break in a
> > full words and not "ta" in the 1st line and "ken" in the 2nd line.
>
> Have you considered using format/write/formline? Here's an
> excerpt from perlform:
>
> % man perlform
> ...
> Or to make an swrite() subroutine, which is to write()
> what sprintf() is to printf(), do this:
>
> use Carp;
> sub swrite {
> croak "usage: swrite PICTURE ARGS" unless @_;
> my $format = shift;
> $^A = "";
> formline($format,@_);
> return $^A;
> }
>
> Try
>
> print swrite <<END, $_ = "How are you today? Have you taken lunch?";
> ^<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
> END
>
> Output:
>
> How are you today? Have you
> taken lunch?
>
> HTH
>
> Joe Schaefer
> --
> print in_str(shift, shift); #prints number of times $ARGV[1] occurs in
$ARGV[0]
> use Inline C => q{ int in_str(SV *x, SV *y){int cnt=0; char
*hay=SvPV(x,PL_na);
> char *ndl=SvPV(y,PL_na);if(!SvPOK(x)||!SvPOK(y)){return
0;}hay=strstr(hay,ndl);
> while( hay != NULL ){ ++cnt; hay = strstr( hay + 1, ndl ); } return( cnt
); } }
>
------------------------------
Date: Wed, 14 Feb 2001 18:32:58 -0000
From: Chris Stith <mischief@velma.motion.net>
Subject: Re: How to scan spaces from a string of characters?
Message-Id: <t8ljqqhhuvjg3a@corp.supernews.com>
cindy <cindyann@mbox3.singnet.com.sg> wrote:
> Joe Schaefer <joe+usenet@sunstarsys.com> wrote in article
> <m3lmr9ibwt.fsf@mumonkan.sunstarsys.com>...
>> "cindy" <cindyann@mbox3.singnet.com.sg> writes upside down:
>>
>> [ In the future, please put your reply under the suitably
>> trimmed article you are responding to. Thanks. ]
> hi Joe,
> You know what happen? The two output lines that I have are supposed to
> contain only 30 characters each. Therefore, by using
> format/write/formline/wrap
> cannot solve my problem. They may be able to break the line properly
> but they can't break it in a way that it fit into the two output lines that
> contain
> only 30 characters.
How about splitting your input on spaces into an array, counting the length
of the elements (plus the spaces), then print out the information an element
at a time until you would go over 30 characters?
The following has problems with long URLs or such that have more than 30
non-space characters consecutively.
#---------------------------
#!/usr/bin/perl -w
use strict;
my $input = 'This line may reach over 30 characters long.';
my @word = split /\s+/, $input;
my $count = 0;
foreach(@word) {
$_ .= ' ';
$count += length;
if( $count == 30 ) {
chop;
print $_ . "\n";
$count = 0;
} elsif( $count > 30 ) {
print "\n" . $_;
$count = length;
} else {
print $_;
}
}
#---------------------------
This may not be the prettiest solution, and you didn't mention whether or
not it was okay to play with the spaces between words other than at the
line breaks.
You could make the above into a subroutine or put it into a loop if needed,
of course. As is, it only works on one input. You could also move the
literal 30 into a variable for future maintenance or code reuse.
The following doesn't do things very prettily at all. You have been warned
not to cut and paste it as is! :-/
It treats the input file a line at a time, and makes sure the lines are
wrapped at 30 characters. This can make for ugly little short lines. To
fix this, you could change the figuring of $count to work across call to
the subroutine or you could slurp the whole file before applying the wrap.
#-------------------------------------
#!/usr/bin/perl -w
use strict;
while( <> ) {
my $output = my_wrap( $_ );
print $output;
}
sub my_wrap($) {
my $input = shift;
my @word = split /\s+/, $input;
my $result = '';
my $count = 0;
foreach(@word) {
$_ .= ' ';
$count += length;
if( $count == 30 ) {
chop;
$result .= $_ . "\n";
$count = 0;
} elsif( $count > 30 ) {
$result .= "\n" . $_;
$count = length;
} else {
$result .= $_;
}
}
return $result;
}
#------------------------------------
The following gives a bit better looking output, but still has problems
with more than 30 consecutive non-space characters, and you lose single
blank lines, with two consecutive blank lines being squished to one.
#--------------------------------------
#!/usr/bin/perl -w
use strict;
my $count = 0;
my $line_length = 30;
while( <> ) {
my $output = my_wrap( $_ );
print $output;
}
sub my_wrap($) {
my $input = shift;
my @word = split /\s+/, $input;
my $result = '';
foreach(@word) {
$_ .= ' ';
$count += length;
if( $count == $line_length ) {
chop;
$result .= $_ . "\n";
$count = 0;
} elsif( $count > $line_length ) {
$result .= "\n" . $_;
$count = length;
} else {
$result .= $_;
}
}
return $result;
}
#-----------------------------------------
These are not meant to be a complete gimmee. They give a (not the -- just
one of many possible) starting point from which you can continue to tweak
and test to meet your needs.
> And I don't understand why you say that I wrote upside down. My e_mail tool
> is the one that pushes all the old message down.
Gee, my newsreader calls my text editor. Text editors tend to start with the
cursor at the top of the file. I guess that means that I also shouldn't have
the common decency to hit pagedown a couple of times to make my posts clear.
By the way, guns really do kill people. Never mind the person behind the
trigger.
Cars really do kill people - especialy when they find a drunk driver inside
them. Never mind the person behind the wheel.
Let's all deny any responsibility for anything, because it's the tools that
do the deeds, not the people utilizing them.
BTW, this is Usenet, not a mail server. You're using a newsreader, not an
email client -- regardless of whether or not the two are integrated as one
program.
> Thanks.
> Cindy
It would be better thanks to post properly.
Chris
--
Christopher E. Stith
Parking for people we like only. All other vehicles will be vandalized.
------------------------------
Date: Wed, 14 Feb 2001 15:34:30 GMT
From: egwong@netcom.com
Subject: Re: Is there a tutorial in XML::Parser
Message-Id: <aAxi6.888$Sx5.89207@news.flash.net>
Andy Holyer <andyh@littoralis.co.uk> wrote:
> The time has come (the Walrus said...)
> I need to parse some XML. As I've discovered, the true way to do this is to
> use XML::Parser.
> Now, I think I follow what this module does, but I'm still a bit lost where
> to start. Has anyone produced a tutorial in how to use it?
A quick google search ("perl XML::Parser tutorial") turned up
http://www.xml.com/pub/a/98/09/xml-perl.html
There were a couple of others.
Also, you can always look at the XML::??? modules on CPAN as examples --
most of them seem to be based on XML::Parser. David Cross has come out
with a book _Data Munging with Perl_, which has a chapter on parsing
XML, including XML::Parser. I saw in at Tower yesterday and it looks
pretty good.
http://www.manning.com/cross/index.html
Good luck, ERic
------------------------------
Date: 14 Feb 2001 09:17:25 -0800
From: Gerard Lanois <gerard@NOSPAMlanois.com>
Subject: Re: Is there a tutorial in XML::Parser
Message-Id: <uae7pw5ei.fsf@NOSPAMlanois.com>
egwong@netcom.com writes:
> Andy Holyer <andyh@littoralis.co.uk> wrote:
> > The time has come (the Walrus said...)
>
> > I need to parse some XML. As I've discovered, the true way to do this is to
> > use XML::Parser.
>
> > Now, I think I follow what this module does, but I'm still a bit lost where
> > to start. Has anyone produced a tutorial in how to use it?
>
>
> A quick google search ("perl XML::Parser tutorial") turned up
> http://www.xml.com/pub/a/98/09/xml-perl.html
>
> There were a couple of others.
>
> Also, you can always look at the XML::??? modules on CPAN as examples --
> most of them seem to be based on XML::Parser. David Cross has come out
> with a book _Data Munging with Perl_, which has a chapter on parsing
> XML, including XML::Parser. I saw in at Tower yesterday and it looks
> pretty good.
> http://www.manning.com/cross/index.html
See also libxml-perl. These are supporting modules which either assist
you in writing your own handlers, or pre-written handlers.
I've found these modules to be very helpful, especially
XML::PatAct::ToObjects.
The URL is
http://bitsko.slc.ut.us/libxml-perl/
The documentation there is complete. I mention this because
the documentation on CPAN has many dead links:
http://search.cpan.org/doc/KMACLEOD/libxml-perl-0.07/doc/index.html
-Gerard
http://www.lanois.com/perl/
------------------------------
Date: 14 Feb 2001 17:44:07 +0000
From: nobull@mail.com
Subject: Re: links in cgi?
Message-Id: <u97l2tgnx4.fsf@wcl-l.bham.ac.uk>
hefe@in-time.se writes:
> I have a "vision", but haven't found anything like this in the docs...
>
> I have a cgi script which prints out links for other webpages. Currently, I'm
> doing this as
> print '<a href.......</a>';
>
> Is there an alternate way of doing this using cgi?
By "using cgi" I assume you mean "using CGI.pm".
In most cases (including <A>):
print '<foo bar=blat>wibble</foo>';
Can be re-written:
print $cgi->foo({ bar => 'blat' }, 'wibble');
Where $cgi is your CGI object.
Note: 'blat' is auotmatically subjected to the appropriate escaping.
If you are using the non-OO interface to CGI.pm then drop the $cgi->.
For details RTFM.
There is nothing wrong with simply ignoring the HTML generation side
of CGI.pm and using HTML::Element or simply literal print statements.
> And, more importantly, is there a way to print out your own error message if
> the page can not be accessed, instead of getting a "404" from the
> server where the file used to be?
Configure your web server to perform some action in place if error 404.
This has nothing to do with Perl, see server documentation.
The exensions to the CGI API to pass error details to scripts that are
being called in an error context are non-standard and differ between
servers.
Currently CGI.pm does nothing to protect you from these differences.
For example under IIS:
$ENV{QUERY_STRING} = '404;http://server.example.com/~bam/some/file/that/does/not/exist'
Under Apache:
$ENV{REDIRECT_ERROR_NOTES} = 'File does not exist: /home/bam/public_html/some/file/that/does/not/exist'
$ENV{REDIRECT_URL} = '/~bam/some/file/that/does/not/exist'
$ENV{REDIRECT_REQUEST_METHOD} = 'GET'
$ENV{REDIRECT_STATUS} = 404
> If it's possible, I would like two things to happen if a file is not found:
> - error message to the user, in the browser
> - error message emailed to me, so I can take care of the problem
>
> Can this be accomplished?
Yes.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 14 Feb 2001 16:48:37 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: localtime -> reverse -> localtime
Message-Id: <hndl8ts3m59gv78ivpns11tbt4ebn976p1@4ax.com>
Marc Roos wrote:
>I am using localtime to change a time in seconds to a time seperated in
>sec min hour day month and year.
>But I would like to go back from sec min .. year to a time in seconds.
Check out timelocal(). It's on your hard disk... if you have Perl.
p.s. Yes, that's a play on words: timelocal() is the reverse of
localtime().
--
Bart.
------------------------------
Date: Wed, 14 Feb 2001 11:30:10 -0400
From: "Stan Sainte-Rose" <stan@olc.win.net>
Subject: Newbie : Help
Message-Id: <96e7kh$i64$1@wanadoo.fr>
Hi,
I don't know Perl language but I need to realize a script for an
application.
I m looking for someone who can help me to do it.
I need a script that can :
-1 Receive datas from a url (like :
www.mywebsite/script.pl?param1=xxx¶m2=yyy)
-2 Make a search into a database using odbc and the param1 and param2
-3 If the datas are found then
Run a exe file with some datas from the database (I don't want to
load a new webpage)
Else
redirect to another webpage
I really need your help for this script.
Regards,
Stan
------------------------------
Date: Wed, 14 Feb 2001 18:51:13 -0000
From: "DaveP" <DavePerkins@bigfoot.com>
Subject: Newbie: How do I make my home Win2k IIS system run Perl scripts?
Message-Id: <96ek1o$7qq$1@newsg4.svr.pol.co.uk>
Hello
I have IIS installed on my Win2k (Pro) pc. I also have Activestate Perl
installed.
How can I make IIS run my Perl scripts? At the moment if I type
http://Localhost/scripts/hello.pl it brings up the download box.
In IIS, do I have to associate the .pl extension with the perl.exe
application?
Thanks
David
------------------------------
Date: Wed, 14 Feb 2001 10:41:49 -0800
From: mothra <mothra@nowhereatall.com>
Subject: OT:status of the reorg of The Perl Journal
Message-Id: <3A8AD16D.6AAFA778@nowhereatall.com>
Hello All,
I would like to subscribe to "The Perl Journal" but when I went to the
web site
I found this:
Unfortunately, The Perl Journal is undergoing a transition and is not
processing
new subscriptions or the ordering of back issues at this time.
Does anyone know when they will be able to take subscriptions ?
Thanks
Mothra
------------------------------
Date: Wed, 14 Feb 2001 16:07:45 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: print << END;
Message-Id: <o4bl8t8sm5lbj1gb7clhgnembgptbak92a@4ax.com>
hefe@in-time.se wrote:
>I tried to do a
>
>print << END;
>
>..lots of html code....
>
>END
>
>but I keep getting an error message saying something like "terminator was not
>found before EOF"... why is that, what am I doing wrong?
First, it's a good idea to put quotes around your terminator string,
there right after the "<<".
Common errors:
* invisible characters on the terminator line (spaces, tabs)
* nothing following that last line. That's a bug on Win32 only. At
least one line following!
And use a different terminator for each here-doc. Otherwise, if the
first one isn't found, you'll get everything right up to the second
terminator.
If your terminator definition is in quotes, you can add all sorts of odd
characters on the actual termination line. For example:
print <<"# This works !!! #";
How about that?
# This works !!! #
--
Bart.
------------------------------
Date: Wed, 14 Feb 2001 13:19:51 -0500
From: "henroc" <henroc@mindspring.com>
Subject: Re: Problems with ActiveState Perl, please help.
Message-Id: <96ei4f$nrh$1@slb6.atl.mindspring.net>
hey,
thanks for the info. i totally forgot about the "or die" syntax, i'm
going to use that from now on. i'm still pretty new to perl and scripting
in general. i found out the reason i wasn't getting any environment
variables with my "list $ENV" script is because for the isapi .dll execution
of perl, environment variables are created dynamically as they are called.
i have one other question, do you have any links to good reads on perl? i'd
go out and buy some books, however i'm currently looking for a job and can't
afford to spend any money, hehe. i'll ask ya for some book titles later.
thanks
henry
"Jonathan Feinberg" <jdf@pobox.com> wrote in message
news:lmrbcc8v.fsf@pobox.com...
> "henroc" <henroc@mindspring.com> writes:
>
> > open (FILENAME, "file.txt"); doesn't open anything
>
> You must ask Perl what is going wrong, by checking the success of the
> open() call and reporting on any failures.
>
> open(F, 'file.txt') || die "Can't open file.txt for read: $!\n";
>
> See chapter 10 in _Learning Perl_ for more details about such things.
> You should also read in perlfunc about open() and die(). HTH.
>
> --
> Jonathan Feinberg jdf@pobox.com Sunny Brooklyn, NY
> http://pobox.com/~jdf
------------------------------
Date: Wed, 14 Feb 2001 16:57:15 -0000
From: "Géry" <ducateg@info.bt.co.uk>
Subject: regex experts
Message-Id: <96edj6$lnm$1@pheidippides.axion.bt.co.uk>
Hi
say that I have the following regex:
pattern:key1=0.16;key2=0.18;
I want to check the validity of this pattern, the rules being
- the first item must be a word (here: 'pattern')
- the second must be a colon
then the sequence goes:
- a key composed of anything (inc any funny character, figures, letters...)
- the equal symbol
- a decimal value
- a semi-colon
there is an indefined number of such a sequence thereafter.
/^\w:(.=\d+\.\d+$;)+/
I tried the regex above, as a validation match, but this does not work, any
ideas?
--
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Géry
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
------------------------------
Date: Wed, 14 Feb 2001 17:43:39 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: regex experts
Message-Id: <x7k86tma7o.fsf@home.sysarch.com>
>>>>> "G" == Géry <ducateg@info.bt.co.uk> writes:
G> Hi say that I have the following regex:
G> pattern:key1=0.16;key2=0.18;
G> I want to check the validity of this pattern, the rules being - the
G> first item must be a word (here: 'pattern') - the second must be a
G> colon
G> then the sequence goes: - a key composed of anything (inc any funny
G> character, figures, letters...) - the equal symbol - a decimal
G> value - a semi-colon
G> there is an indefined number of such a sequence thereafter.
G> /^\w:(.=\d+\.\d+$;)+/
lets look at that. the first \w is only a SINGLE word char and you want
a full word. so make that \w+
same for the .= it matches only a single char and then the =
but you know about + as you use it for \d+ so why not for the others?
the $ before the ; makes no sense. that is going to be parsed as $;
which is an obscure perl var (used to separate keys in a old perl4 style
multilevel hash). $ only means an ending anchor if it is at the end of
the whole regex (or some component that can end the regex).
here is one that should work (untested):
/^\w+:([^=]+=\d+\.\d+;)+$/
that matches a string starting with a word, then a : followed by one or
more token=123.34; strings and then ending.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com
------------------------------
Date: Wed, 14 Feb 2001 18:12:34 GMT
From: maheshasolkar@yahoo.com (Mahesh A)
Subject: Re: regex experts
Message-Id: <3a8ac9ce.176707832@news>
On Wed, 14 Feb 2001 16:57:15 -0000, "Géry" <ducateg@info.bt.co.uk>
wrote:
>Hi
>
>say that I have the following regex:
>pattern:key1=0.16;key2=0.18;
>
>I want to check the validity of this pattern, the rules being
>- the first item must be a word (here: 'pattern')
>- the second must be a colon
>
>then the sequence goes:
>- a key composed of anything (inc any funny character, figures,
letters...)
>- the equal symbol
>- a decimal value
>- a semi-colon
>
>there is an indefined number of such a sequence thereafter.
>
>/^\w:(.=\d+\.\d+$;)+/
>
>I tried the regex above, as a validation match, but this does not
work, any
>ideas?
Could this help?
------------
#!/usr/local/bin/perl -w
use strict;
my $str="pattern:key1=0.16;key2=.18;key3=18";
$str =~ /^(\S+):(.*)$/;
my $pattern = $2;
while ($pattern =~ /([a-zA-z0-9]+)=(\d*)\.?(\d+)?/g) {
# print "$1 = $2.$3\n"; # If Values are strictly going to be in
dd.dd format
print "$1 = $2".(defined($3) ? ".$3" : "")."\n";
}
--------------
Also see: Yesterday's post on FAQ 4.27
------------------------------
Date: 14 Feb 2001 18:05:43 +0000
From: nobull@mail.com
Subject: Re: regex experts
Message-Id: <u91yt1gmx4.fsf@wcl-l.bham.ac.uk>
"Géry" <ducateg@info.bt.co.uk> writes:
> pattern:key1=0.16;key2=0.18;
> /^\w:(.=\d+\.\d+$;)+/
>
> I tried the regex above, as a validation match, but this does not work, any
> ideas?
You clearly realise that \d matches only a single character yet failed
to realise that \w and . do too.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 14 Feb 2001 16:40:38 GMT
From: lejeuned@starband.net (David W LeJeune)
Subject: remote IP Address
Message-Id: <ayyi6.709$fz1.172395545@twister1.starband.net>
I log into a computer (HP) via telnet from my PC. On the HP I need a
Perl subroutine to determine the ip of the computer from whence I'm telnetting.
That computer (my PC) does not have a static IP address. Any ideas?
Regards
Dave
------------------------------
Date: 14 Feb 2001 17:43:06 GMT
From: abigail@foad.org (Abigail)
Subject: Re: remote IP Address
Message-Id: <slrn98lgta.cnp.abigail@tsathoggua.rlyeh.net>
David W LeJeune (lejeuned@starband.net) wrote on MMDCCXXIV September
MCMXCIII in <URL:news:ayyi6.709$fz1.172395545@twister1.starband.net>:
:: I log into a computer (HP) via telnet from my PC. On the HP I need a
:: Perl subroutine to determine the ip of the computer from whence I'm telnettin
:: That computer (my PC) does not have a static IP address. Any ideas?
At least one of 'finger', 'who' or 'w' on most Unix systems will give
you the information you want.
Abigail
--
map{${+chr}=chr}map{$_=>$_^ord$"}$=+$]..3*$=/2;
print "$J$u$s$t $a$n$o$t$h$e$r $P$e$r$l $H$a$c$k$e$r\n";
------------------------------
Date: Wed, 14 Feb 2001 15:47:47 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Replacing single-letter words doesn't work
Message-Id: <x7n1bpmfkt.fsf@home.sysarch.com>
>>>>> "HM" == Holger Marzen <holger.marzen@sik-gmbh.de> writes:
HM> Let's see: $suchtext =~
HM> s/(^|\s)[A-Z0-9ÄÖÜäöüß\-_]{1,2}(?=\s|$)//ig
HM> seems to work ok. Instead of (^|\s) I think I could also write
HM> [\^\s] but I am not sure if - \^ is start-of-line instead NOT - \s
HM> is a space instead of s (can I use special classes like \s in []
HM> ?)
no, ^ in the first case is a metachar meaning start of a line. is that
what you want? if so you also need the /m modifier with the /g to
allow ^ to match an internal newline. you are not clear on this. ^
inside a char class is a regular ^ only unless it is the first one when
it inverts the class. escaping it only makes it a regular char and then
you won't match at the beginning of a line. you have to read perlre more
and get the book mastering regular expressions.
>> more locale chars in the class. look into perl 5.6's support of
>> posix char classes which might solve the problem better. this is
>> possible:
HM> This is perl, version 5.005_03
even so you can simplify by using a var or qr// to hold the char class
of what you want to define as word letters. then you can still use my
code example to do that regardless of what perl defines \b to mean. what
if your 1-2 char word is flanked by a punctuation char? your regex would
not find it. you should bracket your word with lookahead/behind that
acts like \b.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com
------------------------------
Date: 14 Feb 2001 16:24:21 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: setting degree of parallelism
Message-Id: <96ebfl$q2q$2@mamenchi.zrz.TU-Berlin.DE>
<jrpink@my-deja.com> wrote in comp.lang.perl.misc:
>I have a number of tasks which run faster with multiple processes, so I am
>setting up a perl script which I can wrap around those jobs. In this
>example, I am just creating 10 files, printing something in them and exiting.
> The idea is to have a number of child processes equal to the $dop (degree of
>parallelism) variable do the work. Everything works fine in the script
>below, which I cobbled together from a number of examples on forking. The
>problem is when I set the $dop to 1, the first task completes, the child is
>reaped, then the process just sits there spinning on the signal handler, I
>think.
>
>The whole point of this script is to have $dop set greater than one, but
>since I don't understand what is happening when it is one, my ingnorance is
>probably more advanced than I thought.
>
>If anyone can provide some insight as to what is happening, I'd greatly
>appreciate it.
>
>John
I haven't analysed your code in full, but inserted a few remarks
where it looks fishy, and even some where it looks good.
>#!/usr/local/bin/perl -w
-w is good, -w *and* use strict is better.
>use POSIX ":sys_wait_h";
>
>## doesn't work when dop is 1
>## parent just sucks up cpu.
>
>@files = qw(test1 test2 test3 test4 test5 test6 test7 test8 test9 test10);
>
>$children = 0;
>
>$SIG{CHLD} = \&REAPER;
>
>$dop = 1; # degree of parallelism
>
>while(@files)
>{
> $file = shift(@files);
I'd prefer to shift a file only when we know we are going to fork
> if ($children < $dop)
> {
> $children++;
Again, this is too early. You don't know if you have successfully
forked a child yet.
> if ($pid = fork) # parent
> {
> print("pid: $pid\n");
> }
> else # child
> {
> open(FH,">$file");
>
> for ($i = 0;$i<10000 * @files;$i++)
> { print FH "This is test line $i written by child: $children\n"; }
> close(FH);
>
> exit 0;
Very well. The child must exit explicitly, lest it execute the parent's
code.
> }
> }
> else
> {
> print("too many children, sleeping\n");
> unshift (@files,$file);
Oh... here you undo the early shift. Well, I guess it should work,
if everything else did.
> sleep 1;
> }
>}
>
>sub REAPER { my $child; while ($child = waitpid(-1,WNOHANG)) {
>print("error code: $?\n"); #$Kid_Status{$child} = $?; # commentd out, I
>don't know what this line is for. JP if ($child != -1) { print("reaped:
>$child\n"); $children--; print("children: $children\n"); } } }
>
>exit 0;
The last few lines are unreadable. What happened to your line-feeds?
Actually, you don't need a SIGCHLD handler at all in your program, not
just because it forks. You use one if the main program cannot be
bothered to wait for its children because it has other things to do.
Your's hasn't, so you can wait for a kid (any kid) to die in your main
loop. This simplifies things considerably:
#!/usr/bin/perl
use strict; use warnings; $| = 1;
my @files = qw( f1 f2 f3 f4 f5 f6 f7 f8 f9);
my $dop = 5; # don't set this to zero!
my $children = 0;
while ( @files ) {
if ( $children < $dop ) {
my $file = shift @files;
my $pid = fork;
die "Can't fork: $!\n" unless defined $pid;
if ( $pid ) {
$children++;
} else {
print "kid $$ doing file $file\n";
sleep 1 + rand 5; # waste some time
exit;
}
} else {
# we *know* there is an outstanding kid because $dop is > 0.
my $kidpid = waitpid( -1, 0);
$children-- unless $kidpid == -1;
}
}
# we could exit here and let init reap the children
$children-- while waitpid( -1, 0) != -1; # reap remaining children
__END__
Anno
------------------------------
Date: 14 Feb 2001 18:12:38 GMT
From: Xming <duxiao@ufr-info-p7.jussieu.fr>
Subject: split Qustion
Message-Id: <96ehqm$8s1$1@vishnu.jussieu.fr>
This is a small fragement I wrote:
$string = 'title=something';
$name = ${split('=', $string, 2)}[1];
print $name;
When I run the program, Perl does not complain anything, but actually I get nothing in $name.
It seems I have to write it like following:
($useless, $name) = split('=', $string, 2);
Then it will print the string 'something' to the console. Could anybody tell me why the first does not work? Thanks in advance!
Xming
------------------------------
Date: Wed, 14 Feb 2001 18:26:32 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: split Qustion
Message-Id: <x7g0hhm887.fsf@home.sysarch.com>
>>>>> "X" == Xming <duxiao@ufr-info-p7.jussieu.fr> writes:
X> This is a small fragement I wrote: $string = 'title=something';
X> $name = ${split('=', $string, 2)}[1]; print $name;
where did you learn that syntax? burn the book if that was in it.
what you want is a list and index.
$name = ( split('=', $string, 2) )[1];
X> ($useless, $name) = split('=', $string, 2);
that is better done as
(undef, $name) = split('=', $string );
no need for the limit of 2 as you only have 1 = char.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com
------------------------------
Date: 14 Feb 2001 18:36:10 +0000
From: nobull@mail.com
Subject: Re: split Qustion
Message-Id: <u9snlhf6xx.fsf@wcl-l.bham.ac.uk>
Xming <duxiao@ufr-info-p7.jussieu.fr> writes:
> When I run the program, Perl does not complain anything,
By default Perl is very reluctant to point out your mistakes.
Enable warnings a strictures.
use warnings; # Prior to 5.6 use -w on the #! header line
use strict;
Perl will then be more willing point out your mistakes.
If you had lurked for a while you would know that it is considered
rude to come here and ask human beings to look for your mistakes
unless you have asked Perl to look for them first.
It is rude to post to a newsgroup without lurking for a while first.
<FISH>
> $name = ${split('=', $string, 2)}[1];
You said ${} where you meant ()
split() returns a list, not a reference to an array.
</FISH>
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 14 Feb 2001 15:51:51 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Strings and pointers in Perl?
Message-Id: <k8al8t06l5l032cg8fr6tr8q342821v4uf@4ax.com>
Frank van Wensveen wrote:
>But the 'do something wich each character'
>involves a CRC calculation among other things which is a little hard
>to do with regexps. :-)
But there are modules (written in C) that can do that for you. Check
out String::CRC. Digest::MD5 produces something similar to a CRC.
And if you insist, try something like
@asc = unpack 'C*', $string;
You'll get an array of the "Ascii" code of each character. (Technically
speaking, it's more correct to talk about the "ord" value of each
character.)
--
Bart.
------------------------------
Date: Wed, 14 Feb 2001 09:19:28 -0600
From: "Brad A. Gwinn" <bradgwinn@hotmail.com>
Subject: using getc to input user response from keyboard
Message-Id: <Jmxi6.639$cu.2452@gazette.loc1.tandem.com>
I'm writing a script that requires a 'Y' or 'N' response from the user. I'm
using an "until" loop which checks the return from getc, but it also gets
the "\n" from hitting the Enter key, and the cursor drops a line in the
console window. Here's the code I'm using:
until (("\U$char" eq "Y") || ("\U$char" eq "N")){
$char = getc(STDIN);
if ("\U$char" eq "Y") {
$result = 1;
}elsif ("\U$char" eq "N") {
$result = 0;
}else {
$char = "";
}
}
I would like the script to process the response from the keyboard without
the user having to hit Enter, and if the response is not Y or N, then to
just keep checking until one or the other is selected.
Any ideas?
Thanks,
Brad Gwinn
brad.gwinn@compaq.com
------------------------------
Date: Wed, 14 Feb 2001 16:09:20 GMT
From: Ala Qumsieh <aqumsieh@hyperchip.com>
Subject: Re: using getc to input user response from keyboard
Message-Id: <7ag0hh6ybz.fsf@merlin.hyperchip.com>
"Brad A. Gwinn" <bradgwinn@hotmail.com> writes:
> I would like the script to process the response from the keyboard without
> the user having to hit Enter, and if the response is not Y or N, then to
> just keep checking until one or the other is selected.
>
> Any ideas?
Check your local FAQs.
perldoc -q single character
--Ala
------------------------------
Date: Wed, 14 Feb 2001 16:12:09 GMT
From: Gus <gus@black.hole-in-the.net>
Subject: Re: using getc to input user response from keyboard
Message-Id: <982167129.6274.0.nnrp-09.c29f015a@news.demon.co.uk>
Brad A. Gwinn <bradgwinn@hotmail.com> wrote:
> I'm writing a script that requires a 'Y' or 'N' response from the user. I'm
> using an "until" loop which checks the return from getc, but it also gets
> the "\n" from hitting the Enter key, and the cursor drops a line in the
> console window.
Have you looked at Term::ReadKey ? It's designed to make doing exactly this
a lot easier.
--
gus@black.hole-in-the.net
0x58E18C6D
82 AA 4D 7F D8 45 58 05 6D 1B 1A 72 1E DB 31 B5
http://black.hole-in-the.net/gus/
------------------------------
Date: 14 Feb 2001 07:43:51 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Why can't I grab this URL?
Message-Id: <m1vgqd5ky0.fsf@halfdome.holdit.com>
>>>>> "What" == What A Man ! <whataman@home.com> writes:
What> http://www.fortunecity.com/millenium/blyton/243/clemrule.zip
What> $req->header('Accept' => 'text/html');
Why are you doing that? Is a "zip" a text/html?
--
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: Wed, 14 Feb 2001 16:08:56 GMT
From: Jozxyqk <jfeuerst@eecs.tufts.edu>
Subject: XS -- "array subscript is not an integer"
Message-Id: <s4yi6.818$8I.208777@typhoon.ne.mediaone.net>
Here's a weird error, but I'm pretty sure I know the cause of it.
I'm writing an XS module, and one of the arguments to the function
I am writing is a double, with the variable name ax.
When I declare the function, and try to compile, I get:
"array subscript is not an integer" on the next line after that.
If I rename the variable, everything works fine.
Or, if it's of type "int", it's fine.
Is there some global variable in the innards of XS that's called ax
or something? It's no big deal; I can change the variable name;
I just thought it was somewhat humorous, and it spurred my
curiosity.
I would have thought it was my code, but I can reproduce the error
on a different machine, with a different plain-vanilla lib.
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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.
| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 289
**************************************