[9550] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3144 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jul 13 17:07:26 1998

Date: Mon, 13 Jul 98 14:01:35 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 13 Jul 1998     Volume: 8 Number: 3144

Today's topics:
    Re: Quickie: the number of occurences of a character in (David A. Black)
    Re: Quickie: the number of occurences of a character in (Andre L.)
    Re: Quickie: the number of occurences of a character in (Larry Rosler)
    Re: Quickie: the number of occurences of a character in (Sean McAfee)
    Re: Quickie: the number of occurences of a character in <dgris@rand.dimensional.com>
    Re: Quickie: the number of occurences of a character in (David A. Black)
    Re: Quickie: the number of occurences of a character in <dfan@harmonixmusic.com>
        Recommend me Perl! c960901@student.dtu.dk
    Re: References to Anonymous subroutines (Greg Ward)
    Re: RegExps: Check if string consists of EXACTLY 3 digi (Michael J Gebis)
    Re: RegExps: Check if string consists of EXACTLY 3 digi (brian d foy)
    Re: RegExps: Check if string consists of EXACTLY 3 digi (Larry Rosler)
    Re: RegExps: Check if string consists of EXACTLY 3 digi (Bob Trieger)
    Re: RegExps: Check if string consists of EXACTLY 3 digi (Tad McClellan)
        Sorting a 2-d array <gavery@uu.net>
    Re: Sorting a 2-d array (brian d foy)
    Re: Threads in Perl? NEWBIE ? (Josh Kortbein)
        Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)

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

Date: Mon, 13 Jul 1998 15:16:57 EDT
From: dblack@saturn.superlink.net (David A. Black)
Subject: Re: Quickie: the number of occurences of a character in a string
Message-Id: <6odmf9$26f$1@earth.superlink.net>

Hello -

shapiroj@logica.com writes:

>I want to find the number of occurences of a given character ($char) in a
>particular string, ($text). I thought there would be some cool, quick method
>to do this using m//, but all I've been able to come up with is:

>$number_of_occur = scalar ( @temp = ($text =~ /$char/g));

>which is nice because it's one-line, but the @temp array is ick. (And -w
>doesn't like it either.) Any suggestions?

You could do:

$num = $text =~ s/$char/$char/g;

or

$num = $text =~ s/($char)/$1/g;  # which would be more /i friendly, if
                                 # that mattered




David Black
dblack@saturn.superlink.net


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

Date: Mon, 13 Jul 1998 15:29:01 -0500
From: alecler@cam.org (Andre L.)
Subject: Re: Quickie: the number of occurences of a character in a string
Message-Id: <alecler-1307981529010001@dialup-740.hip.cam.org>

In article <6odh9t$s2g$1@nnrp1.dejanews.com>, shapiroj@logica.com wrote:

> I want to find the number of occurences of a given character ($char) in a
> particular string, ($text). I thought there would be some cool, quick method
> to do this using m//, but all I've been able to come up with is:
> 
> $number_of_occur = scalar ( @temp = ($text =~ /$char/g));
> 
> which is nice because it's one-line, but the @temp array is ick. (And -w
> doesn't like it either.) Any suggestions?


$num = () = $text =~ /$char/g;

HTH,
A.L.


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

Date: Mon, 13 Jul 1998 12:49:55 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Quickie: the number of occurences of a character in a string
Message-Id: <MPG.101404bca3e7f4a1989706@nntp.hpl.hp.com>

[This followup was posted to comp.lang.perl.misc and a copy was sent to 
the cited author.]

In article <6odmf9$26f$1@earth.superlink.net> on Mon, 13 Jul 1998 
15:16:57 EDT, David A. Black <dblack@saturn.superlink.net> says...
 ...
> You could do:
> 
> $num = $text =~ s/$char/$char/g;
> 
> or
> 
> $num = $text =~ s/($char)/$1/g;  # which would be more /i friendly, if
>                                  # that mattered

What makes you think that?  These two statements have identical 
functions.

-- 
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Mon, 13 Jul 1998 19:56:47 GMT
From: mcafee@breakout.rs.itd.umich.edu (Sean McAfee)
Subject: Re: Quickie: the number of occurences of a character in a string
Message-Id: <3Etq1.3598$24.20589724@news.itd.umich.edu>

