[31465] in Perl-Users-Digest
Perl-Users Digest, Issue: 2717 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Dec 12 14:09:42 2009
Date: Sat, 12 Dec 2009 11:09:08 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 12 Dec 2009 Volume: 11 Number: 2717
Today's topics:
Re: "negative" regex matching? <seven.reeds@gmail.com>
Re: "negative" regex matching? <tadmc@seesig.invalid>
Re: "negative" regex matching? <ben@morrow.me.uk>
Re: Best way to replace a set of strings in large files sln@netherlands.com
Re: How big do your programs get before you modularise <tadmc@seesig.invalid>
Re: How big do your programs get before you modularise <tadmc@seesig.invalid>
Re: regex find then replace - a cleaner approach? <glex_no-spam@qwest-spam-no.invalid>
Re: regex find then replace - a cleaner approach? <john1949@yahoo.com>
Re: regex find then replace - a cleaner approach? <jurgenex@hotmail.com>
Simple loop error <spam.meplease@ntlworld.com>
Re: Simple loop error <rvtol+usenet@xs4all.nl>
Re: Simple loop error <spam.meplease@ntlworld.com>
Re: Simple loop error <rvtol+usenet@xs4all.nl>
Re: Simple loop error (Bradley K. Sherman)
Re: Unblessed reference. <source@netcom.com>
Re: Unblessed reference. <ben@morrow.me.uk>
Re: Want to judge some remote hosts online or not quick <spamtotrash@toomuchfiction.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 11 Dec 2009 09:01:04 -0800 (PST)
From: "seven.reeds" <seven.reeds@gmail.com>
Subject: Re: "negative" regex matching?
Message-Id: <f350120c-26f3-4432-b0cb-4fd841a4ffb8@j19g2000yqk.googlegroups.com>
>
> =A0 =A0 s{(Johnny|john)} =A0{<span>$1</span>}gi;
>
Hi Ted, this was perfect. I was way over-thinking this.
Thanks
------------------------------
Date: Fri, 11 Dec 2009 13:15:23 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: "negative" regex matching?
Message-Id: <slrnhi56c5.nbo.tadmc@tadbox.sbcglobal.net>
seven.reeds <seven.reeds@gmail.com> wrote:
>>
>> Â Â s{(Johnny|john)} Â {<span>$1</span>}gi;
>>
>
> this was perfect. I was way over-thinking this.
But when you have a pattern that is the prefix of another pattern,
as you do above, you must ensure that the longer one is leftmost.
See for yourself:
------------------------
#!/usr/bin/perl
use warnings;
use strict;
$_ = "Sarah likes Johnny's cooking\n";
s{(Johnny|john)}{<span>$1</span>}gi;
print;
$_ = "Sarah likes Johnny's cooking\n";
s{(john|Johnny)}{<span>$1</span>}gi;
print;
------------------------
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
------------------------------
Date: Fri, 11 Dec 2009 22:19:31 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: "negative" regex matching?
Message-Id: <j4tav6-cd2.ln1@osiris.mauzo.dyndns.org>
Quoth "seven.reeds" <seven.reeds@gmail.com>:
> [Tad wrote:]
> >
> > s{(Johnny|john)} {<span>$1</span>}gi;
> >
>
> Hi Ted, this was perfect. I was way over-thinking this.
ITYM 'Tad'. This would have been more apparent if you had attributed
your quotation properly.
Ben
------------------------------
Date: Fri, 11 Dec 2009 11:42:24 -0800
From: sln@netherlands.com
Subject: Re: Best way to replace a set of strings in large files?
Message-Id: <ta65i55doeanp4i665ndgbfrb1h65o1c91@4ax.com>
On Thu, 10 Dec 2009 23:09:28 -0800 (PST), cvhLE <info@cpi-service.com> wrote:
>On Dec 10, 3:21 pm, Ryan Chan <ryanchan...@gmail.com> wrote:
>> Hello,
>>
>> Consider the case:
>>
>> You have 200 lines of mapping to replace, in a csv format, e.g.
>>
>> apple,orange
>> boy,girl
>> ...
>>
>> You have a 500MB file, you want to replace all 200 lines of mapping,
>> what would be the most efficient way to do it?
>>
>> Thanks.
>
>If you want to replace the whole line or know the column where you
>need to replace it and the line has clear separators you may be be a
>lot faster if you do it using awk:
>
>cat csv|awk -F"," "$2~/apple/ {$2="orange"; print $1,$2} " ...
>
>otherwise I don't see a reason not to use the most obvious way:
>starting from line 1 and running until the end ... especially if dont
>know *where* the 200 lines are ...
>
>#! /usr/bin/perl -w
>%replace=('apple'=>'orange','boy'=>'girl');
>$r="(".join ("|", keys %replace ).")";$r=qr($r);
>while (<>) {
>s/$r/$replace{$1}/g;
>print;
>}
>
I would asume this would take a long
time to do this process.
At a minimum, it would take
500,000,000
x
200
-----------------
100,000,000,000
100 billion character comparisons
if nothing ever matched.
Still not matching word, but the first character
matched before backtracking
100,000,000,000
x
2
----------------
200,000,000,000
brings the total up to 200 billion character
comparisons.
Since this is all a conservative estimate
I would average (conservatively) 4 comparison
characters per map per byte in the file and say
500,000,000
x
800
-----------------
400,000,000,000
400 billion comparisons.
Add to that the menutia of backtracking, loading
buffers, writing to disk, and the underpining layers
Perl has to do to execute C code, and I would go out
for coffee or take a nap.
-sln
------------------------------
Date: Fri, 11 Dec 2009 10:31:09 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: How big do your programs get before you modularise most of it?
Message-Id: <slrnhi4so7.mjs.tadmc@tadbox.sbcglobal.net>
Sir Robert Burbridge <rburbrid@cisco.com> wrote:
> On 12/10/2009 10:55 PM, Xho Jingleheimerschmidt wrote:
>> If I can come up with a modular design
>> within a couple minutes, then almost certainly those modules have
>> already been thought of and already exist on CPAN, and I should use
^^^^^^^^^^^^^
>> those rather than writing my own.
^^^^^^^^^^^^^^
> Apparently you don't have to deal with IT and getting new modules
> installed =)
Huh?
Xho has a new module being installed either way, the alternatives
are installing one written by himself or one from CPAN.
Does your IT object to CPAN but not to self-written, or to self-written
but not to CPAN?
Or does IT just object to modules in general?
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
------------------------------
Date: Fri, 11 Dec 2009 13:16:42 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: How big do your programs get before you modularise most of it?
Message-Id: <slrnhi56ek.nbo.tadmc@tadbox.sbcglobal.net>
Sir Robert Burbridge <rburbrid@cisco.com> wrote:
> On 12/11/2009 11:31 AM, Tad McClellan wrote:
>> Sir Robert Burbridge<rburbrid@cisco.com> wrote:
>>> On 12/10/2009 10:55 PM, Xho Jingleheimerschmidt wrote:
>>
>>>> If I can come up with a modular design
>>>> within a couple minutes, then almost certainly those modules have
>>>> already been thought of and already exist on CPAN, and I should use
>> ^^^^^^^^^^^^^
>>>> those rather than writing my own.
>> ^^^^^^^^^^^^^^
>>
>>> Apparently you don't have to deal with IT and getting new modules
>>> installed =)
>>
>>
>> Huh?
>>
>> Xho has a new module being installed either way, the alternatives
>> are installing one written by himself or one from CPAN.
>>
>> Does your IT object to CPAN but not to self-written, or to self-written
>> but not to CPAN?
>>
>> Or does IT just object to modules in general?
>
> I get your point ... I shouldn't have responded to an absurd reply =)
Huh?
Xho's reply was insightful and helpful...
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
------------------------------
Date: Fri, 11 Dec 2009 10:22:12 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: regex find then replace - a cleaner approach?
Message-Id: <4b2271b5$0$87079$815e3792@news.qwest.net>
John wrote:
> Hi
>
> I have
>
> if ($SIC =~m |20745|) {
> $SIC =~s |20745|567345|g;
> $sql="UPDATE $table SET SIC='$SIC' WHERE id='$id'"
> }
>
> It works, but there must be a cleaner way without having to repeat the
> 'find' first and then the 'replace'. The $sql line only takes effect if
> there is a successful match.
There's nothing wrong with that, except possibly that
you're not using placeholders in your SQL. However, if
you really want to..
if ( $SIC =~ s/20745/567345/g )
{
my $sql = "UPDATE $table SET SIC=? WHERE id=?";
# do something with $sql..
}
or if you only want to do something with $sql, then
return/next right away:
return unless $SIC =~ s/20745/567345/g;
my $sql = "UPDATE $table SET SIC=? WHERE id=?";
------------------------------
Date: Fri, 11 Dec 2009 17:00:10 -0000
From: "John" <john1949@yahoo.com>
Subject: Re: regex find then replace - a cleaner approach?
Message-Id: <hfttqt$spk$1@news.albasani.net>
"J. Gleixner" <glex_no-spam@qwest-spam-no.invalid> wrote in message
news:4b2271b5$0$87079$815e3792@news.qwest.net...
> John wrote:
>> Hi
>>
>> I have
>>
>> if ($SIC =~m |20745|) {
>> $SIC =~s |20745|567345|g;
>> $sql="UPDATE $table SET SIC='$SIC' WHERE id='$id'"
>> }
>>
>> It works, but there must be a cleaner way without having to repeat the
>> 'find' first and then the 'replace'. The $sql line only takes effect if
>> there is a successful match.
>
> There's nothing wrong with that, except possibly that
> you're not using placeholders in your SQL. However, if
> you really want to..
>
>
> if ( $SIC =~ s/20745/567345/g )
> {
> my $sql = "UPDATE $table SET SIC=? WHERE id=?";
> # do something with $sql..
> }
>
> or if you only want to do something with $sql, then
> return/next right away:
>
>
> return unless $SIC =~ s/20745/567345/g;
> my $sql = "UPDATE $table SET SIC=? WHERE id=?";
Hi
OK. I had assumed the if (expression) was always boolean - if (X>6) etc - I
did not realise it tested whether an expression executed.
Regards
John
------------------------------
Date: Fri, 11 Dec 2009 09:31:33 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: regex find then replace - a cleaner approach?
Message-Id: <bvv4i5hh4g6gc7725bagj68rkld1ativkp@4ax.com>
"John" <john1949@yahoo.com> wrote:
>OK. I had assumed the if (expression) was always boolean - if (X>6) etc -
It is
> I did not realise it tested whether an expression executed.
It doesn't.
You are thourougly confused. Yes, the condition in an 'if' must be
boolean. So let's look at it in detail
if ( $SIC =~ s/20745/567345/g ){
What is the return value of s///? The documentation says
s/PATTERN/REPLACEMENT/msixpogce
[...] and returns the number of
substitutions made. Otherwise it returns false.
So, if there were no substitutions then s/// already returns false.
And if there were substitutions made then it returns a number larger
than 0. And the boolean value of any number larger then 0 is true.
So you get exactly the boolean value you would expect on success and
failure. This has nothing to do with if an expression was executed or
not but simply with a smart design for the return value of s///.
jue
------------------------------
Date: Sat, 12 Dec 2009 16:49:12 GMT
From: dan <spam.meplease@ntlworld.com>
Subject: Simple loop error
Message-Id: <cQPUm.29534$3M1.8149@newsfe18.ams2>
The following code does not print 1, because it goes from 0.81 to
0.820000000000001. This is quite a big deal isn't it?
for ($i = 0; $i <= 1; $i += 0.01) {
print "$i\n";
}
Using perl v5.10.0
------------------------------
Date: Sat, 12 Dec 2009 18:17:11 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Simple loop error
Message-Id: <4b23d017$0$22945$e4fe514c@news.xs4all.nl>
dan wrote:
> The following code does not print 1, because it goes from 0.81 to
> 0.820000000000001. This is quite a big deal isn't it?
>
> for ($i = 0; $i <= 1; $i += 0.01) {
> print "$i\n";
> }
>
> Using perl v5.10.0
perldoc -q 999
--
Ruud
------------------------------
Date: Sat, 12 Dec 2009 17:32:49 GMT
From: dan <spam.meplease@ntlworld.com>
Subject: Re: Simple loop error
Message-Id: <5tQUm.29536$3M1.25495@newsfe18.ams2>
On Sat, 12 Dec 2009 18:17:11 +0100, Dr.Ruud wrote:
> dan wrote:
>
>> The following code does not print 1, because it goes from 0.81 to
>> 0.820000000000001. This is quite a big deal isn't it?
>>
>> for ($i = 0; $i <= 1; $i += 0.01) {
>> print "$i\n";
>> }
>>
>> Using perl v5.10.0
>
> perldoc -q 999
So something like this is necessary?
for ($i = 0; $i <= 1; $i = sprintf("%.2f", $i + 0.01)) {
print "$i\n";
}
------------------------------
Date: Sat, 12 Dec 2009 18:41:23 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Simple loop error
Message-Id: <4b23d5c4$0$22939$e4fe514c@news.xs4all.nl>
dan wrote:
> Dr.Ruud:
>> dan:
>>> The following code does not print 1, because it goes from 0.81 to
>>> 0.820000000000001. This is quite a big deal isn't it?
>>>
>>> for ($i = 0; $i <= 1; $i += 0.01) {
>>> print "$i\n";
>>> }
>>
>> perldoc -q 999
>
> So something like this is necessary?
>
> for ($i = 0; $i <= 1; $i = sprintf("%.2f", $i + 0.01)) {
> print "$i\n";
> }
For integer behavior, it is necessary to use integers.
for my $i (24 .. 42) {
printf "%.2f", $i / 100;
}
--
Ruud
------------------------------
Date: Sat, 12 Dec 2009 17:52:24 +0000 (UTC)
From: bks@panix.com (Bradley K. Sherman)
Subject: Re: Simple loop error
Message-Id: <hg0l8o$lk4$1@reader1.panix.com>
In article <cQPUm.29534$3M1.8149@newsfe18.ams2>,
dan <spam.meplease@ntlworld.com> wrote:
>The following code does not print 1, because it goes from 0.81 to
>0.820000000000001. This is quite a big deal isn't it?
>
>for ($i = 0; $i <= 1; $i += 0.01) {
> print "$i\n";
>}
It is ancient knowledge that one does not compare IEEE Standard 754
floating point numbers for equality. Perl or not Perl.
See, e.g.
<http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm>
--bks
------------------------------
Date: Fri, 11 Dec 2009 14:31:02 -0800
From: David Harmon <source@netcom.com>
Subject: Re: Unblessed reference.
Message-Id: <FNKdnQ8nRb6sVb_WnZ2dnUVZ_gqdnZ2d@earthlink.com>
On Wed, 9 Dec 2009 23:10:17 +0000 in comp.lang.perl.misc, Ben Morrow
<ben@morrow.me.uk> wrote,
> In simple terms, an object is just a
>refrence with a bit of magic attached.
"bit of magic attached" isn't simple terms; it's pure obfuscation.
------------------------------
Date: Fri, 11 Dec 2009 23:28:22 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Unblessed reference.
Message-Id: <m51bv6-o23.ln1@osiris.mauzo.dyndns.org>
Quoth "Newsgroup only please, address is no longer replyable." <bad@example.invalid>:
> On Wed, 9 Dec 2009 23:10:17 +0000 in comp.lang.perl.misc, Ben Morrow
> <ben@morrow.me.uk> wrote,
> > In simple terms, an object is just a
> >refrence with a bit of magic attached.
>
> "bit of magic attached" isn't simple terms; it's pure obfuscation.
If you say so. Would you rather I said 'an object is a reference to a
SV that is at least a PVMG, with SvSTASH set to point to the stash of
the class it's blessed into'? Perhaps you would like to provide a simple
explanation of Perl objects, suitable for someone who hasn't met the
concept before?
Ben
------------------------------
Date: Fri, 11 Dec 2009 19:45:08 +0000 (UTC)
From: Kevin Collins <spamtotrash@toomuchfiction.com>
Subject: Re: Want to judge some remote hosts online or not quickly over WAN.
Message-Id: <slrnhi58bd.1sp.spamtotrash@vai.unix-guy.com>
On 2009-12-04, smallpond <smallpond@juno.com> wrote:
> On Dec 4, 8:28 am, Hongyi Zhao <hongyi.z...@gmail.com> wrote:
>> Hi all,
>>
>> I want to judge some remote hosts online or not quickly over WAN. I've
>> learned that ping command will not work in this case if the icmp ack
>> is blocked locally by firewall. Is it possiable for me to do this job
>> by perl codes?
>
>
> So the problem is that they are online but you aren't.
> There is no valid reason to block ICMP packets.
Really? Lots of people do... I believe the idea is to prevent ping floods.
Kevin
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 V11 Issue 2717
***************************************