[18116] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 276 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Feb 12 18:10:49 2001

Date: Mon, 12 Feb 2001 15: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: <982019423-v10-i276@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Mon, 12 Feb 2001     Volume: 10 Number: 276

Today's topics:
        Problems with ActiveState Perl, please help. <henroc@mindspring.com>
        regex match line except for specific number? (Mike Eggleston)
    Re: regex match line except for specific number? (Mike Eggleston)
    Re: regex match line except for specific number? (Ben Okopnik)
    Re: regex match line except for specific number? <kstep@pepsdesign.com>
        reload blocking <c.gersch@team.isneurope.com>
        Replacing single-letter words doesn't work (Holger Marzen)
    Re: Replacing single-letter words doesn't work <DNess@Home.Com>
    Re: Replacing single-letter words doesn't work <uri@sysarch.com>
    Re: Replacing single-letter words doesn't work <DNess@Home.Com>
    Re: Replacing single-letter words doesn't work (Holger Marzen)
    Re: Replacing single-letter words doesn't work (Mark Jason Dominus)
    Re: Replacing single-letter words doesn't work <uri@sysarch.com>
        socket problem <b0l4549@cs.tamu.edu>
    Re: sort of about sort <uri@sysarch.com>
    Re: subroutine woes (Joe Smith)
    Re: the best solution? <mischief@velma.motion.net>
    Re: the best solution? <joe+usenet@sunstarsys.com>
    Re: the best solution? <joe+usenet@sunstarsys.com>
        Timezones <rob@robdon.com>
    Re: XML: "Flattening" XML to dot notation - best module <gerard@NOSPAMlanois.com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Mon, 12 Feb 2001 16:50:35 -0500
From: "henroc" <henroc@mindspring.com>
Subject: Problems with ActiveState Perl, please help.
Message-Id: <969ls3$jts$1@nntp9.atl.mindspring.net>

i'm having big problems with activestat perl on windows 2000 server / iis
5.0:

i'm getting some strange behavior with perl.  when i use perl.exe to process
cgi scripts, all the webserver environment varibles are there, however open
(FILENAME, "file.txt"); doesn't open anything.  on the other hand, when
using perlIS open (FILENAME, "file.txt"); works fine but none of the
webserver environment variables are there.  i'd appreiciate any solutions to
this problem, advice, reads, other implementations of perl on windows,
anything.  thanks.

henry




------------------------------

Date: Mon, 12 Feb 2001 19:34:19 GMT
From: mikee@kensho.eggtech.com (Mike Eggleston)
Subject: regex match line except for specific number?
Message-Id: <slrn98gels.n1l.mikee@kensho.eggtech.com>

Reading a configuration file for patterns to match, I can easily
match a line such as:

ERROR: UNKNOWN ERROR, reply = 1500081

where I want this line to be matched, then ignored, but if I
then want the same line with any other number what do I put?

28104  ERROR: UNKNOWN ERROR, reply = [^1][^5][^0][^0][^0][^8][^1]

matches

28104  ERROR: UNKNOWN ERROR, reply = 1500082

and I do not want the line ignored?

TIA

Mike


------------------------------

Date: Mon, 12 Feb 2001 20:16:12 GMT
From: mikee@kensho.eggtech.com (Mike Eggleston)
Subject: Re: regex match line except for specific number?
Message-Id: <slrn98gh4c.nb8.mikee@kensho.eggtech.com>

On Mon, 12 Feb 2001 19:34:19 GMT, <mikee@kensho.eggtech.com> wrote:
>Reading a configuration file for patterns to match, I can easily
>match a line such as:
>
>ERROR: UNKNOWN ERROR, reply = 1500081
>
>where I want this line to be matched, then ignored, but if I
>then want the same line with any other number what do I put?
>
>28104  ERROR: UNKNOWN ERROR, reply = [^1][^5][^0][^0][^0][^8][^1]
>
>matches
>
>28104  ERROR: UNKNOWN ERROR, reply = 1500082
>
>and I do not want the line ignored?

found it
need to use the (?!...) syntax


------------------------------

Date: 12 Feb 2001 20:38:43 GMT
From: ben-fuzzybear@geocities.com (Ben Okopnik)
Subject: Re: regex match line except for specific number?
Message-Id: <slrn98gim1.jcj.ben-fuzzybear@Odin.Thor>

