[23390] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5609 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 3 14:08:06 2003

Date: Fri, 3 Oct 2003 11:05:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 3 Oct 2003     Volume: 10 Number: 5609

Today's topics:
    Re: DBD::Pg and bind_param <cschadl@satan.blah.org.uk>
    Re: DBD::Pg and bind_param <glex_nospam@qwest.net>
    Re: DBD::Pg and bind_param <cschadl@satan.blah.org.uk>
    Re: DBD::Pg and bind_param <glex_nospam@qwest.net>
    Re: DBD::Pg and bind_param <cschadl@satan.blah.org.uk>
    Re: DBI <someone@somewhere.com>
    Re: DBI <REMOVEsdnCAPS@comcast.net>
        fastest count of instances in string? <bill_knight2@yahoo.com>
    Re: fastest count of instances in string? <ak+usenet@freeshell.org>
    Re: fastest count of instances in string? <daniel.rawson.take!this!out!@asml.nl>
    Re: fastest count of instances in string? <skweek@no.spam>
    Re: fastest count of instances in string? <jurgenex@hotmail.com>
    Re: fastest count of instances in string? <skweek@no.spam>
    Re: fastest count of instances in string? (Tad McClellan)
    Re: fastest count of instances in string? <uri@stemsystems.com>
    Re: fastest count of instances in string? <skweek@no.spam>
    Re: fastest count of instances in string? <REMOVEsdnCAPS@comcast.net>
    Re: fastest count of instances in string? <uri@stemsystems.com>
    Re: fastest count of instances in string? <skweek@no.spam>
    Re: fastest count of instances in string? <uri@stemsystems.com>
    Re: fastest count of instances in string? (Roy Johnson)
    Re: How to generate this list? (Roy Johnson)
    Re: How to generate this list? (Roy Johnson)
        Interpolation problem (Pal Hulden)
    Re: Interpolation problem <kha@rogers.com>
    Re: Interpolation problem <noreply@gunnar.cc>
    Re: javascript in HTML ... not running on some browsers <hyweljenkins@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 03 Oct 2003 10:51:27 -0500
From: "Chris Schadl" <cschadl@satan.blah.org.uk>
Subject: Re: DBD::Pg and bind_param
Message-Id: <pan.2003.10.03.15.51.25.200836@satan.blah.org.uk>

On Fri, 03 Oct 2003 13:20:56 +0200, peter pilsl wrote:

> Chris Schadl wrote:
> 
>> 
>> chris@eclair:~/src/perl/sql$ ./mp3-insert.pl /export/mp3/mp3-X/Pixies\ -\
>> Trompe\ Le\ Monde/ ...
>> DBD::Pg::st execute failed: ERROR:  Function 'insert_mp3(unknown, int4,
>> int4, int4, int4, unknown, unknown, int4, unknown, unknown, unknown,
>> unknown, unknown)' does not exist at ./mp3-insert.pl line 129.
>> ...
> 
> Its really impossible to me to read all your script, cause its ways too 
> long. But to me it seems as if you didnt define the function insert_mp3 
> proper.  (as the error says : "function ... does not exist"). So it looks 
> merely as a sql-problem than a perl- or DBD-problem.
>   As you say : if the error is the same if using bind or if not using bind, 
> it looks like the error has not to do with bind :)
>
 
For the record, insert_mp3() is a PL/pgSQL procedure that I have defined,
and it is (to my knowledge), functioning correctly:

mp3s=# SELECT insert_mp3('Blah Test', 130, 192, false, true, 
	'The Testers - Blah Test.mp3', 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', 
	4, 'The Test', 'Test Rock', '1999', 'Test Records', 'The Testers');
     insert_mp3      
---------------------
 New record inserted
(1 row)


> If you want to test the bind_param-thing just create a small and simple 
> test-scenario that you can post here.

All of my test scenarios seem to work without a hitch, actually, which is
why I was hoping that someone might be able to point out something that
I'm doing wrong DBI-wise in the script I posted.  Aside from the number of
calls to bind_param, it really isn't much longer or more complicated than
some of the test scenarios which I've written.