In article <6odh9t$s2g$1@nnrp1.dejanews.com>,  <shapiroj@logica.com> wrote:
>I want to find the number of occurences of a given character ($char) in a
>particular string, ($text). I thought there would be some cool, quick method
>to do this using m//, but all I've been able to come up with is:

>$number_of_occur = scalar ( @temp = ($text =~ /$char/g));

>which is nice because it's one-line, but the @temp array is ick. (And -w
>doesn't like it either.) Any suggestions?

$n = $i = 0;  $n++ while ($i = index($text, $char, $i) + 1) > 0;

I benchmarked these two methods (as well as a third which was mentioned by
someone else), counting the number of e's in /usr/dict/words:

----------------------------------------------------------------------
use Benchmark;

open(DICT, "/usr/dict/words") ? ($a = join("", <DICT>)) : die;
close(DICT);

timethese(500, {
	RegExp => sub { my ($n,@temp); $n = scalar (@temp = $a =~ /e/g); },
	RegExp2 => sub { my $n = () = $a =~ /e/g; },
	Index  => sub { my ($i,$n)=(0,0); $n++ while ($i=index($a,"e",$i)+1) > 0 }
});
----------------------------------------------------------------------

Benchmark: timing 500 iterations of Index, RegExp, RegExp2...
     Index: 111 secs (85.10 usr  0.00 sys = 85.10 cpu)
    RegExp: 140 secs (119.48 usr  0.05 sys = 119.53 cpu)
   RegExp2: 146 secs (83.29 usr  0.07 sys = 83.36 cpu)

-- 
Sean McAfee | GS d->-- s+++: a26 C++ US+++$ P+++ L++ E- W+ N++ |
            | K w--- O? M V-- PS+ PE Y+ PGP?>++ t+() 5++ X+ R+ | mcafee@
            | tv+ b++ DI++ D+ G e++>++++ h- r y+>++**          | umich.edu




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

Date: Mon, 13 Jul 1998 20:35:28 GMT
From: Daniel Grisinger <dgris@rand.dimensional.com>
Subject: Re: Quickie: the number of occurences of a character in a string
Message-Id: <6odqck$k44$1@rand.dimensional.com>

[posted to comp.lang.perl.misc and mailed to the cited author]

In article <MPG.101404bca3e7f4a1989706@nntp.hpl.hp.com>
lr@hpl.hp.com (Larry Rosler) wrote:

>In article <6odmf9$26f$1@earth.superlink.net> on Mon, 13 Jul 1998 
>15:16:57 EDT, David A. Black <dblack@saturn.superlink.net> says...

>> $num = $text =~ s/$char/$char/g;
>> $num = $text =~ s/($char)/$1/g;  # which would be more /i friendly, if
>>                                  # that mattered

>What makes you think that?  These two statements have identical 
>functions.

Yes, but if you add /i then the second version will preserve case.

$ perl -w
$a = $b = 'this is a test';
$char = 'T';

$a =~ s/$char/$char/ig;
$b =~ s/($char)/$1/ig;

print $a, "\n";
print $b, "\n";
__END__

This is a TesT
this is a test
$

Regards,
Daniel
-- 
Daniel Grisinger           dgris@perrin.dimensional.com
"No kings, no presidents, just a rough consensus and
running code."
                           Dave Clark


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

Date: Mon, 13 Jul 1998 16:23:38 EDT
From: dblack@saturn.superlink.net (David A. Black)
Subject: Re: Quickie: the number of occurences of a character in a string
Message-Id: <6odqca$a6h$1@earth.superlink.net>

Hello -

lr@hpl.hp.com (Larry Rosler) writes:

>[This followup was posted to comp.lang.perl.misc and a copy was sent to 
>the cited author.]

>In article <6odmf9$26f$1@earth.superlink.net> on Mon, 13 Jul 1998 
>15:16:57 EDT, David A. Black <dblack@saturn.superlink.net> says...
>...
>> You could do:
>> 
>> $num = $text =~ s/$char/$char/g;
>> 
>> or
>> 
>> $num = $text =~ s/($char)/$1/g;  # which would be more /i friendly, if
>>                                  # that mattered

