[25735] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7975 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Apr 15 00:05:46 2005

Date: Thu, 14 Apr 2005 21:05:17 -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           Thu, 14 Apr 2005     Volume: 10 Number: 7975

Today's topics:
        =?ISO-8859-1?Q?Composants_application_COM+_appel=E9s_en (CB)
    Re: can anyone explain the last two lines to me?thanks! biomahui@gmail.com
        COM+ component call with Perl (CB)
    Re: COM+ component call with Perl <ebohlman@omsdev.com>
        Command Line single execution of a line(s) of code <mdetomaso@bak.rr.com>
    Re: Command Line single execution of a line(s) of code <noreply@gunnar.cc>
    Re: Comparing Regular Expressions <robert@deadspam.com>
        DBI update <youdontknowme@stranger.com>
    Re: DBI update <glex_nospam@qwest.invalid>
    Re: DBI update <pilkowsk@informatik.uni-marburg.de>
    Re: DBI update xhoster@gmail.com
    Re: DBI update axel@white-eagle.invalid.uk
    Re: DBI update <richard@zync.co.uk>
    Re: DBI update <tadmc@augustmail.com>
    Re: DBI update xhoster@gmail.com
        how to read file when this file is locking.... <sonet.all@msa.hinet.net>
    Re: how to read file when this file is locking.... <noreply@gunnar.cc>
    Re: Is Perl open source? <abigail@abigail.nl>
    Re: Is Perl open source? <matternc@comcast.net>
    Re: Is Perl open source? <groleau+news@freeshell.org>
    Re: Matching mixed up words <usenet@vyznev.invalid>
    Re: Matching mixed up words (Anno Siegel)
    Re: Matching mixed up words <abigail@abigail.nl>
    Re: Matching mixed up words <tassilo.von.parseval@rwth-aachen.de>
    Re: Matching mixed up words <abigail@abigail.nl>
    Re: Matching mixed up words <pilkowsk@informatik.uni-marburg.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 14 Apr 2005 12:38:55 -0700
From: charles_blanchet@hotmail.com (CB)
Subject: =?ISO-8859-1?Q?Composants_application_COM+_appel=E9s_en_PERL?=
Message-Id: <afb1fffd.0504141138.58e6c029@posting.google.com>

Bonjour,
 J'aimerais que mon script PERL puisse utiliser des fonctionnalités
d'une composante (DLL codé en VB6) présente dans une application COM+.

Qu'elle est la façon de faire en Perl pour que je puisse appeler et
utiliser les fonctions et méthodes de cette DLL ?

Merci


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

Date: 14 Apr 2005 20:31:42 -0700
From: biomahui@gmail.com
Subject: Re: can anyone explain the last two lines to me?thanks!
Message-Id: <1113535902.565278.291020@g14g2000cwa.googlegroups.com>


Thank you, Richard.
I find I have two "om" for my hash. I guess it is the reason.
Thanks for the guidance.



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

Date: 14 Apr 2005 13:02:38 -0700
From: charles_blanchet@hotmail.com (CB)
Subject: COM+ component call with Perl
Message-Id: <afb1fffd.0504141202.36fab59d@posting.google.com>

Hi,
 I would like to use a fonctionnality in a Com+ component (DLL create
with VB6) with Perl Script.

How to make to call the DLL functions with Perl command ?

Thanks


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

Date: 15 Apr 2005 00:41:19 GMT
From: Eric Bohlman <ebohlman@omsdev.com>
Subject: Re: COM+ component call with Perl
Message-Id: <Xns9638C9A993228ebohlmanomsdevcom@130.133.1.4>

charles_blanchet@hotmail.com (CB) wrote in 
news:afb1fffd.0504141202.36fab59d@posting.google.com:

> Hi,
>  I would like to use a fonctionnality in a Com+ component (DLL create
> with VB6) with Perl Script.
> 
> How to make to call the DLL functions with Perl command ?

use Win32::OLE;


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

Date: Fri, 15 Apr 2005 03:28:53 GMT
From: "Michael De Tomaso" <mdetomaso@bak.rr.com>
Subject: Command Line single execution of a line(s) of code
Message-Id: <VxG7e.56$K25.34@news-wrt-01.socal.rr.com>