> best,
> peter

Chris Schadl


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

Date: Fri, 03 Oct 2003 11:43:49 -0500
From: "J. Gleixner" <glex_nospam@qwest.net>
Subject: Re: DBD::Pg and bind_param
Message-Id: <DEhfb.204$te5.34574@news.uswest.net>


> DBD::Pg::st execute failed: ERROR:  Function 'insert_mp3(unknown, int4, int4, int4, int4, unknown, unknown, int4, unknown, unknown, unknown, unknown, unknown)' does not exist at ./mp3-insert.pl line 129.
> DBD::Pg::st execute failed: ERROR:  Function 'insert_mp3(unknown, int4, int4, int4, int4, unknown, unknown, int4, unknown, unknown, unknown, unknown, unknown)' does not exist at ./mp3-insert.pl line 129.
> 
> This is exactly the same error that I would recieve if I had simply called
> $sth->execute() with the given variables as arguments rather than passing
> them implicitly with bind_param.  Does anyone know what I'm doing wrong?

Taking a peek at perldoc DBD::Pg:

	"Stored Procedures

	DBD::Pg does not support stored procedures."



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

Date: Fri, 03 Oct 2003 12:05:03 -0500
From: "Chris Schadl" <cschadl@satan.blah.org.uk>
Subject: Re: DBD::Pg and bind_param
Message-Id: <pan.2003.10.03.17.04.57.68181@satan.blah.org.uk>

On Fri, 03 Oct 2003 11:43:49 -0500, J. Gleixner wrote:

> 
>> This is exactly the same error that I would recieve if I had simply called
>> $sth->execute() with the given variables as arguments rather than passing
>> them implicitly with bind_param.  Does anyone know what I'm doing wrong?
> 
> Taking a peek at perldoc DBD::Pg:
> 
> 	"Stored Procedures
> 
> 	DBD::Pg does not support stored procedures."

Hmm, the perldoc page for my version of DBD::Pg (1.22) dosen't say anything about
stored procedures not being supported, and I've been able to execute
simple stored procedures (i.e. concat_text) just fine.  What version of
DBD::Pg do you have that says this?


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

Date: Fri, 03 Oct 2003 12:31:02 -0500
From: "J. Gleixner" <glex_nospam@qwest.net>
Subject: Re: DBD::Pg and bind_param
Message-Id: <Tkifb.216$te5.39658@news.uswest.net>

Chris Schadl wrote:
> On Fri, 03 Oct 2003 11:43:49 -0500, J. Gleixner wrote:
> 
> 
>>>This is exactly the same error that I would recieve if I had simply called
>>>$sth->execute() with the given variables as arguments rather than passing
>>>them implicitly with bind_param.  Does anyone know what I'm doing wrong?
>>
>>Taking a peek at perldoc DBD::Pg:
>>
>>	"Stored Procedures
>>
>>	DBD::Pg does not support stored procedures."
> 
> 
> Hmm, the perldoc page for my version of DBD::Pg (1.22) dosen't say anything about
> stored procedures not being supported, and I've been able to execute
> simple stored procedures (i.e. concat_text) just fine.  What version of
> DBD::Pg do you have that says this?

I don't use Postgresql, just looked at the docs to see if it said 
anything about stored procedures.

http://search.cpan.org/~dwheeler/DBD-Pg-1.22/dbd-pg.pod#Stored_Procedures



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

Date: Fri, 03 Oct 2003 12:59:53 -0500
From: "Chris Schadl" <cschadl@satan.blah.org.uk>
Subject: Re: DBD::Pg and bind_param
Message-Id: <pan.2003.10.03.17.59.52.347177@satan.blah.org.uk>

On Fri, 03 Oct 2003 12:31:02 -0500, J. Gleixner wrote:

> 
> I don't use Postgresql, just looked at the docs to see if it said 
> anything about stored procedures.
> 
> http://search.cpan.org/~dwheeler/DBD-Pg-1.22/dbd-pg.pod#Stored_Procedures