>What makes you think that?  These two statements have identical 
>functions.

If one were to add the /i modifier to each of these,
the first would flatten case to the case of $char, whereas the
second would not; hence I described the second as more /i
friendly.  

But see Andre's post anyway for a better solution not involving
substitution.


David Black
dblack@saturn.superlink.net


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

Date: 13 Jul 1998 16:39:50 -0400
From: Dan Schmidt <dfan@harmonixmusic.com>
Subject: Re: Quickie: the number of occurences of a character in a string
Message-Id: <wkoguth4dl.fsf@turangalila.harmonixmusic.com>

mcafee@breakout.rs.itd.umich.edu (Sean McAfee) writes:

| ----------------------------------------------------------------------
| use Benchmark;
| 
| open(DICT, "/usr/dict/words") ? ($a = join("", <DICT>)) : die;
| close(DICT);
| 
| timethese(500, {
| 	RegExp => sub { my ($n,@temp); $n = scalar (@temp = $a =~ /e/g); },
| 	RegExp2 => sub { my $n = () = $a =~ /e/g; },
| 	Index  => sub { my ($i,$n)=(0,0); $n++ while ($i=index($a,"e",$i)+1) > 0 }
| });
| ----------------------------------------------------------------------
| 
| Benchmark: timing 500 iterations of Index, RegExp, RegExp2...
|      Index: 111 secs (85.10 usr  0.00 sys = 85.10 cpu)
|     RegExp: 140 secs (119.48 usr  0.05 sys = 119.53 cpu)
|    RegExp2: 146 secs (83.29 usr  0.07 sys = 83.36 cpu)
| 
| -- 

You can do better than that:

timethese(500, {
	RegExp => sub { my ($n,@temp); $n = scalar (@temp = $a =~ /e/g); },
	RegExp2 => sub { my $n = () = $a =~ /e/g; },
	Index  => sub { my ($i,$n)=(0,0); $n++ while ($i=index($a,"e",$i)+1) > 0 },
	Tr => sub { my $n = $a =~ tr/e/e/; }
});

--- results follow ---

Benchmark: timing 500 iterations of Index, RegExp, RegExp2, Tr...
     Index: 15 secs (14.67 usr  0.00 sys = 14.67 cpu)
    RegExp: 27 secs (27.33 usr  0.00 sys = 27.33 cpu)
   RegExp2: 21 secs (21.61 usr  0.00 sys = 21.61 cpu)
        Tr:  2 secs ( 1.73 usr  0.00 sys =  1.73 cpu)

But that wasn't the original question anyway.  In the original
question, the letter to search for wasn't determined until run-time.

-- 
                 Dan Schmidt -> dfan@harmonixmusic.com, dfan@alum.mit.edu
Honest Bob & the                http://www2.thecia.net/users/dfan/
Factory-to-Dealer Incentives -> http://www2.thecia.net/users/dfan/hbob/
          Gamelan Galak Tika -> http://web.mit.edu/galak-tika/www/


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

Date: Mon, 13 Jul 1998 19:39:02 GMT
From: c960901@student.dtu.dk
Subject: Recommend me Perl!
Message-Id: <6odnon$cul$1@nnrp1.dejanews.com>

Hello there.

I've never written a line of Perl, but I'm going to in the very near future.
I need someone to recommend me -the best- Perl book available. What can Perl
do? I've seen only little Perl code. Can it scan a directory for subdirs and
files and return the names in a string array? Cause that's what I really
need.

Thanks

Morten

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


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

Date: 13 Jul 1998 18:54:52 GMT
From: greg@bic.mni.mcgill.ca (Greg Ward)
Subject: Re: References to Anonymous subroutines
Message-Id: <6odl5s$ob2@sifon.cc.mcgill.ca>

:                 $rs = sub {
:                                     print "hello \n";
:                                 };
: 
: It's suppose to return a reference to the newly created sub. Can anyone tell
: me how it works?? Let say you declare such a routine can it be referenced to
: somewhere else in the program? And what would be the use in declaring an
: anonymous sub (Real world application).