I installed perl for WIN from ActiveState

D:\study\perl>perl -v

This is perl, v5.8.6 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)

Copyright 1987-2004, Larry Wall

 ...

And I was going to try to test some code from:

http://www.perldoc.com/perl5.8.4/pod/func/split.html



Q: let's say I wanted to run a single line of code such as:

print join(':', split(/ */, 'hi there'));

 ... should I be able to run if from the command line like:

>perl -e print join(':', split(/ */, 'hi there'));


Q: should produce the line: " produces the output 'h:i:t:h:e:r:e'. " right 
after the command right ???

- thanks & take care M.D.

<aka>
    _                                _
   | |_   ___    _ __ ___     __ _  | |_   ___     ___
   | __| / _ \  | '_ ` _ \   / _` | | __| / _ \   / _ \
   | |_ | (_) | | | | | | | | (_| | | |_ | (_) | |  __/
    \__| \___/  |_| |_| |_|  \__,_|  \__| \___/   \___|





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

Date: Fri, 15 Apr 2005 05:58:22 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Command Line single execution of a line(s) of code
Message-Id: <3c8sgrF6knbeuU1@individual.net>

Michael De Tomaso wrote:
> I installed perl for WIN from ActiveState

<snip>

> Q: let's say I wanted to run a single line of code such as:
> 
> print join(':', split(/ */, 'hi there'));
> 
> ... should I be able to run if from the command line like:
> 
>>perl -e print join(':', split(/ */, 'hi there'));

Yes, but you are supposed to surround the statement with quotes; in the 
case of Windows it must be double-quotes:

     perl -e "print join(':', split(/ */, 'hi there'))"

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


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

Date: 14 Apr 2005 22:50:29 +0100
From: Robert Inder <robert@deadspam.com>
Subject: Re: Comparing Regular Expressions
Message-Id: <f51d5sxt4d6.fsf@3lg.org>


>>>>> Jürgen Exner writes:
    > Subject: Re: Comparing Regular Expressions
    > Date: Wed, 13 Apr 2005 23:22:52 GMT

    > Mark Jason Dominus wrote:
    >> In article <d30g15$729$1@sun3.bham.ac.uk>,
    >> Brian McCauley  <nobull@mail.com> wrote:
    >>> My instinct says that will be "hard".  Not just "hard" in the common
    >>> sense, but hard in the CompSci sense of being equivalent to the
    >>> halting problem (i.e. insoluble in the general case in finite time).
    >> 
    >> My recollection is that this is not the case.  Certainly equivalence
    >> of recursive languages is undecidable, but regular languages are
    >> highly restricted special cases.

    > Except that Perl's REs (as most other REs that are in common use
    > today) are not"regular" in the Computer Science classification
    > any longer but have been extended.

Right now, I'm just interested in the what can and can't be done.

So if some restrictions on the regexps would let me get a handle on
comparing them, I'd be intrigued.

Even some pointers to work that "merely" dealt with regular RE's 
would be a great help.

    > jue 

Robert.