Well I'll be damned.  I guess that's what my problem is...


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

Date: Fri, 3 Oct 2003 15:45:16 +0100
From: "Bigus" <someone@somewhere.com>
Subject: Re: DBI
Message-Id: <blk221$oda@newton.cc.rl.ac.uk>

"Michael Budash" <mbudash@sonic.net> wrote in message
news:mbudash-5D612B.11300402102003@typhoon.sonic.net...
> In article <pan.2003.10.02.12.40.42.160229@aursand.no>,
>  Tore Aursand <tore@aursand.no> wrote:
>
> > Problems occur when the data you insert into the SQL query contain
single
> > quotes (or other non-escaped special characters).
> >
> > The easiest way to deal with problems like this is to _always_ bind
> > variables into your SQL query.  I recommend this way of doing it even
when
> > you are 110% sure that the data you're about to insert into the SQL is
> > "clean";
>
> very good advice. also it prevents having to think about which method
> you'll use - just _always_ use placeholders.

Thanks, I shall heed that - although, for one-off DB commands, I think using
the $db->quote() method (when used with say a $db->do()) may require less
lines of code and save the need to open & "finish" other handles like
$sth->.

Regards

Bigus




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

Date: Fri, 03 Oct 2003 11:27:33 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: DBI
Message-Id: <Xns94097E93F9E9Bsdn.comcast@206.127.4.25>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

"Bigus" <someone@somewhere.com> wrote in
news:blk221$oda@newton.cc.rl.ac.uk: 

> Thanks, I shall heed that - although, for one-off DB commands, I think
> using the $db->quote() method (when used with say a $db->do()) may
> require less lines of code and save the need to open & "finish" other
> handles like $sth->.

The $dbh->do() method accepts bind values.

- -- 
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP32jOWPeouIeTNHoEQIBeACfZgo/sdjSpSSyUsgHyF/3WY7qA5YAoKBA
Ol2BpJ9LZUNsZ3g7bNQITMtL
=Q31I
-----END PGP SIGNATURE-----


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

Date: Fri, 3 Oct 2003 14:30:28 +0000 (UTC)
From: bill <bill_knight2@yahoo.com>
Subject: fastest count of instances in string?
Message-Id: <blk164$cd5$1@reader2.panix.com>