I can't tell you in detail how this works, except that it does work
nicely.  Some real-world applications:

  * callbacks in GUI programs
  * sub x needs to pass a bit of code to sub y, and wants to
    do so without interfering with the symbol table (named subroutines
    are always global in Perl)
  * closures (anonymous subroutine that remembers the environment in
    which it was created on subsequent invocations -- a bit spooky, but
    very cool, and very useful for GUI programming)

Keep reading that fine book *Advanced Perl Programming* for more
information.

        Greg
--
Greg Ward - Programmer/Analyst                     greg@bic.mni.mcgill.ca
Brain Imaging Centre (WB201)           http://www.bic.mni.mcgill.ca/~greg
Montreal Neurological Institute           voice: (514) 398-4965 (or 1996)
Montreal, Quebec, Canada  H3A 2B4           fax: (514) 398-8948


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

Date: 13 Jul 1998 19:52:30 GMT
From: gebis@albrecht.ecn.purdue.edu (Michael J Gebis)
Subject: Re: RegExps: Check if string consists of EXACTLY 3 digits ??
Message-Id: <6odohu$d1u@mozo.cc.purdue.edu>

Karsten <karsten.no.123.spam@alpha.futurenet.co.za> writes:

}I am writing a CGI script which accepts input via QUERY_STRING. 
}However, the input is only valid if it consists of EXACTLY 3 DIGITS!  No
}more , no less, no other characters in front, after or in between the
}digits.  Ive tried things such as:

#Untested but if I screw this up I deserve what I get.
if($string =~ m/^\d\d\d$/) { print "Valid\n"; }

-- 
Mike Gebis  gebis@ecn.purdue.edu  mgebis@eternal.net


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

Date: Mon, 13 Jul 1998 16:07:48 -0400
From: comdog@computerdog.com (brian d foy)
Subject: Re: RegExps: Check if string consists of EXACTLY 3 digits ??
Message-Id: <comdog-ya02408000R1307981607480001@news.panix.com>
Keywords: from just another new york perl hacker

In article <35AACA77.D9C@alpha.futurenet.co.za>, karsten.no.123.spam@alpha.futurenet.co.za posted:

>I am writing a CGI script which accepts input via QUERY_STRING. 
>However, the input is only valid if it consists of EXACTLY 3 DIGITS!  No
>more , no less, no other characters in front, after or in between the
>digits.  Ive tried things such as:
>
>if ($ENV{'QUERY_STRING'} =~ /[0-9]\{3\}/)

why are you escaping the curlies? :)

try

   $string =~ m/^\d{3}(?!\n)$/

-- 
brian d foy                                  <comdog@computerdog.com>
CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>
Comprehensive Perl Archive Network (CPAN) <URL:http://www.perl.com>
Perl Mongers Travel Deals! <URL:http://www.pm.org/travel.html>


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

Date: Mon, 13 Jul 1998 13:00:27 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: RegExps: Check if string consists of EXACTLY 3 digits ??
Message-Id: <MPG.10140736fd810aed989707@nntp.hpl.hp.com>

[This followup was posted to comp.lang.perl.misc and a copy was sent to 
the cited author.]

In article <35AACA77.D9C@alpha.futurenet.co.za> on Mon, 13 Jul 1998 
20:03:19 -0700, Karsten <karsten.no.123.spam@alpha.futurenet.co.za> 
says...
> Hi!
> 
> I am writing a CGI script which accepts input via QUERY_STRING. 
> However, the input is only valid if it consists of EXACTLY 3 DIGITS!  No
> more , no less, no other characters in front, after or in between the
> digits.  Ive tried things such as:
> 
> if ($ENV{'QUERY_STRING'} =~ /[0-9]\{3\}/)

  if ($ENV{'QUERY_STRING'} =~ /^[0-9]{3}$/) 

or (identically, but fewer keystrokes):

  if ($ENV{QUERY_STRING} =~ /^\d{3}$/) 

The start-of-string and end-of-string anchors ^ and $ are required by 
your problem specification.

-- 
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Mon, 13 Jul 1998 20:17:03 GMT
From: sowmaster@juicepigs.com (Bob Trieger)
Subject: Re: RegExps: Check if string consists of EXACTLY 3 digits ??
Message-Id: <6odq49$bp$1@strato.ultra.net>

