[19593] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1788 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Sep 21 14:06:33 2001

Date: Fri, 21 Sep 2001 11:05:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1001095510-v10-i1788@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 21 Sep 2001     Volume: 10 Number: 1788

Today's topics:
    Re: a question about comments in perl <rob_13@excite.com>
    Re: ANNOUNCE: grepmail 4.51 released <newspost@coppit.org>
        better if/else statement than this. (Bikesh Patel)
    Re: better if/else statement than this. (Randal L. Schwartz)
    Re: better if/else statement than this. (Randal L. Schwartz)
    Re: better if/else statement than this. <ren@tivoli.com>
    Re: better if/else statement than this. (Randal L. Schwartz)
    Re: converting 20010921 into 21-09-2001 (John J. Trammell)
    Re: converting 20010921 into 21-09-2001 <simon.oliver@umist.ac.uk>
    Re: DBI/MySQL help needed (martinblack)
    Re: dead image links <rob_13@excite.com>
    Re: installing modules in WinNT <eli@there-is-no-more-qzto.com>
    Re: installing modules in WinNT <whatever@nevermind.invalid>
        Newbie help with mailing list <damien.burke@harcotechnology.com>
    Re: Newbie help with mailing list (Chris Fedde)
    Re: Newbie help with mailing list <rob_13@excite.com>
    Re: Pre spawning in perl (Mark Jason Dominus)
    Re: Schwartzian Transform problem <mjcarman@home.com>
        Simple question (Dimitri)
    Re: Simple question (Mark Jason Dominus)
    Re: Simple question <comdog@panix.com>
    Re: Simple question nobull@mail.com
    Re: Simple question <ren@tivoli.com>
    Re: String comparison nobull@mail.com
    Re: using a variable to call a function <uri@sysarch.com>
        who said this? <sun_tong@users.sourceforge.net>
    Re: win32 stat in directory with 4682 files <dtweed@acm.org>
    Re: win32 stat in directory with 4682 files <Peter.Dintelmann@dresdner-bank.com>
    Re: win32 stat in directory with 4682 files <peter_korman@lotus.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 21 Sep 2001 17:22:37 GMT
From: "Rob - Rock13.com" <rob_13@excite.com>
Subject: Re: a question about comments in perl
Message-Id: <Xns9123880775874rock13com@64.8.1.226>

Josef Möllers <news:3BAAFB5D.1200AC3D@fujitsu-siemens.com>:

> Rene Scheibe wrote:
>> Can someone tell me how to not just comment out one line with
>> # but a whole section? 
> 
> Can't you use pod style sections?

Yes. perldoc -q comment

This is discussed in perlfaq7

If you run it through a pod2* though these comments might not be 
ignored.
-- 
Rob - http://rock13.com/
Web Stuff: http://rock13.com/webhelp/


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

Date: Fri, 21 Sep 2001 12:20:11 -0400
From: David Coppit <newspost@coppit.org>
Subject: Re: ANNOUNCE: grepmail 4.51 released
Message-Id: <3BAB68BB.1070704@coppit.org>

Anno Siegel wrote:

> According to Edward Avis  <epa98@doc.ic.ac.uk>:
> 
>>Ideally the source code for all these programs would be searchable, so
>>if you're looking for examples of using HTTP::Response, you'd just
>>type the module name into a search box and browse through code that
>>uses it.
> 
> Quite.  A repository of (mostly simple) applications that use mainly
> one specific module, associated with that module, would be a good idea.
> The problem I see is, who would make and/or approve of this association.


I like the searchable scripts idea too, although this may be difficult 
to implement in practice given that scripts may consist of several code 
& non-code files. Perhaps the upload service can extract the list of 
modules used and just list them in the script description. That way the 
new user can decide which scripts look simple enough to tackle based on 
the number of modules used.

Extracting the module dependencies could be implemented as:
- requirements stated in Makefile.PL
- a search for "use X" or "require X" in the code
- a CPAN-recognized section in the script POD, such as "MODULES USED",
   where the author can list the dependencies.

Come to think of it, I think that listing module dependencies would be a 
good idea for modules too.

David




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

Date: 21 Sep 2001 09:35:41 -0700
From: bikesh@my-deja.com (Bikesh Patel)
Subject: better if/else statement than this.
Message-Id: <728ad7c2.0109210835.445dfb32@posting.google.com>