What's the fastest way to count how many times a specific character
appears in a string?  Anything faster than something like

  for (split //, $string) { ++$count if $_ eq $char }

?

TIA,

	-bill



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

Date: Fri, 3 Oct 2003 14:39:51 +0000 (UTC)
From: Andreas Kahari <ak+usenet@freeshell.org>
Subject: Re: fastest count of instances in string?
Message-Id: <slrnbnr2hm.57e.ak+usenet@norge.freeshell.org>

In article <blk164$cd5$1@reader2.panix.com>, bill wrote:
> 
> 
> What's the fastest way to count how many times a specific character
> appears in a string?  Anything faster than something like
> 
>   for (split //, $string) { ++$count if $_ eq $char }

Regexps are generally slow, but I didn't benchmark your solution
against this:

my $string = 'gtcgtcgtacgtgtgtagtattatgcgcgcgc';
my $ch = 'c';

my $pos = 0;
my $count = 0;

while ($pos = 1 + index($string, $ch, $pos)) {
        ++$count;
}

print "$count\n";

-- 
Andreas Kähäri


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

Date: Fri, 03 Oct 2003 10:41:38 -0400
From: Dan Rawson <daniel.rawson.take!this!out!@asml.nl>
Subject: Re: fastest count of instances in string?
Message-Id: <blk1r2$d7upq$1@ID-122008.news.uni-berlin.de>

bill wrote:
> What's the fastest way to count how many times a specific character
> appears in a string?  Anything faster than something like
> 
>   for (split //, $string) { ++$count if $_ eq $char }
> 
> ?
> 
> TIA,
> 
> 	-bill
> 
 From the camel:

$cnt = tr/?/?/;




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

Date: Fri, 03 Oct 2003 16:48:26 +0200
From: AlV <skweek@no.spam>
Subject: Re: fastest count of instances in string?
Message-Id: <blk27q$1nt$1@news-reader2.wanadoo.fr>

bill wrote:
> What's the fastest way to count how many times a specific character
> appears in a string?  Anything faster than something like
> 
>   for (split //, $string) { ++$count if $_ eq $char }

You could try (beware, result is undef when there is no schar in $_):

# perl -e '$char="a"; $_="cacababaA"; $n=s/$char/$char/g; print "$n\n";'
4

Finding if this is faster than your method is left as an exercise ;o)



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

Date: Fri, 03 Oct 2003 14:55:22 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: fastest count of instances in string?
Message-Id: <u5gfb.39883$Wd7.24499@nwrddc03.gnilink.net>

bill wrote:
> What's the fastest way to count how many times a specific character
> appears in a string?  Anything faster than something like
>
>   for (split //, $string) { ++$count if $_ eq $char }

That code should be quite slow: running the RE engine, implicitely looping
over the string and creating a temporary array, explicitely looping over
that array, comparing each element against $char, ...
Really, no, it should be very slow.

My guess would be using tr() will be the fastest you can get.

jue




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

Date: Fri, 03 Oct 2003 17:00:53 +0200
From: AlV <skweek@no.spam>
Subject: Re: fastest count of instances in string?
Message-Id: <blk2v6$ub8$1@news-reader5.wanadoo.fr>

AlV wrote:
> bill wrote:
> 
>> What's the fastest way to count how many times a specific character
>> appears in a string?  Anything faster than something like
>>
>>   for (split //, $string) { ++$count if $_ eq $char }
> 
> 
> You could try (beware, result is undef when there is no schar in $_):
> 
> # perl -e '$char="a"; $_="cacababaA"; $n=s/$char/$char/g; print "$n\n";'
> 4
> 
> Finding if this is faster than your method is left as an exercise ;o)
> 

Oops,Dan Rawson is right tr// is, for obvious reasons, faster than s//g.

Here are proofs:

# time perl -e 'for my $i (1 .. 1000000) { $char="a"; $_="cacababaA"; 
$n=tr/$char/$char/; }'
	perl -e   1.01s user 0.00s system 98% cpu 1.026 total

# time perl -e 'for my $i (1 .. 1000000) { $char="a"; $_="cacababaA"; 
$n=s/$char/$char/g; }'
	perl -e   5.54s user 0.00s system 99% cpu 5.546 total

# time perl -e 'for my $i (1 .. 1000000) { $char="a"; 
$string="cacababaA"; for (split //, $string) { ++$count if $_ eq $char } }'
	perl -e   13.32s user 0.02s system 99% cpu 13.351 total


Conclusion: always trust the Camel book ;o)



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

Date: Fri, 3 Oct 2003 10:06:16 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: fastest count of instances in string?
Message-Id: <slrnbnr438.371.tadmc@magna.augustmail.com>

bill <bill_knight2@yahoo.com> wrote:
> 
> 
> What's the fastest way to count how many times a specific character
> appears in a string?


   perldoc -f tr


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Fri, 03 Oct 2003 15:20:21 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: fastest count of instances in string?
Message-Id: <x7ekxunya3.fsf@mail.sysarch.com>

>>>>> "A" == AlV  <skweek@no.spam> writes:

  A> Here are proofs:

  A> # time perl -e 'for my $i (1 .. 1000000) { $char="a"; $_="cacababaA";
  A>   $n=tr/$char/$char/; }'

that is broken code. try it out and see what happens.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Fri, 03 Oct 2003 17:53:15 +0200
From: AlV <skweek@no.spam>
Subject: Re: fastest count of instances in string?
Message-Id: <blk61c$cm6$1@news-reader5.wanadoo.fr>

Uri Guttman wrote:
>>>>>>"A" == AlV  <skweek@no.spam> writes:
> 
> 
>   A> Here are proofs:
> 
>   A> # time perl -e 'for my $i (1 .. 1000000) { $char="a"; $_="cacababaA";
>   A>   $n=tr/$char/$char/; }'
> 
> that is broken code. try it out and see what happens.