karsten.no.123.spam@alpha.futurenet.co.za wrote:
-> Hi!
-> 
-> I am writing a CGI script which accepts input via QUERY_STRING. 
-> However, the input is only valid if it consists of EXACTLY 3 DIGITS!  No
-> more , no less, no other characters in front, after or in between the
-> digits.  Ive tried things such as:


/^\d{3}$/

or 

/^\d\d\d$/


HTH

Bob Trieger
sowmaster@juicepigs.com
" Cost a spammer some cash: Call 1-800-400-1972 
  Ext: 1949 and let the jerk that answers know 
  that his toll free number was sent as spam. "


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

Date: Mon, 13 Jul 1998 14:15:41 -0500
From: tadmc@flash.net (Tad McClellan)
Subject: Re: RegExps: Check if string consists of EXACTLY 3 digits ??
Message-Id: <tcmdo6.ue9.ln@localhost>

Karsten (karsten.no.123.spam@alpha.futurenet.co.za) wrote:

: I am writing a CGI script which accepts input via QUERY_STRING. 
: However, the input is only valid if it consists of EXACTLY 3 DIGITS!  No
: more , no less, no other characters in front, after or in between the
: digits.  Ive tried things such as:

: if ($ENV{'QUERY_STRING'} =~ /[0-9]\{3\}/)
                                    ^  ^
                                    ^  ^ you don't need those


   if ($ENV{'QUERY_STRING'} =~ /^\d{3}$/)
                                ^^^   ^


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

Date: Mon, 13 Jul 1998 15:56:51 -0400
From: Gordon Avery <gavery@uu.net>
Subject: Sorting a 2-d array
Message-Id: <35AA6683.C06@uu.net>

Hi,
 I'm trying to sort a 2-d array by the first element of each array and I
have had no luck.  The best way I can come up with is:

sort { $array[$a][0] <=> $array[$b][0] } @array;

Thank you for any help.

Gordon Avery
gavery@uu.net


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

Date: Mon, 13 Jul 1998 16:22:07 -0400
From: comdog@computerdog.com (brian d foy)
Subject: Re: Sorting a 2-d array
Message-Id: <comdog-ya02408000R1307981622070001@news.panix.com>
Keywords: from just another new york perl hacker

In article <35AA6683.C06@uu.net>, Gordon Avery <gavery@uu.net> posted:

>Hi,
> I'm trying to sort a 2-d array by the first element of each array and I
>have had no luck.  The best way I can come up with is:
>
>sort { $array[$a][0] <=> $array[$b][0] } @array;

remember what $a and $b are!  they are the elements of the list
that you are sorting, not the indices of an array.  perhaps you
meant

   sort { $$a[0] <=> $$b[0] } @array;

or

   sort { $a->[0] <=> $b->[0] } @array;

good luck :)

-- 
brian d foy                                  <comdog@computerdog.com>
CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>
Comprehensive Perl Archive Network (CPAN) <URL:http://www.perl.com>
Perl Mongers Travel Deals! <URL:http://www.pm.org/travel.html>


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

Date: 13 Jul 1998 20:38:56 GMT
From: kortbein@iastate.edu (Josh Kortbein)
Subject: Re: Threads in Perl? NEWBIE ?
Message-Id: <6odr90$9al$1@news.iastate.edu>

Su Potter (spotter@remove-to-reply.ms.com) wrote:
: I'd like to know how how to create a thread in Perl and how it would be run.
: Please reply directly to spotter@ms.com aswell as the newsgroup, thanks.

In the current version of perl, you don't.
Surely Tom C. will say that you can do whatever you want with fork()
anyway.


Josh

--

__________________________________________
She had heard all about excluded middles;
they were bad shit, to be avoided.
            - Thomas Pynchon



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

Date: 12 Jul 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Mar 98)
Message-Id: <null>


Administrivia:

Special notice: in a few days, the new group comp.lang.perl.moderated
should be formed. I would rather not support two different groups, and I
know of no other plans to create a digested moderated group. This leaves
me with two options: 1) keep on with this group 2) change to the
moderated one.

If you have opinions on this, send them to
perl-users-request@ruby.oce.orst.edu. 


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.misc (and this Digest), send your
article to perl-users@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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 3144
**************************************

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