--
__                                  To avoid the spam trap, mail me
|_) _ |_  _ ._ |-   | _  _| _ ._    at bcs.org.uk, not deadspam.com.
| \(_)|_)(-'|  |_   || |(_|(-'| '   
                                    Best viewed in Ebriated.


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

Date: Thu, 14 Apr 2005 22:32:09 +0100
From: "Bigus" <youdontknowme@stranger.com>
Subject: DBI update
Message-Id: <p_adnUnNDd8ofMPfRVnysQ@giganews.com>

In the following snippet of code, I'm trying to update several fields in a
database record:

  my $sth = $db->prepare("UPDATE items SET $set WHERE id =
'$form{selitem}'");
  $sth->execute($quoted);

$set, in the prepare statement, is a string that contains field/value pairs
like this:

"type = ?, size = ?, description = NULL"

then in the execute line $quoted is a string containing the bind variables

"$form{type},$form{size}"

So what I'm trying to achieve is the equivalent of:

  my $sth = $db->prepare("UPDATE items SET type = ?, size = ?, description =
NULL WHERE id = '$form{selitem}'");
  $sth->execute($form{type},$form{size});

Now the prepare statement works fine but the db error:

"called with 1 bind variables when 2 are needed"

appears because it's taking $quoted as one of the bind variables, which is
expected. I therefore thought I could do something like:

eval("$sth->execute(".$quoted.")");

That doesn't return any error in the Apache log but doesn't update the DB
either, which suggests it's simply not executing.. what's the correct way to
go about this?

Bigus





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

Date: Thu, 14 Apr 2005 17:49:31 -0500
From: "J. Gleixner" <glex_nospam@qwest.invalid>
Subject: Re: DBI update
Message-Id: <%rC7e.25$cW2.652@news.uswest.net>

Bigus wrote:

> then in the execute line $quoted is a string containing the bind variables
> 
> "$form{type},$form{size}"
> 
> So what I'm trying to achieve is the equivalent of:
> 
>   my $sth = $db->prepare("UPDATE items SET type = ?, size = ?, description =
> NULL WHERE id = '$form{selitem}'");
>   $sth->execute($form{type},$form{size});
> 
> Now the prepare statement works fine but the db error:
> 
> "called with 1 bind variables when 2 are needed"
> 
> appears because it's taking $quoted as one of the bind variables, which is
> expected. I therefore thought I could do something like:
> 
> eval("$sth->execute(".$quoted.")");
> 
> That doesn't return any error in the Apache log but doesn't update the DB
> either, which suggests it's simply not executing.. what's the correct way to
> go about this?

Take a look at the documentation for DBI and locate the part about 
"execute" it's documented and there are multiple examples used in the 
documentation that call execute with something other than $scalar.

Personally, I'd just go with what you have above, using your form hash, 
and using a place holder for 'selitem', passing that in execute.


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

Date: Fri, 15 Apr 2005 00:52:08 +0200
From: Fabian Pilkowski <pilkowsk@informatik.uni-marburg.de>
Subject: Re: DBI update
Message-Id: <3c8aduF6lbm8bU1@individual.net>

* Bigus schrieb:

> In the following snippet of code, I'm trying to update several fields in a
> database record:
> 
>   my $sth = $db->prepare("UPDATE items SET $set WHERE id = '$form{selitem}'");
>   $sth->execute($quoted);
> 
> $set, in the prepare statement, is a string that contains field/value pairs
> like this:
> 
> "type = ?, size = ?, description = NULL"
> 
> then in the execute line $quoted is a string containing the bind variables
> 
> "$form{type},$form{size}"
> 
> So what I'm trying to achieve is the equivalent of:
> 
>   my $sth = $db->prepare("UPDATE items SET type = ?, size = ?, description =
> NULL WHERE id = '$form{selitem}'");
>   $sth->execute($form{type},$form{size});
> 
> Now the prepare statement works fine but the db error:
> 
> "called with 1 bind variables when 2 are needed"

Sure, the whole content of $quoted is seen as one argument. It would be
used for the "type = ?" part of your sql-statement. What you want to do
is some kind of splitting into an array with 2 elements:

    my @bind = split /,/, $quoted, 2;
    $sth->execute( @bind );

    # shortened to:
    $sth->execute( split /,/, $quoted, 2 );

Perhaps you could pay attention on this when building the var $quoted
and save your values in an array rather than a skalar.

regards,
fabian


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

Date: 14 Apr 2005 23:12:18 GMT
From: xhoster@gmail.com
Subject: Re: DBI update
Message-Id: <20050414191218.523$29@newsreader.com>

"Bigus" <youdontknowme@stranger.com> wrote:
> In the following snippet of code, I'm trying to update several fields in
> a database record:
>
>   my $sth = $db->prepare("UPDATE items SET $set WHERE id =
> '$form{selitem}'");
>   $sth->execute($quoted);
>
> $set, in the prepare statement, is a string that contains field/value
> pairs like this:
>
> "type = ?, size = ?, description = NULL"
>
> then in the execute line $quoted is a string containing the bind
> variables
>
> "$form{type},$form{size}"

is that

$quoted="$form{type},$form{size}";

or

$quoted='$form{type},$form{size}';


>
> So what I'm trying to achieve is the equivalent of:
>
>   my $sth = $db->prepare("UPDATE items SET type = ?, size = ?,
> description = NULL WHERE id = '$form{selitem}'");
>   $sth->execute($form{type},$form{size});
>
> Now the prepare statement works fine but the db error:
>
> "called with 1 bind variables when 2 are needed"
>
> appears because it's taking $quoted as one of the bind variables, which
> is expected. I therefore thought I could do something like:
>
> eval("$sth->execute(".$quoted.")");

If you change the eval to a print or warn, do you get what you expect?


> That doesn't return any error in the Apache log but doesn't update the DB
> either, which suggests it's simply not executing.. what's the correct way
> to go about this?

What did eval tell you when you asked it?

die $@ if $@;

The most correct way to go about it is probably not to turn code into
strings if you just need to turn it back in code again later.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Thu, 14 Apr 2005 18:18:31 -0500
From: axel@white-eagle.invalid.uk
Subject: Re: DBI update
Message-Id: <z6CdnWK05cDaZ8PfRVn-jw@adelphia.com>

Bigus <youdontknowme@stranger.com> wrote:
 
> Now the prepare statement works fine but the db error:
> "called with 1 bind variables when 2 are needed"
> 
> appears because it's taking $quoted as one of the bind variables, which is
> expected. I therefore thought I could do something like:
> 
> eval("$sth->execute(".$quoted.")");
> 
> That doesn't return any error in the Apache log but doesn't update the DB
> either, which suggests it's simply not executing.. what's the correct way to
> go about this?

Did you check the status of $@ after the eval?

I suggest you use something like this:

	eval '$sth->execute('. $quoted. ')';
 
Otherwise you are trying to insert a hash reference into the eval
expression to be evaluated.

Axel



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

Date: Thu, 14 Apr 2005 23:23:19 GMT
From: Richard Gration <richard@zync.co.uk>
Subject: Re: DBI update
Message-Id: <pan.2005.04.14.23.25.50.632314@zync.co.uk>

On Thu, 14 Apr 2005 22:32:09 +0100, Bigus wrote:

> In the following snippet of code, I'm trying to update several fields in a
> database record:
> 
>   my $sth = $db->prepare("UPDATE items SET $set WHERE id =
> '$form{selitem}'");
>   $sth->execute($quoted);

What happens if $form{selitem} contains the string

q('; delete from items; select * from items where id = ') ?

> $set, in the prepare statement, is a string that contains field/value pairs
> like this:
> 
> "type = ?, size = ?, description = NULL"
> 
> then in the execute line $quoted is a string containing the bind variables
> 
> "$form{type},$form{size}"
> 
> So what I'm trying to achieve is the equivalent of:
> 
>   my $sth = $db->prepare("UPDATE items SET type = ?, size = ?, description =
> NULL WHERE id = '$form{selitem}'");
>   $sth->execute($form{type},$form{size});
> 
> Now the prepare statement works fine but the db error:
> 
> "called with 1 bind variables when 2 are needed"
> 
> appears because it's taking $quoted as one of the bind variables, which is
> expected. I therefore thought I could do something like:
> 
> eval("$sth->execute(".$quoted.")");

That would become 

eval ("HASH(0x813c1c8)->execute($form{type},$form{size})");

Not quite what you wanted. You're in a bit of an interpolation pickle
here. A more correct way to do what you want is something along
these lines (untested):

my (@fields,@values);
foreach my $field (keys %form) {
	next if ($field =~ /^selitem$/) # strip out others if you wish
	push @fields, $field;
	push @values, $form{$field};
}
my $set = join (' = ?,',@fields) . ' = ?';
push @values, $form{selitem};

my $sth = $db->prepare("UPDATE items SET $set WHERE id = ?");
$sth->execute(@values);

I've done things like this in my own code, but I still think it looks ugly.

Rich














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

Date: Thu, 14 Apr 2005 18:21:15 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: DBI update
Message-Id: <slrnd5tunb.9aa.tadmc@magna.augustmail.com>

Bigus <youdontknowme@stranger.com> wrote:
> In the following snippet of code, I'm trying to update several fields in a
> database record:
> 
>   my $sth = $db->prepare("UPDATE items SET $set WHERE id =
> '$form{selitem}'");


You should use a placeholder for $form{selitem} too, or otherwise
arrange for proper quoting of its value.


>   $sth->execute($quoted);
> 
> $set, in the prepare statement, is a string that contains field/value pairs
> like this:
> 
> "type = ?, size = ?, description = NULL"


If you just say:

   $set = "type = ?, size = ?, description = NULL";

in Perl, then you wouldn't need any of the text in the quoted section
above AND it would be unambiguous.

Have you seen the Posting Guidelines that are posted here frequently?


> then in the execute line $quoted is a string containing the bind variables
> 
> "$form{type},$form{size}"
> 
> So what I'm trying to achieve 


You have left out the most important information of all:

   _Why_ are you trying to achieve this?

That is, what do you hope to gain by putting them into a string
rather than into a list?

I get the feeling that this is an "XY problem"...


> is the equivalent of:
> 
>   my $sth = $db->prepare("UPDATE items SET type = ?, size = ?, description =
> NULL WHERE id = '$form{selitem}'");
>   $sth->execute($form{type},$form{size});
> 
> Now the prepare statement works fine but the db error:
> 
> "called with 1 bind variables when 2 are needed"
> 
> appears because it's taking $quoted as one of the bind variables, which is
> expected. 


Right.

You are not calling the function with the proper arguments.


> I therefore thought I could do something like:
> 
> eval("$sth->execute(".$quoted.")");
> 
> That doesn't return any error in the Apache log 


You should not be stealthy about running in the CGI environment,
that is important information that can make a difference.


> but doesn't update the DB
> either, which suggests it's simply not executing.. what's the correct way to
> go about this?


Call the function with the proper arguments.  :-)

   my @quoted = ($form{type}, $form{size});
   ...
   $sth->execute(@quoted);


Better yet (IMHO), do a (hokey) "parse" of $set to find hash
keys that you can use in a hash slice:

   my @cols = $set =~ /(\w+)\s*=\s*\?/g;
   $sth->execute( @form{ @cols } );         # untested

or, after you fix the $form{selitem}' thing mentioned above:

   $sth->execute( @form{ @cols, 'selitem' } );


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


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

Date: 14 Apr 2005 23:31:41 GMT
From: xhoster@gmail.com
Subject: Re: DBI update
Message-Id: <20050414193141.231$yU@newsreader.com>

Richard Gration <richard@zync.co.uk> wrote:
> On Thu, 14 Apr 2005 22:32:09 +0100, Bigus wrote:
>
> > In the following snippet of code, I'm trying to update several fields
> > in a database record:
> >
> >   my $sth = $db->prepare("UPDATE items SET $set WHERE id =
> > '$form{selitem}'");
> >   $sth->execute($quoted);
>
> What happens if $form{selitem} contains the string
>
> q('; delete from items; select * from items where id = ') ?

You get a SQL syntax error message.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Fri, 15 Apr 2005 11:20:22 +0800
From: "sonet" <sonet.all@msa.hinet.net>
Subject: how to read file when this file is locking....
Message-Id: <d3nbv3$7t$1@netnews.hinet.net>

when i use flock to lock file, i can not read the file from another program.
how to lock a file avoid append operate but can read it in the same time.


open(HANDLE,">>flockfile");
&lock;
for (1..5){
     print HANDLE "process $$ is adding this line # $_\n";
     sleep 2;
}
close(HANDLE);

 sub lock {
     print "Now trying to get lock...\n";
     flock(HANDLE,2) or die "could not lock file: $!";   # exclusive lock
with "2"
     print "locked!\n";
      seek(HANDLE, 0, 2);
 }




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

Date: Fri, 15 Apr 2005 05:47:15 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: how to read file when this file is locking....
Message-Id: <3c8rruF6lk00kU1@individual.net>

sonet wrote:
> when i use flock to lock file, i can not read the file from another program.

Yes you can, unless the other program uses flock() as well.

> how to lock a file avoid append operate but can read it in the same time.

<snip>

>      flock(HANDLE,2) or die "could not lock file: $!";   # exclusive lock with "2"

Don't use the value explicitly; use the Fcntl module and the LOCK_EX 
constant to request an exclusive lock.

     perldoc -f flock

To answer your question, the LOCK_SH constant may be what you are 
looking for.

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


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

Date: 14 Apr 2005 22:34:32 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Is Perl open source?
Message-Id: <slrnd5trvo.u4c.abigail@alexandra.abigail.nl>

Chris Mattern (matternc@comcast.net) wrote on MMMMCCXLIV September
MCMXCIII in <URL:news:E6GdnWFtg8bnxMPfRVn-jA@comcast.com>:
))
))  Yet it is good to remember that often, that second of programmer's time
))  is more expensive than the many seconds of machine time that is saved.


Keep remembering that when your airbag takes a few seconds to inflate.

Generalizing is seldomly correct.


Abigail
-- 
BEGIN {$^H {q} = sub {pop and pop and print pop}; $^H = 2**4.2**12}
"Just "; "another "; "Perl "; "Hacker\n";


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

Date: Thu, 14 Apr 2005 19:56:23 -0400
From: Chris Mattern <matternc@comcast.net>
Subject: Re: Is Perl open source?
Message-Id: <ctednT0c5q66nsLfRVn-3w@comcast.com>

Abigail wrote:

> Chris Mattern (matternc@comcast.net) wrote on MMMMCCXLIV September
> MCMXCIII in <URL:news:E6GdnWFtg8bnxMPfRVn-jA@comcast.com>:
> ))
> ))  Yet it is good to remember that often, that second of programmer's
> time
> ))  is more expensive than the many seconds of machine time that is saved.
> 
> 
> Keep remembering that when your airbag takes a few seconds to inflate.
> 
> Generalizing is seldomly correct.
> 
I did say *often*, not *always*.  Of course, anybody doing tasks 
with hard real-time requirements in Perl is certifiable.