You're, of course, right (from perldoc -f perlop):

Because the transliteration table is built at compile time, neither the 
SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote 
interpolation.  That means that if you want to use variables, you must 
use an eval():

                    eval "tr/$oldlist/$newlist/";
                    die $@ if $@;

                    eval "tr/$oldlist/$newlist/, 1" or die $@;

But that would give the very slow:

# time perl -e 'for my $i (1 .. 1000000) { $char="a"; $_="cacababaA"; 
$n= eval "tr/$char/$char/"; } print $n, "\n"; '
	perl -e   31.11s user 0.06s system 99% cpu 31.259 total


So, s//g would be a much faster way to count the occurences of $char... 
or am I still stupid? :o}



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

Date: Fri, 03 Oct 2003 11:22:11 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: fastest count of instances in string?
Message-Id: <Xns94097DAB5FA1Esdn.comcast@206.127.4.25>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

bill <bill_knight2@yahoo.com> wrote in news:blk164$cd5$1@reader2.panix.com:

> 
> 
> What's the fastest way to count how many times a specific character
> appears in a string?  Anything faster than something like
> 
>   for (split //, $string) { ++$count if $_ eq $char }

Probably the fastest way would be to write a C function to do the counting, 
then link to it via XS (or Inline::C).

- -- 
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP32h92PeouIeTNHoEQIuJgCg9Tee/jyxGCNWGzpflde0awH68w4AmwQ4
VuYqJqYZLU45jpFHTUyM3l+0
=BBO6
-----END PGP SIGNATURE-----


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

Date: Fri, 03 Oct 2003 16:33:53 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: fastest count of instances in string?
Message-Id: <x71xtunuvj.fsf@mail.sysarch.com>

>>>>> "A" == AlV  <skweek@no.spam> writes:

  A> Uri Guttman wrote:
  >>>>>>> "A" == AlV  <skweek@no.spam> writes:
  A> $n=tr/$char/$char/; }'
  >> that is broken code. try it out and see what happens.

  A> You're, of course, right (from perldoc -f perlop):

  A> But that would give the very slow:

  A> # time perl -e 'for my $i (1 .. 1000000) { $char="a"; $_="cacababaA";
  A>   $n= eval "tr/$char/$char/"; } print $n, "\n"; '
  A> 	perl -e   31.11s user 0.06s system 99% cpu 31.259 total

remove the eval from the loop. create a sub with that eval tr// in it
and call that in the loop. then you should make all the benchmark
entries call subs to equalize them. tr will be much faster than the
others.

  A> So, s//g would be a much faster way to count the occurences of
  A> $char... or am I still stupid? :o}

not stupid, just not familiar with how to isolate the thing you really
want to benchmark. and there are many benchmark techniques and also ways
to speed up code like tr// with intrpolation (caching code refs
generated by eval, etc.) so you don't call eval each time. eval is the
killer in that loop.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Fri, 03 Oct 2003 19:06:43 +0200
From: AlV <skweek@no.spam>
Subject: Re: fastest count of instances in string?
Message-Id: <blkab3$ih$1@news-reader5.wanadoo.fr>

Uri Guttman wrote:
>>>>>>"A" == AlV  <skweek@no.spam> writes:
> 
>   A> But that would give the very slow:
> 
>   A> # time perl -e 'for my $i (1 .. 1000000) { $char="a"; $_="cacababaA";
>   A>   $n= eval "tr/$char/$char/"; } print $n, "\n"; '
>   A> 	perl -e   31.11s user 0.06s system 99% cpu 31.259 total
> 
> remove the eval from the loop. create a sub with that eval tr// in it
> and call that in the loop. then you should make all the benchmark
> entries call subs to equalize them. tr will be much faster than the
> others.

Huh... :oO

I am certainly dense, but I can't figure how putting the tr// in a sub 
and calling that sub would mean anything other than a longer execution 
time...
(I tried, but certainly not the way you expected ;o)

