[24765] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6918 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 26 18:11:05 2004

Date: Thu, 26 Aug 2004 15:10: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           Thu, 26 Aug 2004     Volume: 10 Number: 6918

Today's topics:
    Re: There IS a way to test for a file lock (Anno Siegel)
    Re: There IS a way to test for a file lock <tassilo.von.parseval@rwth-aachen.de>
    Re: using the result of a variable regular expression <mritty@gmail.com>
    Re: using the result of a variable regular expression <tore@aursand.no>
    Re: using the result of a variable regular expression (Anno Siegel)
    Re: using the result of a variable regular expression <someone@example.com>
    Re: using the result of a variable regular expression (Anno Siegel)
    Re: using the result of a variable regular expression <mritty@gmail.com>
        XML::Parser error (pj)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 26 Aug 2004 19:41:06 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: There IS a way to test for a file lock
Message-Id: <cgleci$8j0$1@mamenchi.zrz.TU-Berlin.DE>

Sara <genericax@hotmail.com> wrote in comp.lang.perl.misc:
> After querying this group, perldoc, Camel, and my local Perl
> associates, I came up empty on how to deterimine if a file is locked (
> with flock() ). Most of the posts in CLPM suggest where is no way to

[use non-blocking flock()]

> All append the usual comments here to save the naysayers the trouble.
> Good day!
> 
> 
> *****************************************************************************
> 
> 
> This is stupid - why post it?
> 
> Read Camel idiot!
> 
> Did you check perldoc?
> 
> This will never work.
> 
> There is an easier way using a simple 10 line program with eval and
> alarms.

Thank you for your confidence.  You forgot one:

It's off topic.

However, I wonder how you got the impression that this is hard to do
in the first place.  The method you recommend is absolutely standard,
in any language that has flock-based file locking.  Apparently no-one
with a modicum of clue has read your question, or bothered to reply.

Another question is why you want to check for locks.  If you want
one, request it.  If you don't want to hang, do it non-blocking and
check the return value.  That's all there's to it.  There may be
reasons to check if a file is locked (e.g. to see if a process is
still alive), but in the normal business of requesting and releasing
locks, it is useless.

Anno


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

Date: Thu, 26 Aug 2004 22:41:03 +0200
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: There IS a way to test for a file lock
Message-Id: <2p7073FhceiuU1@uni-berlin.de>

Also sprach Anno Siegel:

> Sara <genericax@hotmail.com> wrote in comp.lang.perl.misc:
>> After querying this group, perldoc, Camel, and my local Perl
>> associates, I came up empty on how to deterimine if a file is locked (
>> with flock() ). Most of the posts in CLPM suggest where is no way to
> 
> [use non-blocking flock()]
> 
>> All append the usual comments here to save the naysayers the trouble.
>> Good day!
>> 
>> 
>> *****************************************************************************
>> 
>> 
>> This is stupid - why post it?
>> 
>> Read Camel idiot!
>> 
>> Did you check perldoc?
>> 
>> This will never work.
>> 
>> There is an easier way using a simple 10 line program with eval and
>> alarms.
> 
> Thank you for your confidence.  You forgot one:
> 
> It's off topic.
> 
> However, I wonder how you got the impression that this is hard to do
> in the first place.  The method you recommend is absolutely standard,
> in any language that has flock-based file locking.  Apparently no-one
> with a modicum of clue has read your question, or bothered to reply.

His question had a somewhat familiar ring to me and indeed it showed up
several times in the past with minor variations. According to
google-groups I have given the non-blocking flock advice four times
between 2003/06/14 and 2004/05/18.

However, I wasn't able to find the posting in which he allegedly asked
this group about detecting a lock. And therefore the comments he claims
to have received from this group can only exist in his imagination.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Thu, 26 Aug 2004 18:18:11 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: using the result of a variable regular expression
Message-Id: <DPpXc.21188$rT1.687@trndny02>

[please post your reply *below* what you are posting to.  Thank you]

<leifwessman@hotmail.com> wrote in message
news:cgl82p$9e7@odak26.prod.google.com...
>
> Thanks a lot! I didn't know that. It should say in the perlfaq.