Is there better way to write this?
The output i'm looking for is 0001
or 1111 if the hex is F and so on...
Thanks a bunch
bikesh




$number = hex("1");

 if ($number >= 8 ) {
        @loadstatus = 1;
        $number = $number - 8;
 }
 else {
        @loadstatus = 0;
 }

 if ($number >= 4 ) {
        push(@loadstatus,1);
        $number = $number - 4;
 }
 else {
        push(@loadstatus,0);
 }


 if ($number >= 2 ) {
        push(@loadstatus,1);
        $number = $number - 2;
 }
 else {
        push(@loadstatus,0);
 }


 if ($number >= 1 ) {
        push(@loadstatus,1);
 }
 else  {
        push(@loadstatus,0);
 }


print "@loadstatus\n";


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

Date: 21 Sep 2001 09:53:55 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: better if/else statement than this.
Message-Id: <m1sndg7ae4.fsf@halfdome.holdit.com>

>>>>> "Bikesh" == Bikesh Patel <bikesh@my-deja.com> writes:

Bikesh> Is there better way to write this?
Bikesh> The output i'm looking for is 0001
Bikesh> or 1111 if the hex is F and so on...

for (map { sprintf "%x", $_ } 0..15 ) {
  print "$_ is ", substr(unpack("B*", pack "N", hex $_), -4), "\n";
}

==>

0 is 0000
1 is 0001
2 is 0010
3 is 0011
4 is 0100
5 is 0101
6 is 0110
7 is 0111
8 is 1000
9 is 1001
a is 1010
b is 1011
c is 1100
d is 1101
e is 1110
f is 1111

print pack "B*", "01001010011101010111001101110100001000000110000101101110011011110111010001101000011001010111001000100000010100000110010101110010011011000010000001101000011000010110001101101011011001010111001000101100"

-- 
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: 21 Sep 2001 10:00:50 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: better if/else statement than this.
Message-Id: <m1lmj87a2l.fsf@halfdome.holdit.com>

>>>>> "Randal" == Randal L Schwartz <merlyn@stonehenge.com> writes:

>>>>> "Bikesh" == Bikesh Patel <bikesh@my-deja.com> writes:
Bikesh> Is there better way to write this?
Bikesh> The output i'm looking for is 0001
Bikesh> or 1111 if the hex is F and so on...

Randal> for (map { sprintf "%x", $_ } 0..15 ) {
Randal>   print "$_ is ", substr(unpack("B*", pack "N", hex $_), -4), "\n";
Randal> }

Randal> ==>

Randal> 0 is 0000
Randal> 1 is 0001
Randal> 2 is 0010
Randal> 3 is 0011
Randal> 4 is 0100
Randal> 5 is 0101
Randal> 6 is 0110
Randal> 7 is 0111
Randal> 8 is 1000
Randal> 9 is 1001
Randal> a is 1010
Randal> b is 1011
Randal> c is 1100
Randal> d is 1101
Randal> e is 1110
Randal> f is 1111

Or (duh) simpler:

for (map { sprintf "%x", $_ } 0..15 ) {
  print "$_ is ", substr(unpack("B*", pack "h*", $_), 0), "\n";
}

-- 
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: 21 Sep 2001 12:29:46 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: better if/else statement than this.
Message-Id: <m34rpwih9x.fsf@dhcp9-161.support.tivoli.com>

On 21 Sep 2001, merlyn@stonehenge.com wrote:

>>>>>> "Randal" == Randal L Schwartz <merlyn@stonehenge.com> writes:
> 
>>>>>> "Bikesh" == Bikesh Patel <bikesh@my-deja.com> writes:
> Bikesh> Is there better way to write this?
> Bikesh> The output i'm looking for is 0001
> Bikesh> or 1111 if the hex is F and so on...
> 
> Randal> for (map { sprintf "%x", $_ } 0..15 ) {
> Randal>   print "$_ is ", substr(unpack("B*", pack "N", hex $_),
> Randal>   -4), "\n";
> Randal> }

[output snipped]

> Or (duh) simpler:
> 
> for (map { sprintf "%x", $_ } 0..15 ) {
>   print "$_ is ", substr(unpack("B*", pack "h*", $_), 0), "\n";
> }

(I assume you meant "4" instead of "0" on that substr, as before.)

How about just:

  printf "%x is %04b\n", $_, $_ for 0..15;

-- 
Ren Maddox
ren@tivoli.com


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