In the first question of this thread, it was asked whether a faster 
method existed for counting character occurences in a string (other than 
splitting the string and looking through the array).

For me, timing the speed of a given method is timing everything that is 
involved, and for the sake of reading a meaningful value, looping around 
the piece of code that is involved. For example, if it takes, 30ms to 
complete and my expected accuracy is about a second, I need to perform 
100 to 1000 loops before I can expect a "reasonably accurate" value for 
the execution time.

Down to this particular problem, if using tr/// means using an eval 
around it in order to get interpolation, so be it...

Suppose the content of the string or the char (that we are trying to 
count) come to change, eval should be called each time: IMHO, this 
overhead should be part of the timing (some sort of worst case).

But feel free to contradict me :o)

>   A> So, s//g would be a much faster way to count the occurences of
>   A> $char... or am I still stupid? :o}
> 
> not stupid, just not familiar with how to isolate the thing you really
> want to benchmark. and there are many benchmark techniques and also ways
> to speed up code like tr// with intrpolation (caching code refs
> generated by eval, etc.) so you don't call eval each time.

I fail to see how we could cache "code refs generated by eval" (whatever 
that could be, I am no Perl Guru ;o) if the string (in which we are 
looking for the character) and the character itself change...

> eval is the killer in that loop.

That, I had guessed... :o)



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

Date: Fri, 03 Oct 2003 17:18:32 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: fastest count of instances in string?
Message-Id: <x7vfr6me8n.fsf@mail.sysarch.com>

>>>>> "A" == AlV  <skweek@no.spam> writes:

  A> I fail to see how we could cache "code refs generated by eval"
  A> (whatever that could be, I am no Perl Guru ;o) if the string (in which
  A> we are looking for the character) and the character itself change...

simple:

<untested>

my %tr_subs ;

sub tr_chars {

	my ( $tr_chars ) = @_

	$tr_subs{ $tr_chars } ||= eval "sub { tr/$tr_chars// }" ;

	return $tr_subs{ $tr_chars }->() ;
}

this could also be done using the memoize module i would guess but it is
so short i didn't bother. also it does the tr on $_ and it could easily
be changed to work with an argument or defaulting to $_.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: 3 Oct 2003 11:02:14 -0700
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: fastest count of instances in string?
Message-Id: <3ee08638.0310031002.3260a547@posting.google.com>

AlV <skweek@no.spam> wrote in message news:<blk2v6$ub8$1@news-reader5.wanadoo.fr>...
> # time perl -e 'for my $i (1 .. 1000000) { $char="a"; $_="cacababaA"; 
> $n=tr/$char/$char/; }'

You'll get a smidge faster if you don't bother to replace anything.
tr/// doesn't delete unless you specify the /d modifier, so you can
just do
$n=tr/$char//;
and it is non-destructive.


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

Date: 3 Oct 2003 07:49:23 -0700
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: How to generate this list?
Message-Id: <3ee08638.0310030649.6bb004b6@posting.google.com>

"John W. Krahn" <krahnj@acm.org> wrote in message news:<3F7B247C.586F33C@acm.org>...

> I don't know if this is the best way but:
> 
> my @array = qw/ A A A A B B C D D D /;
> 
> print "$_\n" for glob "{@{[ join ',', @array ]}}" x @array;

Would you explain the magic?


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

Date: 3 Oct 2003 08:44:18 -0700
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: How to generate this list?
Message-Id: <3ee08638.0310030744.6d4911a3@posting.google.com>

Bryan <bryan@akanta.com> wrote in message news:<QEDeb.413$yQ6.62530841@newssvr21.news.prodigy.com>...
> If I have the following:
> 
> 4 A's, 2 B's, 1 C, 3 D's
> 
> What's the best way to create a list of -all- the possible combinations 
> of these letters?
> 
> Thanks!

I don't know that there's a "best" way, but here's *a* way. The
recursion gets a bit costly, so I shortened the string some.

use strict;
use warnings;