-- 
             Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"


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

Date: Thu, 14 Apr 2005 22:00:39 -0500
From: Wes Groleau <groleau+news@freeshell.org>
Subject: Re: Is Perl open source?
Message-Id: <d3nams$42f$1@domitilla.aioe.org>

Josef Moellers wrote:
> A second spent optimizing a frequently used program is many seconds 
> saved during performance.

It used to be said, "A penny saved is a penny earned."

Now, it's more like, "A penny saved is .... ridiculous."

-- 
Wes Groleau

  Guidelines for judging others:

  1. Don't attribute to malice that which
     can be adequately explained by stupidity.

  2. Don't attribute to stupidity that which
     can be adequately explained by ignorance.

  3. Don't attribute to ignorance that which
     can be adequately explained by misunderstanding.


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

Date: Fri, 15 Apr 2005 00:04:35 +0300
From: Ilmari Karonen <usenet@vyznev.invalid>
Subject: Re: Matching mixed up words
Message-Id: <slrnd5tmn3.ajj.usenet@boojum.home.vyznev.net>

Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> kirjoitti 13.04.2005:
> Ilmari Karonen  <usenet@vyznev.invalid> wrote in comp.lang.perl.misc:
>> 
>> Give it a word, and it will return a regex to match any anagram of it.
>
> Nice hack...
>
>> The corresponding regex for "gremlin" is 33218 characters long with
>> the "?:" modifiers, or 25978 without them.
>
> ...and mostly useless.  I like it :)