It's in the perldocs, where you're told what m// returns in a list context.

> However, I ended up in a new problem. I want to match in a loop:
>
> $regexp = "(\d)(\w)(\d)";
> while ($data =~ /$regexp/gs){
> print "Your submatch is: $1\n";
> }
>
> this works fine. However...
>
> chomp (my $num = <STDIN>);
> $regexp = "(\d)(\w)(\d)";
> while (my @matches = $data =~ /$regexp/gs){
> print "Your submatch is: $matches[$num-1]\n";
> }
>
> ..becomes an infinite loop. I can't figure out why...

It becomes an infinite loop because you're using a global match in list context.
So on the first iteration, you've already found all the possible matches.  The
next time,  you're asking it to find them all again.  (This differs from the
original because the /g modifier does different things in list and scalar
context - read the relevant perldocs for more info).

The one line
my @matches = $date = /$regexp/g;
gets *all* the submatches, not just the first three.  So what you want is to
loop through @matches and get each corresponding submatch, based on the number
entered by the user:
for ($i = $num; $i < @matches; $i+=3) {
    print "Match: $matches[$i]\n";
}

Some perl programmers have a severe aversion to "C-style" for loops, however.  A
more perl-ish way of writing this might be:
my $i = 0;
foreach ($data =~ /$regexp/g) {
   print "Match: $_\n" if $i++ % 3 == $num - 1;
}

(Personally, I think that's uglier, but fortunately, that's what TMTOWTDI is all
about)

Paul Lalli




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

Date: Thu, 26 Aug 2004 20:40:04 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: using the result of a variable regular expression
Message-Id: <pan.2004.08.26.18.40.03.624514@aursand.no>

On Thu, 26 Aug 2004 09:19:34 -0700, leifwessman wrote:
> Some code to illustrate my problem:
> [...]

Your code won't run.  Please copy-and-paste working code, instead of
retyping it.

You should also add these:

  use strict;
  use warnings;

> $regexp = "(\d)(\w)(\d)";
> $numb   = 3;                # Means the result I'm looking for is in $3
> # I don't know this number, it's submitted
> by user
> # and may differ
> 
> if ($data =~ /$regexp/) {
> 
> print $numb; # does not work, prints "3"
> 
> # alternative solution that works
> # but it's UGLY
> if ($numb == 1) {
> print $1;
> } elsif ($numb == 2) {
> print $2;
> } elsif ($numb == 3) {
> print $3;
> }
> 
>    # is there another way?
> }

You can match into an array;

  if ( my @match = $data =~ /$regexp/ ) {
      print @match[$numb-1];
  }


-- 
Tore Aursand <tore@aursand.no>
"Computer science education cannot make anybody an expert programmer
 any more than studying brushes and pigment can make somebody an expert
 painter." (Eric Raymond)


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

Date: 26 Aug 2004 18:44:53 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: using the result of a variable regular expression
Message-Id: <cglb35$6ku$1@mamenchi.zrz.TU-Berlin.DE>

leifwessman@hotmail.com <leifwessman@hotmail.com> wrote in comp.lang.perl.misc:
> Paul Lalli wrote:
> > <leifwessman@hotmail.com> wrote in message
> > news:cgl4n0$2hd@odak26.prod.google.com...
> > > leifwessman@hotmail.com wrote:

[attributions restored]

You seem to be new around here.  New to Usenet in general and this
newsgroup in particular.  Please see the posting guidelines that are
posted here frequently.  You have violated some of them.

> Thanks a lot! I didn't know that. It should say in the perlfaq.
> 
> However, I ended up in a new problem. I want to match in a loop:
> 
> $regexp = "(\d)(\w)(\d)";
> while ($data =~ /$regexp/gs){
> print "Your submatch is: $1\n";
> }
> 
> this works fine. However...
> 
> chomp (my $num = <STDIN>);
> $regexp = "(\d)(\w)(\d)";
> while (my @matches = $data =~ /$regexp/gs){
> print "Your submatch is: $matches[$num-1]\n";
> }
> 
> ..becomes an infinite loop. I can't figure out why...