my @array = split(//, 'AAABBCDD');

sub bucketize {
    my %buckets;
    $buckets{$_}++ for (@_);
    return %buckets;
}

sub permute {
    my %buckets = @_;
    my @rlist = ();  # list of arefs
    my $bucket_found = 0;
    for my $head (sort keys %buckets) {
       next unless $buckets{$head} > 0;
       $bucket_found = 1;
       # Use $head as the first, and prepend it onto
       # the permutations of all the others
       --$buckets{$head};
       # stick $head on the front of each ref'd array
       foreach my $ar (permute(%buckets)) {
           push @rlist, "$head$ar";
       }
       ++$buckets{$head};
    }
    $bucket_found or @rlist = ('');
    return @rlist;
}

for (permute bucketize @array) {
    print "$_\n";
}


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

Date: 3 Oct 2003 08:28:04 -0700
From: pdahle@mail.com (Pal Hulden)
Subject: Interpolation problem
Message-Id: <4c546bb6.0310030728.3159e959@posting.google.com>

Say I have this case:

----
#!/usr/bin/perl -w
$a = "(I leave now";
$_ = "Goodbye (I leave now)";
s/$a//;
print "$_\n";
----

I would like to expect

Goodbye )

as an answer. This does obviously not work (get unmatching parenthesis
error) due to more interpolation that I wanted. I have and have read
Wall/Christiansen/Schwarz' book 'Programming Perl' but I cannot find
the solution to the problem in there.

What is the correct syntax?

Please help.

Sincerely
Pål Huldén
Gothenburg
Sweden


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

Date: Fri, 03 Oct 2003 15:46:09 GMT
From: Kien Ha <kha@rogers.com>
Subject: Re: Interpolation problem
Message-Id: <5Rgfb.203195$Lnr1.97055@news01.bloor.is.net.cable.rogers.com>

Pal Hulden wrote:
> Say I have this case:
> 
> ----
> #!/usr/bin/perl -w
> $a = "(I leave now";
> $_ = "Goodbye (I leave now)";
> s/$a//;
> print "$_\n";
> ----
> 
> I would like to expect
> 
> Goodbye )
> 
> as an answer. This does obviously not work (get unmatching parenthesis
> error) due to more interpolation that I wanted. I have and have read
> Wall/Christiansen/Schwarz' book 'Programming Perl' but I cannot find
> the solution to the problem in there.
> 
> What is the correct syntax?
> 

perldoc -f quotemeta

$a = quotemeta $a;
s/$a//;

or
s/\Q$a//;




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

Date: Fri, 03 Oct 2003 17:50:05 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Interpolation problem
Message-Id: <blk5t1$d741s$1@ID-184292.news.uni-berlin.de>

Pal Hulden wrote:
> Say I have this case:
> 
> ----
> #!/usr/bin/perl -w
> $a = "(I leave now";
> $_ = "Goodbye (I leave now)";
> s/$a//;
> print "$_\n";
> ----
> 
> I would like to expect
> 
> Goodbye )
> 
> as an answer. This does obviously not work (get unmatching
> parenthesis error) due to more interpolation that I wanted. I have
> and have read Wall/Christiansen/Schwarz' book 'Programming Perl'
> but I cannot find the solution to the problem in there
> 
> What is the correct syntax?

     s/\Q$a//;
------^^

Check out perldoc perlre.

> Gothenburg Sweden

Really? Me too. :)

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: Fri, 3 Oct 2003 17:47:59 +0100
From: Hywel Jenkins <hyweljenkins@hotmail.com>
Subject: Re: javascript in HTML ... not running on some browsers
Message-Id: <MPG.19e7b89862d38e4b989701@news.individual.net>

In article <bljbog$70e$1@newsreader.mailgate.org>, 
kaspREMOVE_CAPS@epatra.com says...
> > Can someone tell me whats the problem ? And how can i solve it.
> 
> By posting your question in a Javascript or HTML related newsgroup.

He did.  Then he picked some other groups at random as his client-side 
problem is obviously Perl related.

-- 
Hywel     I do not eat quiche
http://hyweljenkins.co.uk/
http://hyweljenkins.co.uk/mfaq.php


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

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


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