It does, however, have one advantage -- it's fast.  Really fast.  Over
an order of magnitude faster than any other solution in this thread so
far, in fact.

Of course, that's only if you ignore the time to build and compile the
regex.  And it only works for fairly short words anyway.

But if you want a solution that both runs _and_ starts fast, here's
something adapted from an earlier thread titled "perl scramble":

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

  my $word = shift;
  my $canon = join "", sort split //, "$word\n";

  my $code = q{
      while (<>) {
	  print if length == length $canon
		   and !tr/LETTERS//c
		   and $canon eq join "", sort split //;
      }
  };
  $code =~ s/LETTERS/\Q$canon/;
  eval $code; die if $@; 

This assumes input comes from a file (or stdin), but it can be easily
modified to, say, grep an array.

-- 
Ilmari Karonen
To reply by e-mail, please replace ".invalid" with ".net" in address.


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

Date: 14 Apr 2005 22:09:28 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Matching mixed up words
Message-Id: <d3mpmo$10g$1@mamenchi.zrz.TU-Berlin.DE>

Ilmari Karonen  <usenet@vyznev.invalid> wrote in comp.lang.perl.misc:
> Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> kirjoitti 13.04.2005:
> > Ilmari Karonen  <usenet@vyznev.invalid> wrote in comp.lang.perl.misc:
> >> 
> >> Give it a word, and it will return a regex to match any anagram of it.
> >
> > Nice hack...