Date: 21 Sep 2001 10:44:28 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: better if/else statement than this.
Message-Id: <m1zo7o5thf.fsf@halfdome.holdit.com>

>>>>> "Ren" == Ren Maddox <ren@tivoli.com> writes:

>> for (map { sprintf "%x", $_ } 0..15 ) {
>> print "$_ is ", substr(unpack("B*", pack "h*", $_), 0), "\n";
>> }

Ren> (I assume you meant "4" instead of "0" on that substr, as before.)

Yes.  Too hasty at posting.

Ren> How about just:

Ren>   printf "%x is %04b\n", $_, $_ for 0..15;

Works fine if you have 5.6.x.  A lot of production machines I have
around here are still 5.5.3.

-- 
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: 21 Sep 2001 15:07:02 GMT
From: trammell@haqq.hypersloth.invalid (John J. Trammell)
Subject: Re: converting 20010921 into 21-09-2001
Message-Id: <slrn9qmlsm.nvg.trammell@haqq.hypersloth.net>

On 21 Sep 2001 07:50:12 -0700, Martin <martin.van.den.berg@rivm.nl> wrote:
> There must be an easy way to convert a date string like 20010921 into
> a readable string like 21-09-2001 (european format).
> Please help me out!

[ ~ ] perl -pe 's/(....)(..)(..)/$3-$2-$1/'
20010921
21-09-2001
foobarbaz
ba-ar-foobz
[ ~ ]

-- 
Rule #0: Spam is theft.


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

Date: Fri, 21 Sep 2001 16:43:14 +0100
From: "Simon Oliver" <simon.oliver@umist.ac.uk>
Subject: Re: converting 20010921 into 21-09-2001
Message-Id: <3bab6380$1@news.umist.ac.uk>

I suggest you take a two pass approach:

my ($y, $m, $d) = unpack "a4a2a2", '20010921';

my $euro = sprintf "%02d-%02d-%04d", $d, $m, $y;


"Martin" <martin.van.den.berg@rivm.nl> wrote in message
news:2a468b35.0109210650.5797a870@posting.google.com...
> There must be an easy way to convert a date string like 20010921 into
> a readable string like 21-09-2001 (european format).
> Please help me out!
>
> Thanks




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

Date: 21 Sep 2001 10:11:25 -0700
From: martinblack26@yahoo.com (martinblack)
Subject: Re: DBI/MySQL help needed
Message-Id: <c025943b.0109210911.f8aadc0@posting.google.com>

I changed the SQL, got rid of the comma and it worked as expected... A
very big thanks to all who helped out.
Cheers, 
Martinblack


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

Date: Fri, 21 Sep 2001 17:53:25 GMT
From: "Rob - Rock13.com" <rob_13@excite.com>
Subject: Re: dead image links
Message-Id: <Xns91238D3FE27C8rock13com@64.8.1.226>

Chuck Kahn <news:M6Iq7.38841$Aa7.2359934@news20.bellglobal.com>:

>     $URL[0] = "http://dailynews.yahoo.com/headlines/";
>     $URL[1] = "http://dailynews.yahoo.com/headlinezzz/";
>     $URL[2] =
> "http://images.amazon.com/images/P/0130799807.01.TZZZZZZZ.jpg";
>     $URL[3] =
> "http://images.amazon.com/images/P/0130895547.01.TZZZZZZZ.jpg";
> 
>     foreach $item (@URL)
>     {
>         $ua = LWP::UserAgent->new;
>         $request = HTTP::Request->new('GET', $item);
>         $response = $ua->request($request)->status_line();
> 
>         print "[$response] $item\n";
>     }
> 
> In the example above, $URL[1] and $URL[3] are invalid, but
> $URL[3] does not come up as invalid:
> 
> [200 OK] http://dailynews.yahoo.com/headlines/
> [404 Not Found] http://dailynews.yahoo.com/headlinezzz/
> [200 OK]
> http://images.amazon.com/images/P/0130799807.01.TZZZZZZZ.jpg 
> [200 OK]
> http://images.amazon.com/images/P/0130895547.01.TZZZZZZZ.jpg 
> 
> Is there a better way to find invalid image links?

It seems images.amazon.com is deliberately not giving a 404 so as 
not to produce broken images. When I request one of those URLs with 
telent its sending a GIF--at least it sends that intially. Though 
for $URL[2] it sends more data.

