[29274] in Perl-Users-Digest
Perl-Users Digest, Issue: 518 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jun 15 09:14:20 2007
Date: Fri, 15 Jun 2007 06:14:08 -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, 15 Jun 2007 Volume: 11 Number: 518
Today's topics:
Re: Simplify Variable Number of Regex Groups <scobloke2@infotop.co.uk>
Re: Simplify Variable Number of Regex Groups <wcitoan@NOSPAM-yahoo.com>
Re: Simplify Variable Number of Regex Groups <tadmc@seesig.invalid>
Re: Simplify Variable Number of Regex Groups <purlgurl@purlgurl.net>
Re: Simplify Variable Number of Regex Groups <dummy@example.com>
Re: Simplify Variable Number of Regex Groups <wahab-mail@gmx.net>
Strange results of "global match". <krivenok.dmitry@gmail.com>
Re: Strange results of "global match". <mritty@gmail.com>
Re: Strange results of "global match". <peter@makholm.net>
Re: Strange results of "global match". <wahab-mail@gmx.net>
Which Perl 5 OO extension can be seen as "standard" (de <ilias@lazaridis.com>
Re: Win32: How to quit perl script during log off autom <petersob@gmx.net>
Re: Win32: How to quit perl script during log off autom <ThomasKratz@REMOVEwebCAPS.de>
Re: Writing row at a time in Excel using OLE anno4000@radom.zrz.tu-berlin.de
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 15 Jun 2007 11:20:01 +0100
From: Ian Wilson <scobloke2@infotop.co.uk>
Subject: Re: Simplify Variable Number of Regex Groups
Message-Id: <467267d2$0$30332$fa0fcedb@news.zen.co.uk>
W. Citoan wrote:
> I need to reformat portions of text. The patterns I am matching and
> their replacements are as follows:
>
> word1 [note:word1] --> [word1]
> word1 word2 [note:word1_word2] --> [word1 word2]
> word1 word2 word3 [note:word1_word2_word3] --> [word1 word2 word3]
>
...
> I was wondering if there was anyway to
...
> use a single (or smaller set of) substitution.
C:\temp>type citoan.txt
word1 [note:word1]
word1 word2 [note:word1_word2]
word1 word2 word3 [note:word1_word2_word3]
C:\temp>perl -p -e "s/^([^[]*) \[.*$/[$1]/" citoan.txt
[word1]
[word1 word2]
[word1 word2 word3]
You don't say why the above, obvious, solution would not apply. I
suspect you may not have fully stated the problem you are trying to solve.
For example, does the data contain lines like
word1 word2 [note:other_thing]
word1 word2 [notanote:word1_word2]
which should be left unchanged?
------------------------------
Date: Fri, 15 Jun 2007 11:12:43 GMT
From: "W. Citoan" <wcitoan@NOSPAM-yahoo.com>
Subject: Re: Simplify Variable Number of Regex Groups
Message-Id: <slrnf74t1c.2l0.wcitoan@wcitoan-via.verizon.net>
Purl Gurl wrote:
>
> W. Citoan wrote:
>
> > I need to reformat portions of text. The patterns I am matching and
> > their replacements are as follows:
>
> > word1 [note:word1] --> [word1]
> > word1 word2 [note:word1_word2] --> [word1 word2]
> > word1 word2 word3 [note:word1_word2_word3] --> [word1 word2 word3]
> print "[", substr ($_, 0, index ($_, "[") - 1), "]\n\n";
I appreciate the help, but this doesn't work.
1) These patterns occur in larger blocks of text and I need everything
else to remain unchanged. I apologize for not making that clear. In
the case of "keep word1 [note:word1] keep", I need "keep [word1] keep"
and not the "[keep]" your suggestion provides.
2) Your suggestion doesn't actually look for a pattern and produces
false matches. Example: "word1 word2 [note:word1_NOTMATCH]" becomes
"[word1 word2]".
Thanks,
- W. Citoan
--
Even the gods do not fight against necessity.
-- Pittacus
------------------------------
Date: Fri, 15 Jun 2007 11:25:28 GMT
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Simplify Variable Number of Regex Groups
Message-Id: <slrnf74tkd.93r.tadmc@tadmc30.sbcglobal.net>
W. Citoan <wcitoan@NOSPAM-yahoo.com> wrote:
>
> I need to reformat portions of text. The patterns I am matching and
> their replacements are as follows:
>
> word1 [note:word1] --> [word1]
> word1 word2 [note:word1_word2] --> [word1 word2]
> word1 word2 word3 [note:word1_word2_word3] --> [word1 word2 word3]
> However, I was wondering if there was anyway to simplify this so that I
> can use a single (or smaller set of) substitution. If it wasn't for the
> underscores, I could use a larger grouping
So you need a bit of code to insert the underscores before matching:
s#(.*) \[note:(??{ (local $_=$1)=~tr/ /_/; $_})]#[$1]#;
or if you hope for people to be able to read it more easily:
s#(.*)[ ]\[note:
(??{
(local $_ = $1) =~ tr/ /_/;
$_
})
]
#[$1]#x;
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Fri, 15 Jun 2007 04:54:23 -0700
From: Purl Gurl <purlgurl@purlgurl.net>
Subject: Re: Simplify Variable Number of Regex Groups
Message-Id: <bMudnVTuyNlu4O_bnZ2dnUVZ_o6gnZ2d@giganews.com>
W. Citoan wrote:
> Purl Gurl wrote:
>> W. Citoan wrote:
>> print "[", substr ($_, 0, index ($_, "[") - 1), "]\n\n";
> I appreciate the help, but this doesn't work.
This is untrue. My code example works with absolute perfection
and performs precisely the task you describe using the precise
data sample you provide.
Your description of your task does not work. This is your problem.
--
Purl Gurl
--
"Then again what can you expect from a fat-assed, champagne swilling,
half-breed just off the Rez?"
- Joe Kline
------------------------------
Date: Fri, 15 Jun 2007 12:16:52 GMT
From: "John W. Krahn" <dummy@example.com>
Subject: Re: Simplify Variable Number of Regex Groups
Message-Id: <Uqvci.18582$vT6.1717@edtnps90>
W. Citoan wrote:
> I need to reformat portions of text. The patterns I am matching and
> their replacements are as follows:
>
> word1 [note:word1] --> [word1]
> word1 word2 [note:word1_word2] --> [word1 word2]
> word1 word2 word3 [note:word1_word2_word3] --> [word1 word2 word3]
>
> This pattern continues (word4, word5, ... wordN) with an indeterminate
> maximum number of words. I know how to replace each individual case.
>
> For example:
>
> my @data = (
> "word1 [note:word1]",
> "word1 word2 [note:word1_word2]",
> "word1 word2 word3 [note:word1_word2_word3]",
> );
>
> for (@data) {
> print "$_\n";
> s/(\w+)\s\[note:(\1)\]/[$1]/g;
> s/(\w+)\s(\w+)\s\[note:(\1)_(\2)\]/[$1 $2]/g;
> s/(\w+)\s(\w+)\s(\w+)\s\[note:(\1)_(\2)_(\3)\]/[$1 $2 $3]/g;
> print "$_\n\n";
> }
>
> produces:
>
> word1 [note:word1]
> [word1]
>
> word1 word2 [note:word1_word2]
> [word1 word2]
>
> word1 word2 word3 [note:word1_word2_word3]
> [word1 word2 word3]
$ perl -le'
my @data = (
"word1 [note:word1]",
"word1 word2 [note:word1_word2]",
"word1 word2 word3 [note:word1_word2_word3]",
);
for ( @data ) {
print;
s/\A\W+//, s/\W+\z//, s/\s+/ /g for my ( $x, $y ) = split /note:/, $_, 2;
$y =~ tr/_/ /;
print "[$x]" if $x eq $y;
}
'
word1 [note:word1]
[word1]
word1 word2 [note:word1_word2]
[word1 word2]
word1 word2 word3 [note:word1_word2_word3]
[word1 word2 word3]
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: Fri, 15 Jun 2007 14:20:46 +0200
From: Mirco Wahab <wahab-mail@gmx.net>
Subject: Re: Simplify Variable Number of Regex Groups
Message-Id: <f4u0o8$mbi$1@mlucom4.urz.uni-halle.de>
W. Citoan wrote:
> I need to reformat portions of text. The patterns I am matching and
> their replacements are as follows:
>
> word1 [note:word1] --> [word1]
> word1 word2 [note:word1_word2] --> [word1 word2]
> word1 word2 word3 [note:word1_word2_word3] --> [word1 word2 word3]
>
> Any ideas? Am I missing something obvious?
Thats not *that* obvious. After Johns and Tads
working solutions, I'll put another one:
==>
use strict;
use warnings;
my @data = (
'keep word1 [note:word1] as is', # --> [word1]
'word1 word2 [note:word1_word2]', # --> [word1 word2]
'word1 word2 word3 [note:word1_word2_word3]' # --> [word1 word2 word3]
);
print
join "\n",
map {
my ($v) = /(?<=\[note:).+?(?=\])/g; # extract bracketed text
(my $w=$v) =~ y/_/ /; # prepare match in front of [..]
s/(?<=$w\s\[)note:$v(?=\])/$w/; # substitute the brackets if match
} @data;
<==
Regards
Mirco
------------------------------
Date: Fri, 15 Jun 2007 05:28:08 -0700
From: Krivenok Dmitry <krivenok.dmitry@gmail.com>
Subject: Strange results of "global match".
Message-Id: <1181910488.343977.303540@q69g2000hsb.googlegroups.com>
Hello guys!
Can anyone explain me the following results:
krivenok@olimpico ~ $ cat global_match.pl
use strict;
my $str = "aaa noSuchName sadsad asdsa dsa dsad";
while(<>)
{
# NOTE: ig
if($str =~ /noSuchName/ig)
{
print "YES\n";
}
else
{
print "NO\n";
}
}
krivenok@olimpico ~ $ perl global_match.pl
YES
NO
YES
NO
YES
Thank you beforehand!
------------------------------
Date: Fri, 15 Jun 2007 05:35:57 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: Strange results of "global match".
Message-Id: <1181910957.386848.88200@n2g2000hse.googlegroups.com>
On Jun 15, 8:28 am, Krivenok Dmitry <krivenok.dmi...@gmail.com> wrote:
> Can anyone explain me the following results:
>
> krivenok@olimpico ~ $ cat global_match.pl
> use strict;
>
> my $str = "aaa noSuchName sadsad asdsa dsa dsad";
>
> while(<>)
> {
> # NOTE: ig
> if($str =~ /noSuchName/ig)
> {
> print "YES\n";
> }
> else
> {
> print "NO\n";
> }}
>
> krivenok@olimpico ~ $ perl global_match.pl
>
> YES
>
> NO
>
> YES
>
> NO
>
> YES
>
> Thank you beforehand!
The /g modifier starts a "progressive match". That is, it will search
for the first match it can find in the string. If it finds a match,
it returns true, and then *remembers* where it found that match. The
next time the same pattern is executed, the pattern match starts
searching from the point at which it left off. If it finds another
match, it again returns true and remembers where it left off again.
And so on. When it finally cannot find any more matches, it returns
false, and resets the position pointer to the beginning of the string.
So in your example, it found noSuchName starting at position 4 in the
string. It returned true, and kept the position pointer at position
14 - immediately after the successful match. Then the next time
through your while loop, it starts searching the string at position
14, and of course does not find another match. So it returns false,
and resets the position pointer back to the beginning of the string.
The third time through, it once again finds noSuchName at position 4,
so returns true, and updates the position pointer to 14. Etc etc etc.
For more information:
perldoc -f pos
perldoc perlre
Hope this helps,
Paul Lalli
------------------------------
Date: Fri, 15 Jun 2007 12:37:33 +0000
From: Peter Makholm <peter@makholm.net>
Subject: Re: Strange results of "global match".
Message-Id: <87sl8t5r76.fsf@makholm.net>
Krivenok Dmitry <krivenok.dmitry@gmail.com> writes:
> Can anyone explain me the following results:
You might want to read what 'perldoc perlop' says about m//g in scalar
context:
In scalar context, each execution of "m//g" finds the next
match, returning true if it matches, and false if there is no
further match. The position after the last match can be read
or set using the pos() function; see "pos" in perlfunc. A
failed match normally resets the search position to the begin-
ning of the string, but you can avoid that by adding the "/c"
modifier (e.g. "m//gc"). Modifying the target string also
resets the search position.
//Makholm
------------------------------
Date: Fri, 15 Jun 2007 14:34:30 +0200
From: Mirco Wahab <wahab-mail@gmx.net>
Subject: Re: Strange results of "global match".
Message-Id: <f4u1hv$mid$1@mlucom4.urz.uni-halle.de>
Krivenok Dmitry wrote:
> Hello guys!
>
> Can anyone explain me the following results:
>
> krivenok@olimpico ~ $ cat global_match.pl
> use strict;
>
> my $str = "aaa noSuchName sadsad asdsa dsa dsad";
>
> while(<>)
> {
> # NOTE: ig
> if($str =~ /noSuchName/ig)
This is: /g and in scalar context (won't reset $str.pos() before NO_MORE_MATCH_AVAIL)
You probably want' to use ($str) =~ /noSuchName/ig (list context)
or $str =~ /noSuchName/i (scalar context w/out /g)
> YES
first match found, str.pos() at the end of 'noSuchName'
> NO
string end reached (no other match found), str.pos() resetted to 0
> YES
first match found, str.pos() at the end of 'noSuchName'
> NO
string end reached (no other match found), str.pos() resetted to 0
> YES
first match found, str.pos() at the end of 'noSuchName'
Regards
M.
------------------------------
Date: Fri, 15 Jun 2007 12:20:02 -0000
From: Ilias Lazaridis <ilias@lazaridis.com>
Subject: Which Perl 5 OO extension can be seen as "standard" (defacto, quasi)?
Message-Id: <1181910002.327137.71560@q66g2000hsg.googlegroups.com>
I like the very fundamental OO support of Perl 5.
constructs like "separated data/method inheritance", AUTOLOAD, give me
the freedom to implement things as I like: very generic, general and
dynamic.
But as I don't like to "develope away from standard ways to do
things", I've tried to find out which is the OO extension to use with
perl 5.
I've found this very compact one:
http://www.netalive.org/swsu/archives/2004/09/reformed_perl_t.html
but have immediately the problem that "self" is used instead of
"$self" and that it seems that the code adds overhead at execution
time.
Then I've seen Perl6::Bundle which bring Perl 6 stuff to Perl 5
http://www.linux-magazine.com/issue/49/Perl_6_Preview.pdf
but I don't know two things:
* how stable are those implementations?
* how many perl developer do actually use them?
* can I pick just the OO stuff out of the bundle, or are ther
dependencies?
* Is there any document which suggests which modules to use for new
projects?
Any suggestions / comments are welcome.
I summarize all results herein:
http://dev.lazaridis.com/lang/wiki/PerlOO
.
--
http://dev.lazaridis.com/lang/ticket/2
------------------------------
Date: Fri, 15 Jun 2007 08:09:40 +0000 (UTC)
From: Peter Sobisch <petersob@gmx.net>
Subject: Re: Win32: How to quit perl script during log off automatically ?
Message-Id: <f4thg4$8bj$1@online.de>
Reinhard Pagitsch <rprp@gmx.net> schrieb:
>>>> Every time I try to shutdown or log off, pop-up is comming up with
>>>> "application doesn't response" and expecting to kill by clicking a
>>>> button -> very dirty thing :-/
>>>> So, I tried to catch WM_QUERYENDSESSION (using Win32::GUI::Hook), but no
>>> You should reveive a WM_CLOSE according to Bill.
>> not really, I'm affraid windows doesn't sent neither WM_CLOSE nor
>> WM_QUIT because of the same reason of not sending WM_QUERYENDSESSION.
>
> Have you also read the MSDN documentation about SetConsoleCtrlHandler?
> The last paragraph say:
>
> The system generates CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, and
> CTRL_SHUTDOWN_EVENT signals when the user closes the console, logs off,
> or shuts down the system so that the process has an opportunity to clean
> up before termination. Console functions, or any C run-time functions
> that call console functions, may not work reliably during processing of
> any of the three signals mentioned previously. The reason is that some
> or all of the internal console cleanup routines may have been called
> before executing the process signal handler.
this is the thing I wanted to do but the Win32::API::Callback doesn't
work :-(
But I have a small workaround for now, I wrote a C module where I
register a C-written-callback where I run ExitProcess().
It's dirty to me, because I have no possibility to clean up or do
something else, but, the process really ends on logoff.
Neverthereless, I still interesting in Win32::API::Callback to get it
work, because it would be very useful feature in Win32 enviroment.
I tried to write something similars, but how I mentioned above, I've a
problem to call a perl routine from the C-callback, not even the
simplest one from perlcall manpage worked, I got SEGFAULT every time.
I'm not sure, but could it be a problem calling the call_pv() or call_sv()
from the callback which is launching in another thread ?
MS says the CtrlHandler will be called in separated thread, see
the "HandlerRoutine" in MSDN:
------snip------
Because the system creates a new thread in the process to execute the
handler function, it is possible that the handler function will be
terminated by another thread in the process. Be sure to synchronize
threads in the process with the thread for the handler function.
------snap------
> Can you send me the code you are using, maybe I can find it out why the
> Win32::API::Callback Module crashes.
just a simple define of a callback:
----snip---
use Win32::API;
use Win32::API::Callback;
my $callback = Win32::API::Callback->new(
sub { my($a, $b) = @_; return $a+$b; },
"NN", "N",
);
----snap---
gives the SEGFAULT and following message written to the console:
"Free to wrong pool 15e2660 not e58b2660, <DATA> line 164."
------------------------------
Date: Fri, 15 Jun 2007 11:23:11 +0200
From: Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de>
Subject: Re: Win32: How to quit perl script during log off automatically ?
Message-Id: <46725a7f$0$2585$bb690d87@news.main-rheiner.de>
Peter Sobisch wrote:
> ----snip---
> use Win32::API;
> use Win32::API::Callback;
>
> my $callback = Win32::API::Callback->new(
> sub { my($a, $b) = @_; return $a+$b; },
> "NN", "N",
> );
> ----snap---
>
> gives the SEGFAULT and following message written to the console:
> "Free to wrong pool 15e2660 not e58b2660, <DATA> line 164."
Your Win32::API::Callback seems to be broken. The code runs without
errors on my 5.8.8 (windows non ActiveState). Where did you install it from?
Thomas
--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
------------------------------
Date: 15 Jun 2007 08:52:47 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Writing row at a time in Excel using OLE
Message-Id: <5df2avF31ugnoU1@mid.dfncis.de>
anton.vandersteen@chello.nl <anton.vandersteen@chello.nl> wrote in comp.lang.perl.misc:
> On Jun 14, 7:49 pm, Ash <ashish...@gmail.com> wrote:
> > Hi!
> >
> > I need to write a row at a time or a whole sheet at a time, anything
[...]
> In this example I did use an connection to an Access database.
>
> Have fun.
>
>
> #!perl/bin/perl
You're running without strict and warnings!
> use Spreadsheet::WriteExcel;
> use DBI;
[some code snipped]
> #output database results
>
>
> while (@row=$sth9->fetchrow_array())
> {
> $worksheet->write($i, 0, @row[0]);
"@row[ 0]" should be written "$row[ 0]". With "warnings" Perl
would have told you so. Also, the loop body should be indented.
> $worksheet->write($i, 1, @row[1]);
[28 similar lines]
> $worksheet->write($i, 30, @row[30]);
>
> $i=$i+1;
>
> };
About thirty lines of your code can be replaced with a one-line loop:
my $i = 0;
while (@row=$sth9->fetchrow_array()) {
$worksheet->write($i, $_, $row[ $_]) for 0 .. 30;
++ $i;
}
That's what loops are for! Use them.
Anno
------------------------------
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 V11 Issue 518
**************************************