[...]

> But if you want a solution that both runs _and_ starts fast, here's
> something adapted from an earlier thread titled "perl scramble":
> 
>   #!/usr/bin/perl -w
>   use strict;
> 
>   my $word = shift;
>   my $canon = join "", sort split //, "$word\n";
> 
>   my $code = q{
>       while (<>) {
> 	  print if length == length $canon
> 		   and !tr/LETTERS//c
> 		   and $canon eq join "", sort split //;
>       }
>   };
>   $code =~ s/LETTERS/\Q$canon/;
>   eval $code; die if $@; 
> 
> This assumes input comes from a file (or stdin), but it can be easily
> modified to, say, grep an array.

Ah, that's basically the "sort-solution", but the length and tr/// tests
speed it up.  Sorting only happens when a word is entirely made of the
same letters, but in different numbers (with the same total).  That helps
a lot in typical situations when most candidates are not anagrams.  Nifty.

Anno


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

Date: 14 Apr 2005 23:06:24 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Matching mixed up words
Message-Id: <slrnd5ttrg.u4c.abigail@alexandra.abigail.nl>

Tassilo v. Parseval (tassilo.von.parseval@rwth-aachen.de) wrote on
MMMMCCXLIII September MCMXCIII in <URL:news:slrnd5pcas.pf.tassilo.von.parseval@localhost.localdomain>:
||  Also sprach Michael T. Davis:
||  
|| > 	Just to be clear, I'm looking for a regex-based mechanism that will
|| > work within the confines of "m/.../".  I would imagine it's going to need to
|| > rely on the "(${code})" construct.
||  
||  Most likely even (??{CODE}). However, any of my attempts so far ended up
||  in a segmentation fault or 'panic: '. I knew that some of these extended
||  patterns are flagged as experimental but I didn't expect them to be that
||  fragile. It's tricky enough coming up with a pure regex solution but
||  here you'll also need to find one that wont crash perl. So I wouldn't
||  bother.