That's a problem with the behavior of m//g.  In list context it
matches all occurrences of the match at once (and delivers them all
in one happy list).  If you match again, it matches again.  That's
why you got an endless loop in your second example.

Your first example has the match in scalar context, and that gets special
behavior.  Each consecutive match starts where the last one left off,
so you get them one after the other.  The problem is that you can't
get the list of submatches *and* have the match in scalar context.

If you know the allowed (or possible) range of user selections at
coding time, it's easy.  Say they can select 1 .. 3, then

    while ( $data =~ /$regex/g ) {
        defined and print "$_\n" for ( undef, $1, $2, $3)[ $num];
    }

will do.  It will never print anything for $num = 4, even if $regex
has that many submatches.

If the selection is only bounded at run time, it's harder.  You can
use the match-related arrays @- and @+ (look them up in perlvar).

    while ( $data =~ /$regex/g ) {
        my ( $from, $to) = ( $-[ $num], $+[ $num]);
        print substr( $data, $from, $to - $from), "\n" if defined $to;
    }

This code will allow selection of all submatches, whatever their number.
It will also show the complete match (not only the captured parts) if
$num = 0.

[fullquote snipped]

Anno


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

Date: Thu, 26 Aug 2004 19:13:03 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: using the result of a variable regular expression
Message-Id: <3DqXc.12966$A8.901@edtnps89>

leifwessman@hotmail.com wrote:
> 
> I need to extract a certain value from a text. But the result isn't
> always in the variable $1 - it might be in $2, $3, $4 or some other
> predefined variable.
> 
> Some code to illustrate my problem:
> 
> $regexp = "(\d)(\w)(\d)";
> $numb   = 3;                # Means the result I'm looking for is in $3
> # I don't know this number, it's submitted
> by user
> # and may differ
> 
> if ($data =~ /$regexp/) {
> 
> print $numb; # does not work, prints "3"
> 
> # alternative solution that works
> # but it's UGLY
> if ($numb == 1) {
> print $1;
> } elsif ($numb == 2) {
> print $2;
> } elsif ($numb == 3) {
> print $3;
> }
> 
>    # is there another way?
> }

You are extracting single characters.  How about substr()?

print substr( $data, $numb - 1, 1 )


Why not define your regexp based on the submitted value?

my @fields = ( '\d', '\w', '\d' );
$fields[ $numb - 1 ] = '(' . $fields[ $numb - 1 ] . ')';
my $regexp = join '', @fields;
if ( $data =~ /$regexp/ ) {
     print $1;
     }


Or you could use the @+ and @- arrays:

my $regexp = '(\d)(\w)(\d)';
if ( $data =~ /$regexp/ ) {
     print substr( $data, $-[ $numb ], $+[ $numb ] - $-[ $numb ] );
     }



John
-- 
use Perl;
program
fulfillment


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

Date: 26 Aug 2004 19:17:54 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: using the result of a variable regular expression
Message-Id: <cgld12$7si$1@mamenchi.zrz.TU-Berlin.DE>

Paul Lalli <mritty@gmail.com> wrote in comp.lang.perl.misc:
> [please post your reply *below* what you are posting to.  Thank you]
> 
> <leifwessman@hotmail.com> wrote in message
> news:cgl82p$9e7@odak26.prod.google.com...
> >
> > Thanks a lot! I didn't know that. It should say in the perlfaq.
> 
> It's in the perldocs, where you're told what m// returns in a list context.
> 
> > However, I ended up in a new problem. I want to match in a loop:
> >
> > $regexp = "(\d)(\w)(\d)";
> > while ($data =~ /$regexp/gs){
> > print "Your submatch is: $1\n";
> > }
> >
> > this works fine. However...
> >
> > chomp (my $num = <STDIN>);
> > $regexp = "(\d)(\w)(\d)";
> > while (my @matches = $data =~ /$regexp/gs){
> > print "Your submatch is: $matches[$num-1]\n";
> > }
> >
> > ..becomes an infinite loop. I can't figure out why...
> 
> It becomes an infinite loop because you're using a global match in list context.
> So on the first iteration, you've already found all the possible matches.  The
> next time,  you're asking it to find them all again.  (This differs from the
> original because the /g modifier does different things in list and scalar
> context - read the relevant perldocs for more info).
> 
> The one line
> my @matches = $date = /$regexp/g;
> gets *all* the submatches, not just the first three.  So what you want is to
> loop through @matches and get each corresponding submatch, based on the number
> entered by the user:
> for ($i = $num; $i < @matches; $i+=3) {
>     print "Match: $matches[$i]\n";
> }
> 
> Some perl programmers have a severe aversion to "C-style" for loops, however.
> A