The ancient archives of Mon, 12 Feb 2001 19:34:19 GMT showed
Mike Eggleston of comp.lang.perl.misc speaking thus:
>Reading a configuration file for patterns to match, I can easily
>match a line such as:
>
>ERROR: UNKNOWN ERROR, reply = 1500081
>
>where I want this line to be matched, then ignored, but if I
>then want the same line with any other number what do I put?
>
>28104  ERROR: UNKNOWN ERROR, reply = [^1][^5][^0][^0][^0][^8][^1]
>
>matches
>
>28104  ERROR: UNKNOWN ERROR, reply = 1500082
>
>and I do not want the line ignored?


print unless /1500081/;


For more info, see "perldoc perlsyn" and "perldoc perlre".


Ben Okopnik

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
If you're keeping score, win. -- Red Auerbach


------------------------------

Date: Mon, 12 Feb 2001 15:45:33 -0500
From: "Kurt Stephens" <kstep@pepsdesign.com>
Subject: Re: regex match line except for specific number?
Message-Id: <969hv4$bni$1@slb3.atl.mindspring.net>

"Mike Eggleston" <mikee@kensho.eggtech.com> wrote in message
news:slrn98gh4c.nb8.mikee@kensho.eggtech.com...
> On Mon, 12 Feb 2001 19:34:19 GMT, <mikee@kensho.eggtech.com> wrote:
> >Reading a configuration file for patterns to match, I can easily
> >match a line such as:
> >
> >ERROR: UNKNOWN ERROR, reply = 1500081
> >
> >where I want this line to be matched, then ignored, but if I
> >then want the same line with any other number what do I put?
> >
> >28104  ERROR: UNKNOWN ERROR, reply = [^1][^5][^0][^0][^0][^8][^1]
> >
> >matches
> >
> >28104  ERROR: UNKNOWN ERROR, reply = 1500082
> >
> >and I do not want the line ignored?
>
> found it
> need to use the (?!...) syntax

How about using the regex to parse the data and grep to filter it out?

my @ignore = (1500081, 1270075);
my %lookup = map {$_ => 1} @ignore;
my @errors = grep {chomp && /(\d+)$/ && !$lookup{$1}} <DATA>;
print join "\n", @errors;

Kurt Stephens






------------------------------

Date: Mon, 12 Feb 2001 23:38:11 +0100
From: "Christian Gersch" <c.gersch@team.isneurope.com>
Subject: reload blocking
Message-Id: <969okj$b9t$01$1@news.t-online.com>

Hi there!

The script (I found it free at the web) attatched _should_ prevent the user
from seeing the same random text if he would like reload the websites. This
really works fine - but there's one problem: What do I have to change in
order that if the user has seen every single random text doesn't start again
with the first seen random text - he should get a message like "no more
textes left".

PLEASE help me!!

Thank you very, very much!

-Chris

===== CODE: Start =====
#!/usr/bin/python
########################################################
#            Randex  1.21 (Python version)             #
#               By Thomas Tsoi 3.10.99                 #
#             webmaster@astronomy.org.hk               #
########################################################
#                                                      #
# Perl on the Rock:                                    #
#   http://www.astronomy.org.hk/perl/                  #
# Astronomy for Dummies                                #
#   http://www.astronomy.org.hk/                       #
#                                                      #
# #################################################### #
#                                                      #
# You can distribute this script and use it freely as  #
# long as this header is not edited in the script.     #
#                                                      #
########################################################

############ This is the only thing required
# chmod 644
rand_file = "/home/test/ad.dat"

############ Advanced Options  Ignore the following if you are not
interested
# chmod 666
rand_data = "/home/test/data.txt"
# chmod 666
rand_ip   = "/home/test/iplog.txt"
# number of ip to store in iplog.txt
number_ip = 200
########################################################
#
# That's it, setup ends.
#
########################################################

import os
import string
import whrandom

#os.environ["REMOTE_ADDR"] = "127.0.0.1"

FileExist = 0
advanced = 0
line = ''
print "Content-type: text/html\n\n",

try:
 DATA = open(rand_data, 'r')
 IP   = open(rand_ip, 'r')
except IOError:
 pass
else:
 FileExist = 1
 DATA.close()
 IP.close()

if FileExist == 1 and number_ip > 0 :
 advanced = 1

#############################