-- 
Rob - http://rock13.com/
Web Stuff: http://rock13.com/webhelp/


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

Date: 21 Sep 2001 16:23:51 GMT
From: Eli the Bearded <eli@there-is-no-more-qzto.com>
Subject: Re: installing modules in WinNT
Message-Id: <eli$0109211222@qz.little-neck.ny.us>

In comp.lang.perl.misc, asif  <whatever@nevermind.invalid> wrote:
> Eli the Bearded wrote:
> > I got a recent ActiveState perl. I tried to install it on a WinNT
 ...
> > But ppm does not work. I need to install one non-standard module
> > for an app to work: Time::HiRes.
> Unless there's a specific reason for not using it, download the MSI 
> version.

Okay, Did that, it seems to work. I avoided it at first since I
had no idea what "MSI" meant.

> I had similar problems with an NT install. Deleting everything, going to 
> MS' site and getting the MSI application, downloading the MSI version of 
> ASPerl and installing using the MSI installer solved 99.9% of my problems.

Modules install nicely, double clicking perl scripts works, but I can't
right click and 'Open With...' perl scripts. Anything simple to fix
that?

Elijah
------
vastly prefers not using Windows


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

Date: Fri, 21 Sep 2001 16:54:15 GMT
From: asif <whatever@nevermind.invalid>
Subject: Re: installing modules in WinNT
Message-Id: <3BAB70C9.5010000@nevermind.invalid>

Eli the Bearded wrote:

<snip/>


> 
> Modules install nicely, double clicking perl scripts works, but I can't
> right click and 'Open With...' perl scripts. Anything simple to fix
> that?


I get an option to "Open" the file, which runs the script when right 
clicking. Open==Run!? Were you hoping to have it open in an editor?


> 
> Elijah
> ------
> vastly prefers not using Windows


Likewise.

/whatever






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

Date: Fri, 21 Sep 2001 16:51:40 +0100
From: "Damien Burke" <damien.burke@harcotechnology.com>
Subject: Newbie help with mailing list
Message-Id: <9ofnlg$lgk$1@uranium.btinternet.com>

Hi,

I need to send out a HTML e-mail to a mailing list of 500+ people.
I want each recipient to see only their name in the 'To' field.
Does anyone know any good cgi scripts to do this or have any advice?

Thanks,
Damien




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

Date: Fri, 21 Sep 2001 15:56:23 GMT
From: cfedde@fedde.littleton.co.us (Chris Fedde)
Subject: Re: Newbie help with mailing list
Message-Id: <HqJq7.585$Owe.253168640@news.frii.net>

In article <9ofnlg$lgk$1@uranium.btinternet.com>,
Damien Burke <damien.burke@harcotechnology.com> wrote:
>Hi,
>
>I need to send out a HTML e-mail to a mailing list of 500+ people.
>I want each recipient to see only their name in the 'To' field.
>Does anyone know any good cgi scripts to do this or have any advice?
>
>Thanks,
>Damien
>

I'm confused.  What does CGI have to do with Spam?
Still it's prety easy to send mail from perl.  Lots of resources 
are available if you just look at the usual search engines (google, yahoo,
lycos, etc) CGI resources can be found in a similar way.

Good Luck
-- 
    This space intentionally left blank


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

Date: Fri, 21 Sep 2001 17:58:16 GMT
From: "Rob - Rock13.com" <rob_13@excite.com>
Subject: Re: Newbie help with mailing list
Message-Id: <Xns91238E12D8E63rock13com@64.8.1.226>

Damien Burke <news:9ofnlg$lgk$1@uranium.btinternet.com>:

> I need to send out a HTML e-mail to a mailing list of 500+
> people.

Did they request HTML email? I've lost count of the mailing lists 
I've unsuscribed from for switching from text to HTML.

> I want each recipient to see only their name in the
> 'To' field. Does anyone know any good cgi scripts to do this or
> have any advice? 

Wrong NG. If you have a Perl CGI script for doing this that you 
want help with then that is more apropriate.

As it stands you should start with Google.com and perhaps the likes 
of cgiresources.com and perlarchive.com

I use ezmlm with qmail.

-- 
Rob - http://rock13.com/
Web Stuff: http://rock13.com/webhelp/


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

Date: Fri, 21 Sep 2001 15:39:07 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Pre spawning in perl
Message-Id: <3bab5f1a.3654$304@news.op.net>

