[23869] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6072 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Feb 3 14:05:52 2004

Date: Tue, 3 Feb 2004 11:05:07 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 3 Feb 2004     Volume: 10 Number: 6072

Today's topics:
        capturing a match (Chris)
    Re: capturing a match <ittyspam@yahoo.com>
    Re: capturing a match <pinyaj@rpi.edu>
    Re: capturing a match <dwall@fastmail.fm>
    Re: capturing a match <bmb@ginger.libs.uga.edu>
    Re: CGI syntax error not behaving as I expected but... <mmosher@storm.ca>
    Re: CGI syntax error not behaving as I expected but... <ThomasKratz@REMOVEwebCAPS.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 3 Feb 2004 08:10:24 -0800
From: c.cole@umist.ac.uk (Chris)
Subject: capturing a match
Message-Id: <da822336.0402030810.5ede94b3@posting.google.com>

Below is a grepped list of data I want to parse in Perl. This was
initially going to be done on a one-liner hence why grep was
used.

out.lis:
profs/pr00001.clean:Query id = asni00107
profs/pr00002.clean:Query id = asni00108
profs/pr00003.clean:Query id = asni00109
profs/pr00004.clean:Query id = asni00110
profs/pr00005.clean:Query id = asni00111
profs/pr00006.clean:Query id = asni00112
profs/pr00007.clean:Query id = asni00113
profs/pr00008.clean:Query id = asni00114
profs/pr00009.clean:Query id = asni00115
profs/pr00010.clean:Query id = asni00116

From this list I need the 'pr000??' data and the 'asni00??' data
from each line.

The following script does what I want (ie. print
'asni00107=pr00001'): 

#! /usr/bin/perl -w

use strict;

open (FILE, "out.lis") or die "Can't open file: $!\n";

my %h;

while (<FILE>) {

        my @F = split;
        print "4th field: $F[3]\n";
        $F[0] =~ /(pr\d{5})/;
        $h{$F[3]} = $1;
}
close(FILE);

foreach my $k (sort keys %h) {
        print "$k=$h{$k}\n";
}
exit;

But I wanted to do this:
my $h{$F[3]} = $F[0] =~ /(pr\d{5})/;

However, this gives 'asni00107=1'. How can I get the capturing to
return the match rather than the number of matches? I've done
this before with success and I'm sure I haven't missed anything
obvious (have I?).
Any pointers appreciated.

Chris.


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

Date: Tue, 3 Feb 2004 11:26:54 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: capturing a match
Message-Id: <20040203112605.I483@dishwasher.cs.rpi.edu>

On Tue, 3 Feb 2004, Chris wrote:

> But I wanted to do this:
> my $h{$F[3]} = $F[0] =~ /(pr\d{5})/;
>
> However, this gives 'asni00107=1'. How can I get the capturing to
> return the match rather than the number of matches? I've done
> this before with success and I'm sure I haven't missed anything
> obvious (have I?).
> Any pointers appreciated.
>

You need list context.

my ($h{$F[3]}) = $F[0] =~ /(pr\d{5})/;

Paul Lalli



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

Date: Tue, 3 Feb 2004 11:56:47 -0500
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
To: Chris <c.cole@umist.ac.uk>
Subject: Re: capturing a match
Message-Id: <Pine.SGI.3.96.1040203115526.97849A-100000@vcmr-64.server.rpi.edu>

[posted & mailed]

On 3 Feb 2004, Chris wrote:

>my $h{$F[3]} = $F[0] =~ /(pr\d{5})/;

First of all, the 'my' will give you a syntax error.

Anyway, just put the $h{...} in parentheses.  This enforces list context
on the right-hand side of the =.

  ($h{$F[3]}) = $F[0] =~ /(pr\d{5})/;

-- 
Jeff Pinyan            RPI Acacia Brother #734            2003 Rush Chairman
"And I vos head of Gestapo for ten     | Michael Palin (as Heinrich Bimmler)
 years.  Ah!  Five years!  Nein!  No!  | in: The North Minehead Bye-Election
 Oh.  Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)



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

Date: Tue, 03 Feb 2004 16:56:57 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: capturing a match
Message-Id: <Xns9484798D74002dkwwashere@216.168.3.30>

Chris <c.cole@umist.ac.uk> wrote:

> Below is a grepped list of data I want to parse in Perl. This was
> initially going to be done on a one-liner hence why grep was
> used.
> 
> out.lis:
> profs/pr00001.clean:Query id = asni00107
> profs/pr00002.clean:Query id = asni00108
> profs/pr00003.clean:Query id = asni00109
> profs/pr00004.clean:Query id = asni00110
> profs/pr00005.clean:Query id = asni00111
> profs/pr00006.clean:Query id = asni00112
> profs/pr00007.clean:Query id = asni00113
> profs/pr00008.clean:Query id = asni00114
> profs/pr00009.clean:Query id = asni00115
> profs/pr00010.clean:Query id = asni00116
> 
> From this list I need the 'pr000??' data and the 'asni00??' data
> from each line.

grep ... | perl -ne 'print "$2=$1\n" if m{^\S+/(pr\d+)\S+ id = (asni\d+)$}'

There are lots of other ways to write the regex; I just took a lazy way.



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

Date: Tue, 3 Feb 2004 11:54:48 -0500
From: Brad Baxter <bmb@ginger.libs.uga.edu>
Subject: Re: capturing a match
Message-Id: <Pine.A41.4.58.0402031147430.14072@ginger.libs.uga.edu>

On Tue, 3 Feb 2004, Chris wrote:

> But I wanted to do this:
> my $h{$F[3]} = $F[0] =~ /(pr\d{5})/;
>
> However, this gives 'asni00107=1'. How can I get the capturing to
> return the match rather than the number of matches? I've done
> this before with success and I'm sure I haven't missed anything
> obvious (have I?).
> Any pointers appreciated.

I think this is what you're after:

($h{$F[3]}) = $F[0] =~ /(pr\d{5})/;

Or perhaps ...

perl -F'\W+' -lane'print"$F[5]=$F[1]"' out.lis
perl -F'\W+' -lape'$_="$F[5]=$F[1]"' out.lis

Regards,

Brad


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

Date: Tue, 03 Feb 2004 11:52:24 -0500
From: mmosher <mmosher@storm.ca>
Subject: Re: CGI syntax error not behaving as I expected but...
Message-Id: <bvojkd$qa5$1@news.storm.ca>

Thank you both for the explaination. Should have relized with was my 
logic error and not syntax. My thinking was that since it was in an if 
statement I could not do an assignment. Everything makes sense now. I'll 
check that document.

Yes I can be taught.

mmosher wrote:

> ... doing exactly what its supposed to do.
>
> I wrote a perl script to allow me to look at portions of the log that 
> is kept for people searching. It was written some time ago and seemed 
> to work perfectly. It was not until last week that I noticed that 
> after 12pm the time would jump back an hour and stay that way until 
> 12am. I suspected a bug but doubled check incase of some weird virus 
> or something.
>
> Of course it was a bug:
>
>  if (substr($date_time_raw[$loop],8,2)<12)           {##do something}
>  elsif (substr($date_time_raw[$loop],8,2)=12)       {##do something}
>  
> else                                                                      
> {##do something}
>
> Of course the error is in the elsif statement it should be ==12 not 
> =12. So of course it works in the am the pm messes up.
>
> My question is this: I thought a syntax error of this type would not 
> run and that I would get an error with no output? Or is this type of 
> error considered a warning?
>
> I am fairly inexperienced in perl and usually have to write stuff as 
> "we need this up tommorrow. Figure it out quick" kinda of situation 
> which is not a good way to actually learn and understand. I also do 
> most of perl work in the morning and it did not occur to change the 
> clock and test different hours. Lesson learned.
>
> Thank you.
>



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

Date: Tue, 03 Feb 2004 18:10:35 +0100
From: Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de>
Subject: Re: CGI syntax error not behaving as I expected but...
Message-Id: <401fd6f7.0@juno.wiesbaden.netsurf.de>

mmosher wrote:

> Thank you both for the explaination. Should have relized with was my 
> logic error and not syntax. My thinking was that since it was in an if 
> statement I could not do an assignment. Everything makes sense now. I'll 
> check that document.
> 
> Yes I can be taught.

Can you be taught to stop top posting?

Thomas


-- 
open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</  #.>s^~h<t< ..~. ...c.^..
&&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..


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

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


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