try:
 RANDEX = open(rand_file, 'r')
except IOError:
 print "Cannot open " + rand_file
else:
 line = RANDEX.read()
 RANDEX.close

codes = string.split(line, "[%%BREAK%%]")
index = whrandom.choice(range(len(codes)))

#############################

if advanced == 1 and len(codes) > 1 :
 ##################
 # Check IP
 try:
  IP = open(rand_ip, 'r')
 except IOError:
  print "Cannot open " + rand_ip
 else:
  line = IP.read()
  lines = string.split(line, "\n")
  IP.close

 for line in lines :
  hit = string.split(line, "||", 1)
  if hit[0] == os.environ["REMOTE_ADDR"] :
   lastid = hit[1]
   while index == lastid :
    index = iwhrandom.choice(range(len(codes)))



 try:
  IP = open(rand_ip, 'w')
 except IOError:
  print "Cannot open " + rand_ip
 else:
  IP.write(os.environ["REMOTE_ADDR"] + '||' + str(index) + '\n')
  count = 1
  for line in lines :
   hit = string.split(line, "||", 1)
   if hit[0] != os.environ["REMOTE_ADDR"] and count < number_ip :
    IP.write(line + "\n")
    count = count + 1


  IP.close

 ##################
 # Count hits
 try:
  DATA = open(rand_data, 'r')
 except IOError:
  print "Cannot open " + rand_data
 else:
  line = DATA.read()
  line = line[:len(line)-1]
  lines = string.split(line, "\n")
  DATA.close

 flag = 0
 try:
  DATA = open(rand_data, 'w')
 except IOError:
  print "Cannot open " + rand_data
 else:
  for line in lines:
   m = string.find(line, "html" + str(index) + "|")
   if m != -1:
    info = string.split(line, "|", 1)
    info[1] = int(info[1]) + 1
    DATA.write("html" + str(index) + "|" + str(info[1]) + "\n")
    flag = 1
   else :
    DATA.write(line + "\n")

  if flag == 0:
   DATA.write("html" + str(index) + "|1\n")

  DATA.close


print codes[index]
===== CODE: End =====

Thank you very much!
-Chris
--
Christian Gersch, chris@team.isneurope.com
:: www.neukunden.myisn.de - Geld für Ihre Startseite ::




------------------------------

Date: 12 Feb 2001 19:05:20 GMT
From: holger.marzen@sik-gmbh.de (Holger Marzen)
Subject: Replacing single-letter words doesn't work
Message-Id: <slrn98gcvg.c48.holger.marzen@sally.sik-gmbh.de>

I want to delete single-letter words (or replace them by a space) in a
string and tried:

$suchtext =~ s/([\^ ])[^ ][ \^]/ /g;

However, it does not work. "foo a b c d e" is changed to "foo b d".
Any ideas?


------------------------------

Date: Mon, 12 Feb 2001 20:26:11 GMT
From: David Ness <DNess@Home.Com>
Subject: Re: Replacing single-letter words doesn't work
Message-Id: <3A8846ED.3CFBF224@Home.Com>

Holger Marzen wrote:
> 
> I want to delete single-letter words (or replace them by a space) in a
> string and tried:
> 
> $suchtext =~ s/([\^ ])[^ ][ \^]/ /g;
> 
> However, it does not work. "foo a b c d e" is changed to "foo b d".
> Any ideas?

I think I'd try something based on: 
     s/(\b)\S(\b)/$1$2/g;
where the $1 and/or $2 only appear if you want to preserve the `break'
whitespace...


------------------------------

Date: Mon, 12 Feb 2001 20:42:10 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Replacing single-letter words doesn't work
Message-Id: <x74rxzocpo.fsf@home.sysarch.com>

>>>>> "DN" == David Ness <DNess@Home.Com> writes:

  DN> Holger Marzen wrote:
  >> 
  >> I want to delete single-letter words (or replace them by a space) in a
  >> string and tried:
  >> 
  >> $suchtext =~ s/([\^ ])[^ ][ \^]/ /g;
  >> 
  >> However, it does not work. "foo a b c d e" is changed to "foo b d".
  >> Any ideas?

  DN> I think I'd try something based on: 
  DN>      s/(\b)\S(\b)/$1$2/g;
  DN> where the $1 and/or $2 only appear if you want to preserve the `break'
  DN> whitespace...