In article <3ba939ee.6532$5c@news.op.net>,
Mark Jason Dominus <mjd@plover.com> wrote:
>I think you will want to follow an outline something like this:

There were some errors:  I had the arguments to 'accept' in the
wrong order, and I forget to check for a 0 return from 'waitpid'.
I also forgot to put a loop in to handle_clients; without this,
children handle only one client each before exiting.

This version appears to work correctly.  (That is, I have tested it.)

        #!/usr/bin/perl -w

        use POSIX ":sys_wait_h";
        use Socket;
        socket(S, AF_INET, SOCK_STREAM, 0) or die "socket: $!";
        my $port = 1234;
        my $addr = sockaddr_in($port, INADDR_ANY);
        bind(S, $addr) or die "bind: $!";
        listen(S, 5) or die "listen: $!";
        my $children = 0;
        my $MAX_CHILDREN = 10; # This many children active at once
        my $MAX_SERVICE = 100; # children exit after serving this many clients

        while (1) {
          my $dead_child;

          if ($children == $MAX_CHILDREN) {
            # Do not continue until at least one child is dead
            wait() or die "wait: $!";
            $children--;
          }

          # See if any other children are dead 
          # continue immediately if none are dead
          my $no_dead_children;
          do {
            my $res = waitpid(-1, &WNOHANG);
            $no_dead_children = ($res == -1 || $res == 0);
            --$children unless $no_dead_children ;
          } until $no_dead_children;

          # Spawn new children
          while ($children < $MAX_CHILDREN) {
            my $pid = fork();
            unless (defined $pid) {
              # couldn't fork; handle this error somehow
              die "fork: $!";
            }
            if ($pid == 0) { 
              # I am the child
              handle_clients();
              exit;
            }
            # I am the parent
            $children++;
          }
        }

        sub handle_clients {
          my $clients_served = 0;
          while ($clients_served < $MAX_SERVICE) {
            accept(NS, S);  # wait until a conection comes in
            # Now deal with the client; it is connected via NS
            close NS;
            $clients_served++;
          }
          exit;
        }

-- 
@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: Fri, 21 Sep 2001 10:26:52 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: Schwartzian Transform problem
Message-Id: <3BAB5C3C.14BB4E31@home.com>

Matt Garrish wrote:
> 
> "Michael Carman" <mjcarman@home.com> wrote:
> >
> > What?! That's completely wrong.
> 
> What?! However did you come to that conclusion nearly 12 hours
> after Randal posted his message!?!

At the time I posted, there were no other responses to your message in
my newsfeed. Randal's post still hasn't shown up in mine. Things
sometimes take the long way around in Usenet. Sometimes they get lost
completely. My newsfeed seems to be more tempermental than most. :(

> [...] I quickly replied without stopping to think.

A very dangerous thing in this ng. ;)

> So colour me stupid!

That was not my intent; I apologize if you feel that it was. I saw an
error and corrected it lest someone of lesser experience take it for
truth. There was nothing personal about it. Corrections to code are just
that and nothing more.

-mjc


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

Date: 21 Sep 2001 08:21:45 -0700
From: mauroid@csi.forth.gr (Dimitri)
Subject: Simple question
Message-Id: <a3ebf7b8.0109210721.52840b85@posting.google.com>

A very simple Perl question: Having a string say in $s, how can I count
how many times the character 'a' appears in the string? The only way I
can think of is :

$cnt = ($s =~ s/a/a/g);

But is there a faster way? The string $s is quite long, and it seems
like the above command is doing more work than it needs to (it is
replacing, not just counting).

Thanks for any help!
-Dimitri


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

Date: Fri, 21 Sep 2001 15:25:55 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Simple question
Message-Id: <3bab5c03.3592$7d@news.op.net>