#!/usr/bin/perl

use strict;
use warnings;
no warnings qw /syntax/;

my $word = "gremlin";
my $ana  = "nlmregi";
    
my (%h);
print $word =~
    /^(?{%h = ()})
      (?{$h {substr $ana => $_, 1} ++ for 0 .. length ($ana) - 1})
      (?: (.) (?(?{$h {$1} --> 0})|(?!)) )*
     $(?(?{grep {$_} values %h}})(?!)|)/x ? "match\n" : "no match\n";
 
 
__END__


HTH. HAND.


Abigail
-- 
$_ = "\nrekcaH lreP rehtona tsuJ"; my $chop; $chop = sub {print chop; $chop};
$chop -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()
-> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()


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

Date: Fri, 15 Apr 2005 01:27:52 +0200
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: Matching mixed up words
Message-Id: <slrnd5tv3o.27g.tassilo.von.parseval@localhost.localdomain>

Also sprach Abigail:

> Tassilo v. Parseval (tassilo.von.parseval@rwth-aachen.de) wrote on
> MMMMCCXLIII September MCMXCIII in <URL:news:slrnd5pcas.pf.tassilo.von.parseval@localhost.localdomain>:
>||  Also sprach Michael T. Davis:
>||  
>|| > 	Just to be clear, I'm looking for a regex-based mechanism that will
>|| > work within the confines of "m/.../".  I would imagine it's going to need to
>|| > rely on the "(${code})" construct.
>||  
>||  Most likely even (??{CODE}). However, any of my attempts so far ended up
>||  in a segmentation fault or 'panic: '. I knew that some of these extended
>||  patterns are flagged as experimental but I didn't expect them to be that
>||  fragile. It's tricky enough coming up with a pure regex solution but
>||  here you'll also need to find one that wont crash perl. So I wouldn't
>||  bother.
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> no warnings qw /syntax/;
>
> my $word = "gremlin";
> my $ana  = "nlmregi";
>     
> my (%h);
> print $word =~
>     /^(?{%h = ()})
>       (?{$h {substr $ana => $_, 1} ++ for 0 .. length ($ana) - 1})
>       (?: (.) (?(?{$h {$1} --> 0})|(?!)) )*
>      $(?(?{grep {$_} values %h}})(?!)|)/x ? "match\n" : "no match\n";