Ugh, watch that line length.

Nah, C-style loops are okay in their place, and this is one of those
places.

> more perl-ish way of writing this might be:
> my $i = 0;
> foreach ($data =~ /$regexp/g) {
>    print "Match: $_\n" if $i++ % 3 == $num - 1;
> }
> 
> (Personally, I think that's uglier, but fortunately, that's what TMTOWTDI is all
> about)

If I wanted to avoid (;;)-loops at any price, I'd probably build the list
of wanted indices, instead of selecting them:

    my @matches = ( undef, $data =~ /$regex/g); # first match at 1
    print "$_\n" for @matches[ map $num + 3*$_, 0 .. @matches/3 - 1];

But for the purpose, the (;;)-loop is just fine.  "map" is less
suitable for generating the same index sequence.

Whether the whole approach is a good one is another question.  It
assumes that the number of submatches is always 3.  Since $regex
is a variable, that assumption may be wrong.

Anno


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

Date: Thu, 26 Aug 2004 19:54:08 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: using the result of a variable regular expression
Message-Id: <AdrXc.6847$oA.6582@trndny04>

"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:cgld12$7si$1@mamenchi.zrz.TU-Berlin.DE...
> Paul Lalli <mritty@gmail.com> wrote in comp.lang.perl.misc:
> > The one line
> > my @matches = $date = /$regexp/g;
> > gets *all* the submatches, not just the first three.  So what you
want is to
> > loop through @matches and get each corresponding submatch, based on
the number
> > entered by the user:
> > for ($i = $num; $i < @matches; $i+=3) {
> >     print "Match: $matches[$i]\n";
> > }
> >
> > Some perl programmers have a severe aversion to "C-style" for loops,
however.
> > A
>
> Ugh, watch that line length.

Sorry about that.  Circumstances have forced me to temporarily use
Outlook express until my system is back in order.  I've changed the
settings, hopefully it will wrap at something more appropriate now...

> Nah, C-style loops are okay in their place, and this is one of those
> places.

I tend to agree, but I wanted to pre-emptively squash the posts I was
expecting to say "Eww, C-Style for loops!  Don't use those in Perl!" :-)


> Whether the whole approach is a good one is another question.  It
> assumes that the number of submatches is always 3.  Since $regex
> is a variable, that assumption may be wrong.

Concur.  Originally, I was going to include something about that, and
then I realized I didn't immediately have a good fix for that new
problem (ie, how do dynamically determine how many tokens are in the
regexp), so I chose to ignore it.  Probably a poor decision on my part.

Paul Lalli




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

Date: 26 Aug 2004 13:48:19 -0700
From: nsf470@yahoo.com (pj)
Subject: XML::Parser error
Message-Id: <11711ebf.0408261248.46d5f1c5@posting.google.com>

Hi, I am getting the following error
----------------------------------------------------------------
not well-formed (invalid token) at line 1, column 0, byte 0 at
/vdt/perl/lib/i386-linux-thread-multi/XML/Parser.pm line 187
----------------------------------------------------------------
when I tried to parse the XML file using XML::Parse::EasyTree 

The xml file I am trying to parse looks like 
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE GANGLIA_XML [
<GANGLIA_XML VERSION="2.5.5" SOURCE="gmetad">
 ....

and I have no idea why it's complaining about the line 1, which is 
<?xml version="1.0" encoding="ISO-8859-1"?>

any ideas? 
thanks in advance


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

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


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