In article <a3ebf7b8.0109210721.52840b85@posting.google.com>,
Dimitri <mauroid@csi.forth.gr> wrote:
>$cnt = ($s =~ s/a/a/g);

        $cnt = ($s =~ tr/a//);

is likely to be faster.

-- 
@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: Fri, 21 Sep 2001 11:33:01 -0500
From: brian d foy <comdog@panix.com>
Subject: Re: Simple question
Message-Id: <comdog-1972C4.11330121092001@news.panix.com>

In article <a3ebf7b8.0109210721.52840b85@posting.google.com>, 
mauroid@csi.forth.gr (Dimitri) wrote:

> A very simple Perl question: Having a string say in $s, how can I count
> how many times the character 'a' appears in the string? The only way I
> can think of is

http://www.perldoc.com/perl5.6.1/pod/perlfaq4.html#How-can-I-count-the-number-of-occurrences-of-a-substring-within-a-string-

-- 
brian d foy <comdog@panix.com> - Perl services for hire
CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html



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

Date: 21 Sep 2001 17:17:13 +0100
From: nobull@mail.com
Subject: Re: Simple question
Message-Id: <u9hetwecxi.fsf@wcl-l.bham.ac.uk>

mauroid@csi.forth.gr (Dimitri) writes:

> Subject: Simple question

Please use the subject line of your post to describe the subject of
your post.  'Cute' meta-non-information is just plain rude.

> A very simple Perl question: Having a string say in $s, how can I count
> how many times the character 'a' appears in the string?

See FAQ: "How can I count the number of occurrences of a substring
within a string?"

You are supposed to consult the FAQ _before_ you post.  Pop quiz: Can
you guess _why_ you are supposed to consult the FAQ _before_ you post?

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 21 Sep 2001 11:06:39 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: Simple question
Message-Id: <m3adzoil4g.fsf@dhcp9-161.support.tivoli.com>

On 21 Sep 2001, mauroid@csi.forth.gr wrote:

> A very simple Perl question: Having a string say in $s, how can I
> count how many times the character 'a' appears in the string? The
> only way I can think of is :
> 
> $cnt = ($s =~ s/a/a/g);
> 
> But is there a faster way? The string $s is quite long, and it seems
> like the above command is doing more work than it needs to (it is
> replacing, not just counting).

$cnt = $s =~ tr/a//;

-- 
Ren Maddox
ren@tivoli.com


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

Date: 21 Sep 2001 18:19:46 +0100
From: nobull@mail.com
Subject: Re: String comparison
Message-Id: <u9elp0ea19.fsf@wcl-l.bham.ac.uk>

sami@xenetic.fi (Samppa) writes:

> Here is a simple string comparison code.

> if ($line =~ /$string1/) 

Wrong, it is not simple string comparison code, it is regex pattern
match code.  Simple string comparison (equaliry) operator is 'eq'.

To look to see if one string contains another you can use index().
You can also do it using regex pattern matching.  To do simple
substing matching using regex you must escape all charaters that are
special in regex.

$line =~ /$target/; # Does $line match the regex pattern contained $target 
$line =~ $target; # Same as above but less readable

index($line,$target) > -1; # Does $line contain the string in $target 
$line =~ /\Q$target/; # Same as above but slower

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Fri, 21 Sep 2001 16:09:51 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: using a variable to call a function
Message-Id: <x7u1xwbk48.fsf@home.sysarch.com>

>>>>> "A" == Andy  <ccsam@bath.ac.uk> writes:

  A> Thanks to both of you...
  A> Thomas Bätzler <Thomas@Baetzler.de> wrote in message 
  >> 
  >> You shouldn't do that. But if you must, try this:

  A> I don't *have* to, what are the security implications if I do?
  A> Security and robustness is a priority.

symbolic references are dangerous. they can be security problems and can
lead to hard to find bugs.

  A> I have a .pm of different arrays, given the same names as some users.
  A> My intention was to:

  A> &foo ($user);

  A> With $user having a dual purpose, i.e. as the basis of `ps -fu$user`
  A> and &$user();  Therefore, is it more secure to use.. no that wont
  A> work. Should I have a separate array of valid array names to check
  A> before calling them?


use a hash or a dispatch table. the keys are the user names and the
values are the code or sub refs to call. there are many examples of this
in this group. search groups.google for 'dispatch table'


  A> btw does 'no strict "refs";' expire after the closing }?

  >> { 
  >> no strict "refs";
  >> &{$func};
  >> }

yes. but you don't need it.

  A> Security -> to another point. Although all data  being passed to my
  A> pm's *should* be secure, if I run with '-T' it complains when I use
  A> that data as a parameter for a shell command (a perfect example being
  A> `ps -fu$user`).

that is what tainting is all about. you must untaint data before it can
be used externally. it is a security feature.

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Search or Offer Perl Jobs  --------------------------  http://jobs.perl.org


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

Date: 21 Sep 2001 12:57:17 -0300
From: * Tong * <sun_tong@users.sourceforge.net>
Subject: who said this?
Message-Id: <sa8adzo7d0i.fsf@suntong.personal.users.sourceforge.net>