Hmmh, this doesn't compile:

Sequence (?{...}) not terminated or not {}-balanced in regex; marked by <-- HERE in m/^(?{%h = ()})
      (?{$h {substr $ana => $_, 1} ++ for 0 .. length ($ana) - 1})
      (?: (.) (?(?{$h {$1} --> 0})|(?!)) )*
     $(?(?{ <-- HERE grep {$_} values %h}})(?!)|)/ at - line 13.

I can get rid of these by strategically inserting a few spaces here and
there, but then it eventually complains about an "Unknown switch
condition".

Tassilo
-- 
use bigint;
$n=71423350343770280161397026330337371139054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);


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

Date: 14 Apr 2005 23:50:40 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Matching mixed up words
Message-Id: <slrnd5u0eg.u4c.abigail@alexandra.abigail.nl>

Tassilo v. Parseval (tassilo.von.parseval@rwth-aachen.de) wrote on
MMMMCCXLIV September MCMXCIII in <URL:news:slrnd5tv3o.27g.tassilo.von.parseval@localhost.localdomain>:
,,  Also sprach Abigail:
,,  
,, > [ Snip ]
,,  
,,  Hmmh, this doesn't compile:
,,  
,,  Sequence (?{...}) not terminated or not {}-balanced in regex; marked by <-- HERE in m/^(?{%h = ()})
,,        (?{$h {substr $ana => $_, 1} ++ for 0 .. length ($ana) - 1})
,,        (?: (.) (?(?{$h {$1} --> 0})|(?!)) )*
,,       $(?(?{ <-- HERE grep {$_} values %h}})(?!)|)/ at - line 13.


Cut-and-paste error. Retry:

#!/usr/bin/perl

use strict;
use warnings;
no warnings qw /syntax/;

my $word = "gremlin";
my $ana  = "nlmregi";

my (%h);
print $word =~
    /^(?{%h = ()})
      (?{$h {substr $ana => $_, 1} ++ for 0 .. length ($ana) - 1})
      (?: (.) (?(?{$h {$1} --> 0})|(?!)) )*
     $(?(?{grep {$_} values %h})(?!)|)/x
    ? "match\n" : "no match\n";


__END__


Abigail
-- 
map{${+chr}=chr}map{$_=>$_^ord$"}$=+$]..3*$=/2;        
print "$J$u$s$t $a$n$o$t$h$e$r $P$e$r$l $H$a$c$k$e$r\n";


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

Date: Fri, 15 Apr 2005 02:07:29 +0200
From: Fabian Pilkowski <pilkowsk@informatik.uni-marburg.de>
Subject: Re: Matching mixed up words
Message-Id: <3c8eqlF6kvpljU1@individual.net>

* Tassilo v. Parseval schrieb:
> Also sprach Abigail:
> >   
> > my (%h);
> > print $word =~
> >     /^(?{%h = ()})
> >       (?{$h {substr $ana => $_, 1} ++ for 0 .. length ($ana) - 1})
> >       (?: (.) (?(?{$h {$1} --> 0})|(?!)) )*
> >      $(?(?{grep {$_} values %h}})(?!)|)/x ? "match\n" : "no match\n";
                                  ^^
> 
> Hmmh, this doesn't compile:

Delete one of those marked curly parentheses and it'll work fine.

regards,
fabian


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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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


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