\b is an ZERO-WIDTH assertion which cannot be grabbed. so putting parens
around them and using $1 and $2 there is meaningless.

if the OP would show some example input and output it would be easier to
figure out what he wants. this works in a general way. it can be fine
tuned by replacing \w with a char class.

perl -pe 's/\b\w\b/ /g'

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: Mon, 12 Feb 2001 20:48:40 GMT
From: David Ness <DNess@Home.Com>
Subject: Re: Replacing single-letter words doesn't work
Message-Id: <3A884C32.DF1FAAA2@Home.Com>

Uri Guttman wrote:
> 
> 
> \b is an ZERO-WIDTH assertion which cannot be grabbed. so putting parens
> around them and using $1 and $2 there is meaningless.
> 
> if the OP would show some example input and output it would be easier to
> figure out what he wants. this works in a general way. it can be fine
> tuned by replacing \w with a char class.
> 
> perl -pe 's/\b\w\b/ /g'
> 
> uri
> 

Very instructive, thanks. I guess I had always used it in circumstances
where that happened (as you point out, apparently by inadvertance on my part)
was the right thing, so I never looked deeper. One of the rare occasions where
my programming luck was `too good' I guess.


------------------------------

Date: 12 Feb 2001 21:37:11 GMT
From: holger.marzen@sik-gmbh.de (Holger Marzen)
Subject: Re: Replacing single-letter words doesn't work
Message-Id: <slrn98gls7.ca6.holger.marzen@sally.sik-gmbh.de>

On Mon, 12 Feb 2001 20:42:10 GMT, Uri Guttman wrote:

>perl -pe 's/\b\w\b/ /g'

Thanks. This does the job. Where can I find some information about
\b, \w and so on?


------------------------------

Date: Mon, 12 Feb 2001 22:13:24 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Replacing single-letter words doesn't work
Message-Id: <3a886003.1641$32f@news.op.net>

In article <slrn98gcvg.c48.holger.marzen@sally.sik-gmbh.de>,
Holger Marzen <holger.marzen@sik-gmbh.de> wrote:
>I want to delete single-letter words (or replace them by a space) in a
>string and tried:
>
>$suchtext =~ s/([\^ ])[^ ][ \^]/ /g;
>
>However, it does not work. "foo a b c d e" is changed to "foo b d".

With s///g, each substitution starts after the previous one finsihes;
they do not overlap.  For example, consider this:

        s/xY/x/g;

If this is given "xYYxY", you might think it would replace the initial
"xY" with "x" yielding "xYxY", and then replace the new initial "xY"
with "x" yielding "xxY" and then the final "xY", yielding "xx", but it
does not do this.

After Perl replaces the first "xY", yielding "xYxY", it looks *further
on* in the string for more things to replace.  It finds and replaces
the final "xY", and the final result is "xYx".

To get the behavior you expected, the usual idiom is:

        1 while $suchtext =~ s/([\^ ])[^ ][ \^]/ /;

This turns $suchtext into "foo e".

However, I also note several problems with your regex.  The
parentheses (...) are not doing anything, which suggests that you put
them in for some purpose which is not actually being accomplished.
You have [\^ ] in one place and [ \^], but these mean the sanme thing,
which suggests that you thnik there is a difference, but there isn't.
The [ \^] is bizarre by itself, because it means to match a single
character that is either a space or a '^' character---probably not
what you meant.

To fix, see the other messages in this thread.


-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


------------------------------

Date: Mon, 12 Feb 2001 22:15:51 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Replacing single-letter words doesn't work
Message-Id: <x7zofrmtt4.fsf@home.sysarch.com>

>>>>> "HM" == Holger Marzen <holger.marzen@sik-gmbh.de> writes:

  HM> On Mon, 12 Feb 2001 20:42:10 GMT, Uri Guttman wrote:
  >> perl -pe 's/\b\w\b/ /g'

  HM> Thanks. This does the job. Where can I find some information about
  HM> \b, \w and so on?

where you can find all the other information about perl, in its copious
documentation. in particular perlre covers perl's regular expressions.
also the book mastering regular expressions is very good too.

NOTE for all the other perl newbies: notice how this poster ASKED about
how to find more info. we didn't have to tell him to rtfm or bring up
the fish stuff. learn from this. ask about a specific problem and ask
for more help in that area. you will learn perlmuch faster and better
that way.

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: Mon, 12 Feb 2001 15:10:04 -0600
From: Bin - Lu <b0l4549@cs.tamu.edu>
Subject: socket problem
Message-Id: <Pine.SOL.4.10.10102121503140.12267-100000@robert>

Hi,

I'm using the following code, which is running fine on a server (perl
5.005 on Unix, apache) but fails to work on another (perl 5.005 on Linux,
apache).

---------------------------
$remote = $LSERV_ADDR;
$port = $LSERV_PORT;
if ($port =~ /\D/) {
  $port = getservbyname($port, 'tcp');
}
$iaddr = inet_aton ($remote);
$paddr = sockaddr_in ($port, $iaddr);

$proto = getprotobyname ('tcp');
socket (SOCK, PF_INET, SOCK_STREAM, $proto);
connect (SOCK, $paddr);                               

send(SOCK, $sendbuf, $buflen, 0); 
-----------------------------

In the second case, the sockets are set up but the data is not sent to
the socket which is supposed to receive. The receiver program (in C) has
always been on the Linux server in both cases.

Anyone has a clue what the problem is? I'd really appreciate it!

Bin



------------------------------

Date: Mon, 12 Feb 2001 19:12:50 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: sort of about sort
Message-Id: <x77l2vogul.fsf@home.sysarch.com>

>>>>> "GB" == Greg Bacon <gbacon@HiWAAY.net> writes:

  GB> I wonder how much of a win it would be to have a special builtin
  GB> comparison for Schwartzian Transforms, e.g., $a->[0] <=> $b->[0].
  GB> Would the invariant have to go in the 0th position?  Joseph Hall
  GB> pointed out that it would be nice if there were an XS interface
  GB> for providing one's own quick comparisons.

you have to handle many possible cases. string vs. number, normal or
reverse order, etc. also supporting multiple keys is an issue.
extracting the key into $a->[0] is another important issue. by using the
GRT you remove the need for a special XS callback as well. i have a
partially written moduledust gathering that supports complex key
extraction and autogenerates that code and then performs a GRT sort on
the data. it needs a fair amount of work but the principle design ideas
are valid. if anyone wants to take it on i will be glad to help. i don't
have the time to drive that project. it has fairly ambitious goals and
requires some decent coding skills to make it clean and fast. any
volunteers?

among other things, the module could be used to generate code that can
be cut and pasted into your programs. we could even support the common
cases directly so again no need for special XS code.

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: 12 Feb 2001 20:46:50 GMT
From: inwap@best.com (Joe Smith)
Subject: Re: subroutine woes
Message-Id: <969i3q$e18$1@nntp1.ba.best.com>

In article <966o6h$89p$1@uranium.btinternet.com>,
Richard Brown <anarch1st@yahoo.com> wrote:
>print h2('File MIME type'), uploadInfo($_[0])->{'Content-Type'};
>    push(@test,<TEST>);
>    while (<TEST>) {
>      #$length += length($_);
>    }

The push(@test,<TEST>) statement does not read just one line.
It read all the input from the TEST file handle, reading until
end of file.  Therefore the code inside the while(<TEST>){} will
never be executed.

And if uploadInfo($_[0]) does not return a reference to a hash, you're
screwed.  You need to test to see if that value is defined before
using the -> operator on it.
	-Joe
--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.


------------------------------

Date: Mon, 12 Feb 2001 19:04:53 -0000
From: Chris Stith <mischief@velma.motion.net>
Subject: Re: the best solution?
Message-Id: <t8gcul2npflo1a@corp.supernews.com>

Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
> Chris Fedde <cfedde@fedde.littleton.co.us> wrote in comp.lang.perl.misc:

>>#/usr/bin/perl -w
>>#
>># Please excuse jeopardy style posting

[snip]

>>In article <v_Ug6.63$s4.218@news.indigo.ie>,
>>Tony O Sullivan <tony@codeisland.net> wrote:
>>>Hi all,
>>>I know this seems like a simple problem but I can't figure out the best way

> [snip problem]

> Correct solution posted in jeopardy position.  You know better
> than that, Chris.  Furrfu!

Tbbq pnyy, Naab!

I think Chris is close (the other Chris -- Chris Fedde -- that
is...) to what the OP wants. I think, though, the OP wanted to
search substrings of a string. This is what the OP was asking
about, IMHO:

#!/usr/bin/perl -w
use strict;

    my $string = 'substring of string';
    my $temp_string = $string;
    my $count = $temp_string =~ s/string//g;
#    print "$count\t$string\n";

It would work, but this is simpler (and in my tests, faster
although not very much so):

#!/usr/bin/perl -w
use strict;

    my $string = 'substring of string';
    my $count = $string =~ s/string/string/g;
#    print "$count\t$string\n";

CS => Chris Stith
OP => Original Poster (as I understand the question)

Benchmark: running CS, OP, each for at least 10 CPU seconds...
        CS:  9 wallclock secs @ 120616.78/s (n=1207374)
        OP: 11 wallclock secs @ 114564.24/s (n=1146788)

The speed advantage isn't entirely clear to me. Perhaps
the removal and adding back in with s/// is caught by the
interpreter as a definite no-op. Perhaps the extra assignment
slows things down that much. The difference should be
negligible in most cases. On other systems there might be
some strange magic that speeds up the former, although I
doubt that is the case. The latter method is probably going
to use ever so slightly less memory and run very slightly
faster than the former.

Either of these will run slower on my machine I'm testing
this on ( K62 500, 128Megs RAM, Linux Mandrake 7.0, Perl
version 5.005_03 built for i386-linux ) than the following,
which I believe is the other option the OP asked about:

    $count = 0;
    $count++ while( $string =~ /string/g );

Here are the speed results, which are substantial in the OP's
case of very stringent demands:

Benchmark: running CS, OP, OP_option, each for at least 10 CPU seconds...
        CS: 11 wallclock secs @ 123317.61/s (n=1260306)
        OP:  8 wallclock secs @ 112332.77/s (n=1124451)
 OP_option: 11 wallclock secs @ 178768.23/s (n=1789470)

Here's the exact code I used for this test. For those taking
only a quick glance, I named the looping method OP_option:

#----------------------------------------
#!/usr/bin/perl -w
use strict;
use Benchmark;

my $string = 'substring of string';
my $count = 0;

timethese -10, {
    'CS' => \&CS,
    'OP' => \&OP,
    'OP_option' => \&OP_option,
};

print "$count\t$string\n";

sub CS {
    $count = $string =~ s/string/string/g;
}

sub OP {
    my $temp_string = $string;
    $count = $temp_string =~ s/string//g;
}

sub OP_option {
    $count = 0;
    $count++ while( $string =~ /string/g );
}
#------------------------------------------

The question remains, though, why would you use Perl if
memory and speed are the most important factors? Use Perl
if programmer speed is the most important factor. ;) Use
C if program speed is the most important (unless you're
in a really tight loop inside a device driver or library
specific to a platform, in which case you use assembly).

Chris
-- 
Christopher E. Stith

Product shown enlarged to make you think you're getting more.



------------------------------

Date: 12 Feb 2001 15:42:24 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: the best solution?
Message-Id: <m3itmfljkf.fsf@mumonkan.sunstarsys.com>

Chris Stith <mischief@velma.motion.net> writes:

> The speed advantage isn't entirely clear to me. Perhaps
> the removal and adding back in with s/// is caught by the
> interpreter as a definite no-op. Perhaps the extra assignment
> slows things down that much. The difference should be
> negligible in most cases. On other systems there might be
> some strange magic that speeds up the former, although I
> doubt that is the case. The latter method is probably going
> to use ever so slightly less memory and run very slightly
> faster than the former.

I agree that it's not clear at all.  You appear to base your 
rationale on the results of a single benchmark on a single string 
on a single machine on a single perl version.  There's quite a lot 
of conjecture and extrapolation going on in your statements 
just to validate a minor performance difference.

> Here are the speed results, which are substantial in the OP's
> case of very stringent demands:
> 
> Benchmark: running CS, OP, OP_option, each for at least 10 CPU seconds...
>         CS: 11 wallclock secs @ 123317.61/s (n=1260306)
>         OP:  8 wallclock secs @ 112332.77/s (n=1124451)
>  OP_option: 11 wallclock secs @ 178768.23/s (n=1789470)

You forgot to try varying the string; try running it with 

    my $string = 'substring of string' x 100;

In that case, on linux 5.005_03 I don't see any performance 
difference between them; and on 5.6 the advantage shifts 
significantly (~20%) to the s/string//g algorithm.

Big deal.

> The question remains, though, why would you use Perl if
> memory and speed are the most important factors? Use Perl
> if programmer speed is the most important factor. ;) Use
> C if program speed is the most important (unless you're
> in a really tight loop inside a device driver or library
> specific to a platform, in which case you use assembly).

Mostly agreed.  Perl code doesn't need to carry a lot of
ornamental baggage either, which can make it a joy to
change and maintain.

As a few side notes, there is another way to do it:

  sub GZ {
    my $i = $count = 0;
    ++$count while ( ($i = 1+index($string,"string", $i)) > 0);
  }

and, in the course of checking your results,
I learned that the following code

  % perl -e 'study($_=q(substring of string)); 1 while /string/g;'

locks up on 5.005_03; 5.6 handles it fine though.

Best.
--
Joe Schaefer     "If one studies too zealously, one easily loses his pants."
                                               --Albert Einstein



------------------------------

Date: 12 Feb 2001 16:27:32 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: the best solution?
Message-Id: <m3elx3lhh7.fsf@mumonkan.sunstarsys.com>

Joe Schaefer <joe+usenet@sunstarsys.com> writes:

> You forgot to try varying the string; try running it with 
> 
>     my $string = 'substring of string' x 100;
> 
> In that case, on linux 5.005_03 I don't see any performance 
> difference between them; and on 5.6 the advantage shifts 
> significantly (~20%) to the s/string//g algorithm.
  ^^^^^^^^^^^^^^^^^^^^

No- I think I got that wrong.  The tests I ran using 5.6 had a 

        study $string;

line in the setup, which apparently caused a slowdown in the
OP_option sub.  After commenting it out, there doesn't seem 
to be any appreciable difference on my box for 5.6, either.

> Big deal.

And another, even slower and more obscure solution:

  sub LAiSC {
        $count = () = $string =~ /string/g;  
  }

-- 
Joe Schaefer      "I am not one of those who in expressing opinions confine
                                    themselves to facts."
                                               --Mark Twain


------------------------------

Date: Mon, 12 Feb 2001 22:36:50 +0100
From: "Rob Donovan" <rob@robdon.com>
Subject: Timezones
Message-Id: <3a885aa0_2@goliath.newsfeeds.com>

Hi,


I know about localtime & gmtime routines, but does anyone know how to
convert from GMT to anyother timezone without having to know the hours
difference.  IE just give the routines the country code or something...


Thanks,

Rob Donovan




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----


------------------------------

Date: 12 Feb 2001 13:10:13 -0800
From: Gerard Lanois <gerard@NOSPAMlanois.com>
Subject: Re: XML: "Flattening" XML to dot notation - best module?
Message-Id: <uvgqfy5e2.fsf@NOSPAMlanois.com>

clintp@geeksalad.org (Clinton A. Pierce) writes:

> In article <id698tsakcm68sqmk17vs295upqobav75i@4ax.com>,
> 	Bart Lateur <bart.lateur@skynet.be> writes:
> > Matt Kruse wrote:
> > 
> >>I want to "flatten" XML into dot notation. For example:
> >><foo><bar>X</bar><bar2>Y</bar2></foo>
> >>
> >>Becomes: foo.bar = X, foo.bar2 = Y
> >>
> > This doesn't quite work:
> > [code trimmed]
> > but mainly because XML::Parser doesn't ignore whitespace. So you must
> > weed them out.
> > 
> > Careful: text can be split over multiple calls to the Char handler. So,
> > you ought to append the text to a string, and not print it out straight
> > away.
> 
> This does quite work, well mostly.
> 
> [...some code...]

I wanted to do roughly the same thing.  I found XML::PatAct::ToObjects
did exactly what I wanted, in a very flexible and easily maintainable
way.

Go here:

       http://bitsko.slc.ut.us/libxml-perl/

 ...and follow the link for "XML::PatAct::ToObjects".

See especially the "EXAMPLES" section.


I think the full address is 

http://bitsko.slc.ut.us/libxml-perl/XML%3A%3APatAct%3A%3AToObjects.html

Using this technique, you just have to write a schema instead of
maintaining any parser code.

Does anyone have any experiences, positive or negative, with
XML::PatAct::ToObjects?


-Gerard
http://www.lanois.com/perl/




------------------------------

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 276
**************************************


home help back first fref pref prev next nref lref last post