Hi,

I remember a saying like:

  In the windoze world, I am limited by the tools that I can use, In
  Unix, I am limited by my own wisdom.

I want to know who said it (I know he is very famous), and I want to
know the original quote. Thanks

-- 
Tong (remove underscore(s) to reply)
  *niX Power Tools Project: http://xpt.sourceforge.net/
  - All free contribution & collection


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

Date: Fri, 21 Sep 2001 15:32:09 GMT
From: Dave Tweed <dtweed@acm.org>
Subject: Re: win32 stat in directory with 4682 files
Message-Id: <3BAB5C34.68661541@acm.org>

Peter Korman wrote:
> How would one speed this up?

For one thing, posting without HTML would make it easier for people to
help you.

> 10:15:56.......................10:17:17

So you're stat()ing 4600 files in 81 seconds, or about 56.8 stats/sec.

Since you're not doing anything with the information anyway, I'd take
out the call to stat() :-)

There's nothing else you can do from inside Perl to make stat() any
faster. Try a different OS? Under NT, the filesystem runs as a separate
process, so each stat() causes at least two (and maybe more) context
switches.

Perhaps Win32::File provides an interface that would let you get the
information you need all in one go. But then your script would be tied
to Win32 machines (and maybe that's OK with you).

-- Dave Tweed


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

Date: Fri, 21 Sep 2001 17:21:43 +0200
From: "Dr. Peter Dintelmann" <Peter.Dintelmann@dresdner-bank.com>
Subject: Re: win32 stat in directory with 4682 files
Message-Id: <9oflba$8ir1@news-1.bank.dresdner.net>

    Hi Peter,

"Peter Korman" <peter_korman@lotus.com> wrote in message
news:3BAB4D53.A820FB65@lotus.com...
How would one speed this up?
---------------------------------------------------------
use cwd;
@when=localtime();
print $when[2], ":" , $when[1],  ":" , $when[0];
chdir "G:/ENGLISH/WIN2000/ADV_SERV/I386";
opendir D, ".";
@direntries=readdir(D);
foreach $i (@direntries)
{ 

    maybe I am wrong but I assume that it is faster to read an 
    entry at a time (because you are not building a list first)

        while (defined(my $i = readdir D))
        {
        }

    Just give it a try.

    Regards,

        Peter Dintelmann








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

Date: Fri, 21 Sep 2001 12:08:17 -0400
From: Peter Korman <peter_korman@lotus.com>
Subject: Re: win32 stat in directory with 4682 files
Message-Id: <3BAB65F1.D33AB683@lotus.com>


--------------CE28798BDE3245C4C94E4B21
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This was worth at try, but did not help.
------------------------------------------

use cwd;
@when=localtime();
print $when[2], ":" , $when[1],  ":" , $when[0];
chdir "G:/ENGLISH/WIN2000/ADV_SERV/I386";
opendir D, ".";
while (defined(my $i = readdir D))
{
  stat($i);
  $cnt++;
  if (0 == $cnt %200)
  {
     print ".";
     $blk++;
     if (0 == $blk % 80)
     {
       print "\n";
     }
  }

}
@when=localtime();
print $when[2], ":" , $when[1],  ":" , $when[0];

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


12:0:39.......................12:1:59

Peter Korman wrote:

> How would one speed this up?
> ---------------------------------------------------------
> use cwd;
> @when=localtime();
> print $when[2], ":" , $when[1],  ":" , $when[0];
> chdir "G:/ENGLISH/WIN2000/ADV_SERV/I386";
> opendir D, ".";
> @direntries=readdir(D);
> foreach $i (@direntries)
> {
>   @s=stat($i);
>   $cnt++;
>   if (0 == $cnt %200)
>   {
>      print ".";
>      $blk++;
>      if (0 == $blk % 80)
>      {
>        print "\n";
>      }
>   }
> }
> @when=localtime();
> print $when[2], ":" , $when[1],  ":" , $when[0];
> ---------------------------------------------------------
>
> Running NT 4.0 on a 350MHZ pentium II system under perl:
>
> This is perl, v5.6.1 built for MSWin32-x86-multi-thread
> Binary build 629 provided by ActiveState Tool Corp.
> http://www.ActiveState.com
> Built 12:27:04 Aug 20 2001
>
> This produces the output:
>
> 10:15:56.......................10:17:17
>
>
> Thanks.
>

--------------CE28798BDE3245C4C94E4B21
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
This was worth at try, but did not help.
<br>------------------------------------------
<p><tt>use cwd;</tt>
<br><tt>@when=localtime();</tt>
<br><tt>print $when[2], ":" , $when[1],&nbsp; ":" , $when[0];</tt>
<br><tt>chdir "G:/ENGLISH/WIN2000/ADV_SERV/I386";</tt>
<br><tt>opendir D, ".";</tt>
<br><tt><font color="#FF0000">while (defined(my $i = readdir D))</font></tt>
<br><tt>{</tt>
<br><tt>&nbsp; stat($i);</tt>
<br><tt>&nbsp; $cnt++;</tt>
<br><tt>&nbsp; if (0 == $cnt %200)</tt>
<br><tt>&nbsp; {</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp; print ".";</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp; $blk++;</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp; if (0 == $blk % 80)</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp; {</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "\n";</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp; }</tt>
<br><tt>&nbsp; }</tt><tt></tt>
<p><tt>}</tt>
<br><tt>@when=localtime();</tt>
<br><tt>print $when[2], ":" , $when[1],&nbsp; ":" , $when[0];</tt><tt></tt>
<p><tt>------------------------------------------</tt>
<br><tt></tt>&nbsp;<tt></tt>
<p><tt>12:0:39.......................12:1:59</tt>
<p>Peter Korman wrote:
<blockquote TYPE=CITE>How would one speed this up?
<br>---------------------------------------------------------
<br><font face="Courier New,Courier"><font size=-2>use cwd;</font></font>
<br><font face="Courier New,Courier"><font size=-2>@when=localtime();</font></font>
<br><font face="Courier New,Courier"><font size=-2>print $when[2], ":"
, $when[1],&nbsp; ":" , $when[0];</font></font>
<br><font face="Courier New,Courier"><font size=-2>chdir "G:/ENGLISH/WIN2000/ADV_SERV/I386";</font></font>
<br><font face="Courier New,Courier"><font size=-2>opendir D, ".";</font></font>
<br><font face="Courier New,Courier"><font size=-2>@direntries=readdir(D);</font></font>
<br><font face="Courier New,Courier"><font size=-2>foreach $i (@direntries)</font></font>
<br><font face="Courier New,Courier"><font size=-2>{</font></font>
<br><font face="Courier New,Courier"><font size=-2>&nbsp; @s=stat($i);</font></font>
<br><font face="Courier New,Courier"><font size=-2>&nbsp; $cnt++;</font></font>
<br><font face="Courier New,Courier"><font size=-2>&nbsp; if (0 == $cnt
%200)</font></font>
<br><font face="Courier New,Courier"><font size=-2>&nbsp; {</font></font>
<br><font face="Courier New,Courier"><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;
print ".";</font></font>
<br><font face="Courier New,Courier"><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;
$blk++;</font></font>
<br><font face="Courier New,Courier"><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;
if (0 == $blk % 80)</font></font>
<br><font face="Courier New,Courier"><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;
{</font></font>
<br><font face="Courier New,Courier"><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
print "\n";</font></font>
<br><font face="Courier New,Courier"><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;
}</font></font>
<br><font face="Courier New,Courier"><font size=-2>&nbsp; }</font></font>
<br><font face="Courier New,Courier"><font size=-2>}</font></font>
<br><font face="Courier New,Courier"><font size=-2>@when=localtime();</font></font>
<br><font face="Courier New,Courier"><font size=-2>print $when[2], ":"
, $when[1],&nbsp; ":" , $when[0];</font></font>
<br>---------------------------------------------------------
<p>Running NT 4.0 on a 350MHZ pentium II system under perl:
<p><tt>This is perl, v5.6.1 built for MSWin32-x86-multi-thread</tt>
<br><tt>Binary build 629 provided by ActiveState Tool Corp. <a href="http://www.ActiveState.com">http://www.ActiveState.com</a></tt>
<br><tt>Built 12:27:04 Aug 20 2001</tt>
<p>This produces the output:
<p><font face="Courier New,Courier"><font size=-2>10:15:56.......................10:17:17</font></font>
<br>&nbsp;
<p>Thanks.
<br>&nbsp;</blockquote>
</html>

--------------CE28798BDE3245C4C94E4B21--



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

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


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