[23693] in Perl-Users-Digest
Perl-Users Digest, Issue: 5900 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Dec 5 18:05:49 2003
Date: Fri, 5 Dec 2003 15:05:11 -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 Fri, 5 Dec 2003 Volume: 10 Number: 5900
Today's topics:
appending to file appends twice!? <someone@somewhere.nb.ca>
create @AoAref from a file <eddGallary2@hotmail.com>
Re: create @AoAref from a file <usenet@morrow.me.uk>
Re: create @AoAref from a file <noreply@gunnar.cc>
Re: create @AoAref from a file <eddGallary2@hotmail.com>
Re: create @AoAref from a file <usenet@morrow.me.uk>
Re: Don't Read - Testing (Daniel R. Tobias)
Executing a command on a remote Windows 2000 machine <asdf@asdf.com>
Re: Getting return code from a child process <usenet@morrow.me.uk>
Re: Getting return code from a child process <jgibson@mail.arc.nasa.gov>
Re: gui in perl <xxala_qumsiehxx@xxyahooxx.com>
Re: gui in perl (James Willmore)
Re: Idiom for array index that I'm foreach'ing over? (Anno Siegel)
perl against ms access (Mike_red)
Re: perl against ms access <usenet@morrow.me.uk>
Re: perl against ms access <usenet@dwall.fastmail.fm>
read file with while and then scan lines into array (Martin Foster)
Re: Regex Question <mikeflan@earthlink.net>
Re: SGML/HTML syntax trivia (was Re: any idea how to op <usenet@dwall.fastmail.fm>
Re: sorting file names (Chris Charley)
Re: subroutine parameter with regex <chatasos@yahoo.com>
Re: subroutine parameter with regex <usenet@morrow.me.uk>
Syntax problem <undisclosed_sender@undisclosed_domain.com>
Re: Syntax problem <dover@nortelnetworks.com>
Re: Syntax problem <dover@nortelnetworks.com>
Re: Syntax problem <usenet@morrow.me.uk>
Re: Syntax problem <trammell+usenet@hypersloth.invalid>
Re: Syntax problem <undisclosed_sender@undisclosed_domain.com>
Re: Using a hash for the post data in LWP::useragent? <theaney@cablespeed.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 05 Dec 2003 19:08:42 GMT
From: "Guy" <someone@somewhere.nb.ca>
Subject: appending to file appends twice!?
Message-Id: <_I4Ab.5192$IF6.255648@ursa-nb00s0.nbnet.nb.ca>
Hi all, :-)
I wrote a Perl script to append Questions and Answers to a text data file.
The file would look like this and would grow when Q&As are added.
1 Q This is a question
1 A This is an answer
2 Q This is another question
2 A This is another answer
All I do is open the file for append, print 2 lines (Q line and A line) and
then close the file. What's happening however is that my data is appended
twice, like this: This is an actual cut & Paste of the entire file.
1 Q Here is my question
1 A Hee is my answer
2 Q Here is my question
2 A Hee is my answer
3 Q here is another question
3 A this is another answer
4 Q here is another question
4 A this is another answer
5 Q qqqqq
5 A zaaaaaa
6 Q qqqqq
6 A zaaaaaa
7 Q sdfkhj asdlfh fih asdfh ;alsdfh
7 A asdkfj; alsdfh ausf iaufh jkahf kawhef awef
8 Q sdfkhj asdlfh fih asdfh ;alsdfh
8 A asdkfj; alsdfh ausf iaufh jkahf kawhef awef
What's worst is that you notice on the above file that the count does
increment as if my routine is called twice. So what I've done was added a
few check-point print commands inside the routine and these only print once,
as if my routine is only called once.
Any ideas,
Guy
FYI
They tell me the server is a SUN Sparc V100 with dual 100mb ports.
The Web is provided by an iPlanet webserver.
It is Perl 5.008
Curious
Here is the code for the updating routine...
sub f25
{
my $chk=0;
my $number=0;
my $qaline;
my @qafield;
&prt_head; # Prints HTML code back to browser
$chk=get_qa(); # see below
if($chk==0)
{
foreach $qaline (@qafile)
{
@qafield=();
push(@qafield,split(/\t/,$qaline));
$number=$qafield[0];
}
print"Number = $number <BR> \n"; # checkpoint print command
$number++;
print"Number=$number<BR><BR><BR>\n"; # checkpoint print command
$chk=3840;
if(-d "$fd")
{
$chk=256;
if(open(FILE,">>$qafil"))
{
print"Added<BR>\n";
print FILE "$number\tQ\t$fq\t\n$number\tA\t$fa\t\n";
close(FILE);
print"Done<BR>\n";
$chk=0;
}
}
}
if($chk != 0){erreur($chk);}
print"</FORM></BODY></HTML>\n";
}
sub get_qa
{
my $chk=3840;
if(-d "$fd")
{
$chk=256;
if(open(FILE,"$qafil"))
{
@qafile=<FILE>;
close(FILE);
$chk=0;
}
}
return $chk;
}
------------------------------
Date: Sat, 06 Dec 2003 06:24:06 +1100
From: Edo <eddGallary2@hotmail.com>
Subject: create @AoAref from a file
Message-Id: <3FD0DB56.5040101@hotmail.com>
Hello
I am having hard time with these compex data structure although I have
been doing lots of reading, but with out doing it my self I will not
learn it. so after few hours last night and today, I gave up.
I have a file
a A
b B
c C
d D
I need to create array like this
@array = (a [A B C D]);
this is puting out
Can't use string ("a") as an ARRAY ref while "strict refs" in use ...
while (<FH>) {
$. == 1 ?
push @array, (split //)[0] :
push @{ $array[0]}, (split //)[1];
}
thanks
------------------------------
Date: Fri, 5 Dec 2003 19:36:07 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: create @AoAref from a file
Message-Id: <bqqmn7$mpn$3@wisteria.csv.warwick.ac.uk>
Edo <eddGallary2@hotmail.com> wrote:
> I have a file
>
> a A
> b B
> c C
> d D
>
> I need to create array like this
> @array = (a [A B C D]);
...but with commas :).
> this is puting out
> Can't use string ("a") as an ARRAY ref while "strict refs" in use ...
> while (<FH>) {
> $. == 1 ?
> push @array, (split //)[0] :
If you want the 'A' in your sub-array you want
$. == 1 and push @array, (split //)[0];
rather than ?:.
> push @{ $array[0]}, (split //)[1];
This says:
take the first element of @array,
de-ref it as an ARRAY ref,
push something onto it.
The first element of @array is the 'a' you just pushed on, as the
error told you. What you meant was
push @{$array[1]}, (split //)[1];
.
Ben
--
Joy and Woe are woven fine,
A Clothing for the Soul divine William Blake
Under every grief and pine 'Auguries of Innocence'
Runs a joy with silken twine. ben@morrow.me.uk
------------------------------
Date: Fri, 05 Dec 2003 20:43:15 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: create @AoAref from a file
Message-Id: <bqqnjn$265p1a$1@ID-184292.news.uni-berlin.de>
Edo wrote:
> I am having hard time with these compex data structure although I
> have been doing lots of reading, but with out doing it my self I
> will not learn it. so after few hours last night and today, I gave
> up. I have a file
>
> a A
> b B
> c C
> d D
>
> I need to create array like this
> @array = (a [A B C D]);
Do you mean
@array = ('a', ['A', 'B', 'C', 'D']);
??
> this is puting out
> Can't use string ("a") as an ARRAY ref while "strict refs" in use
> ...
> while (<FH>) {
> $. == 1 ?
> push @array, (split //)[0] :
> push @{ $array[0]}, (split //)[1];
> }
1. Provided that my guess about what it is you want to create is
correct (se above), it shall be $array[1] in the last 'push'
statement. Was that so hard to figure out? The error message let you
know which line caused the error, didn't it?
2. The last 'push' statement shall be executed also for the first
line, or did you change your mind?
3. You dont want "split //", but rather just "split". Please re-read
the docs for the split() function.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sat, 06 Dec 2003 07:52:14 +1100
From: Edo <eddGallary2@hotmail.com>
Subject: Re: create @AoAref from a file
Message-Id: <3FD0EFFE.4070502@hotmail.com>
> 2. The last 'push' statement shall be executed also for the first
> line, or did you change your mind?
no, I don't have a strong base in perl, bad habbit being more of a goal
than process orinted.
my $start = 5; #start at line 5 of the file
my $field = 2; #that will be in the arrayref of the array
my $number = 15 # collect 15 lines including the start line
my @seg;
while (<FH>) {
next if $. < $start;
if ( $. == $start ) {
push @seg, (split /,/)[1];
push @{ $seg[1]}, (split /,/)[$field];
} else {
push @{ $seg[1]}, (split /,/)[$field];
}
last if $. == $start + $number -1;
}
I just wanted to use the ?: all in one line, gives me the "maybe-false"
feeling that it will run faster than 6 line substitute.
thanks
------------------------------
Date: Fri, 5 Dec 2003 21:07:00 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: create @AoAref from a file
Message-Id: <bqqs1k$qob$1@wisteria.csv.warwick.ac.uk>
Edo <eddGallary2@hotmail.com> wrote:
> my $start = 5; #start at line 5 of the file
> my $field = 2; #that will be in the arrayref of the array
> my $number = 15 # collect 15 lines including the start line
> my @seg;
>
> while (<FH>) {
> next if $. < $start;
last if $. == $start + $number;
(unless it matters that this will read one more line than what you had.)
> if ( $. == $start ) {
> push @seg, (split /,/)[1];
> push @{ $seg[1]}, (split /,/)[$field];
> } else {
> push @{ $seg[1]}, (split /,/)[$field];
> }
First rule of programming: never ever write the same thing twice.
$. == start and push @seg, (split /,/)[1];
push @{$seg[1]}, (split /,/)[$field];
> last if $. == $start + $number -1;
> }
>
> I just wanted to use the ?: all in one line, gives me the "maybe-false"
> feeling that it will run faster than 6 line substitute.
Second rule of programming: don't think about whether things will run
faster, benchmark. If it matters, which it usually doesn't.
A ?: construct will run exactly as fast as the equivalent if...else:
the extra lines of code make no difference. What matters is what the
code is doing.
Ben
--
Joy and Woe are woven fine,
A Clothing for the Soul divine William Blake
Under every grief and pine 'Auguries of Innocence'
Runs a joy with silken twine. ben@morrow.me.uk
------------------------------
Date: 5 Dec 2003 14:29:50 -0800
From: dan@tobias.name (Daniel R. Tobias)
Subject: Re: Don't Read - Testing
Message-Id: <aab17256.0312051429.753c0952@posting.google.com>
"Matt Garrish" <matthew.garrish@sympatico.ca> wrote in message news:<75xzb.10435$zf2.1295016@news20.bellglobal.com>...
> Don't think of posting to Usenet as being akin to writing an email to
> someone. Emailers hate it when others intersperse comments in their messages
> because it makes following the train of logic more difficult (i.e., it's
> usually a conversation between two people, so you know what you're both
> talking about).
I, for one, have always used the same interleaved reply style for
e-mail as I have for newsgroups; mailing lists, Web forums, and (back
when I still used them) dialup BBS discussion areas merited the same
approach.
My comments on reply quoting style:
http://mailformat.dan.info/quoting/
--
Dan
------------------------------
Date: Fri, 05 Dec 2003 11:26:49 -0800
From: Noname <asdf@asdf.com>
Subject: Executing a command on a remote Windows 2000 machine
Message-Id: <1km1tv8rnbc18pm324qp8p2pd4ocluvlsp@4ax.com>
Is there a perl module that will allow me to execute a command on a
remote Win2K machine? Basically, I would like to make a cluster
administration script that uses the Win32API rather than resorting to
system calls and hackish programs like psexec.
Thanks
................................................................
Posted via TITANnews - Uncensored Newsgroups Access
>>>> at http://www.TitanNews.com <<<<
-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-
------------------------------
Date: Fri, 5 Dec 2003 19:19:21 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Getting return code from a child process
Message-Id: <bqqlnp$mpn$1@wisteria.csv.warwick.ac.uk>
jhayden@usa.com (Jon) wrote:
> The wait() function returns the pid and what I really want is the
> return code from that pid.
What this appears to be asking for is the '0' out of 'exit 0'; you can
get that with $? >> 8 after a successful wait(). See perlvar.
> I would like to pass the return code of a
> function I ran in the child back to the parent...Is there a simple way
> to do this?
If the value you wish to return is an integer between 0 and 255,
inclusive, you can pass it to exit in the child and retreive it as
above in the parent. If it is anything more complicated than that, you
have basically two options:
1. Use threads instead of processes. You'll want 5.8 for this; start
by reading perldoc perlthrtut.
> I *think* there might be a way to do this with pipes, but
> I have no clue as to how to do that.
2. Pass the data back from child to parent in a pipe. You need to
decide on some format for your data: if there are no other
considerations, Storable provides a compact representation of
'most any Perl data structure. The way you would do this would be
something like (this is completely untested and written straight
off the top of my head):
use Storable qw/store_fd fd_retrieve/;
my $kid = fork;
defined $kid or die "fork failed: $!";
pipe my $FROM, my $TO;
if($kid) {
close $TO;
# parent
} else {
close $FROM;
my $return = my_function();
store_fd \$return, $TO; # you have to store a reference
exit 0;
}
my $return = fd_retrieve $FROM or die "fd_retrieve: $!";
$return = $$return; # de-ref the reference
close $FROM;
Read the docs for Storable to see what you can send and how to send it.
Ben
--
And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
------------------------------
Date: Fri, 05 Dec 2003 12:59:18 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Getting return code from a child process
Message-Id: <051220031259181465%jgibson@mail.arc.nasa.gov>
In article <28769dad.0312051024.790055a7@posting.google.com>, Jon
<jhayden@usa.com> wrote:
> How do I get the return value of a child process? Take this example:
>
> ====================================
[non-working program snipped]
>
>
> The wait() function returns the pid and what I really want is the
> return code from that pid. I would like to pass the return code of a
> function I ran in the child back to the parent...Is there a simple way
> to do this? I *think* there might be a way to do this with pipes, but
> I have no clue as to how to do that.
>
> Thanks for any help,
>
> Jon
There isn't really a return value from a terminating child process.
There is an "error status" that a terminating process can return to the
operating system and that can possibly be fetched by the parent process
of the terminating child process:
#!/opt/perl/bin/perl
use strict;
use warnings;
my $child_pid=0;
if (!defined($child_pid = fork())) {
die "cannot fork: $!";
} elsif ($child_pid) {
# I'm the parent
print "child pid $child_pid forked\n";
} else {
# I'm the child
my $return = 10;
exit $return;
}
my $pid = wait;
my $status = $? >> 8;
print("parent: child $pid exited with status $status\n")
__OUTPUT__
child pid 4152 forked
parent: child 4152 exited with status 10
The exit status of the child is returned to the parent process in the
upper bits of the error status (the lower 8 bits are used for something
else). Since the operating system or the shell is munging this value,
it is not reliable. See the documentation on system, wait, and waitpid.
For more reliable parent-to-child communication, you can use an
anonymous pipe ( open(PIPE,"-|") ). See the documentation on
interprocess communication ("perldoc perlipc": Safe Pipe Opens) or
Chapter 16 in the Camel Book, 3rd Edition:
#!/opt/perl/bin/perl
use strict;
use warnings;
my $return = 0;
my $child_pid = open(FROM,"-|");
if( ! defined $child_pid ) {
die("Can't fork child with open: $!");
}elsif( $child_pid ) {
# I'm the parent
print "child pid $child_pid forked\n";
$return = <FROM>;
} else {
# I'm the child
print STDOUT 10;
exit;
}
my $pid = wait;
close(FROM);
print("child $pid returned $return\n")
__OUTPUT__
child pid 4201 forked
child 4201 returned 10
------------------------------
Date: Fri, 05 Dec 2003 22:11:54 GMT
From: "Ala Qumsieh" <xxala_qumsiehxx@xxyahooxx.com>
Subject: Re: gui in perl
Message-Id: <Ko7Ab.66137$5r7.7588@newssvr25.news.prodigy.com>
"Ben Morrow" <usenet@morrow.me.uk> wrote in message
news:bqq6tc$a3k$1@wisteria.csv.warwick.ac.uk...
>
> Helgi Briem <HelgiBriem_1@hotmail.com> wrote:
> > On Fri, 5 Dec 2003 13:20:55 +0000 (UTC), Ben Morrow
> > <usenet@morrow.me.uk> wrote:
> >
> > >scriptyrich@yahoo.co.uk wrote:
> > >> As others have said, there is also the Tk stuff - certainly pretty
easy to
> > >> use and get up and running, but from my experience it produces
horrible
> > >> looking apps - but that may not be of concern to you, and I know the
Tk
> > >> guys are working on it.
> > >
> > >While this is certainly true under Unix, the win32 port of Tk produces
> > >apps which look just like any other windows app; no more and no less
> > >offensive :).
> >
> > It looks just as good (or bad) as the underlying windowing
> > system, X, Apple Mac or whatever.
>
> X, unlike win32 and MacOS, does not define a widget set. Tk uses its
> own, which is different from any other, and (IMHO) is truly hideous.
No. Tk uses the Motif look on X by default, which I agree is horrible. But,
this can be very easily changed, although not too many people know about it.
Check out this thread on comp.lang.perl.tk for more details (let's hope my
newsreader doesn't screw it up):
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&c2coff=1&frame=r
ight&th=7123d87e82847225&seekm=TSpwb.483254%246C4.125757%40pd7tw1no#link9
I have personally used Qt, Tk and Java, and while all offer pretty much the
same functionality, I like Tk the most. It certainly has the most mature
Perl port.
--Ala
--
$email =~ s/xx// if $replying_by_mail;
------------------------------
Date: 5 Dec 2003 14:23:24 -0800
From: jwillmore@myrealbox.com (James Willmore)
Subject: Re: gui in perl
Message-Id: <d61170e5.0312051423.18d810c9@posting.google.com>
James Willmore <jwillmore@remove.adelphia.net> wrote in message news:<20031205020248.4cb13e0c.jwillmore@remove.adelphia.net>...
> On Fri, 05 Dec 2003 08:48:39 +1100
> Edo <eddGallary2@hotmail.com> wrote:
>
> > I need to know if perl is ok for an interactive gui programming,
> <snip>
>
> I've used Tk scripts without issue and find they are easier to deal
> with than Java GUI's - but my opinion is biased :-)
>
> There are many Tk module that you can incorporate into your script
> that will speed development.
Just to qualify my comments ...
I meant perl/Tk :-)
Jim
(jwillmore _at_ adelphia _dot_ net)
------------------------------
Date: 5 Dec 2003 20:13:26 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Idiom for array index that I'm foreach'ing over?
Message-Id: <bqqot6$m39$1@mamenchi.zrz.TU-Berlin.DE>
Ben Morrow <usenet@morrow.me.uk> wrote in comp.lang.perl.misc:
>
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
> > BEGIN {{
> > my $i = -1;
> > sub rewind { $i = -1 }
> >
> > sub them (;\@\@\@\@\@) {
> > $i++; # increment early, so we can return a valid index
> > return if $i >= @{ $_[ 0]};
> > ( $i, map $_->[ $i], @_);
> > }
> > }}
> >
> > while ( my ( $i, $x, $y, $t) = them( @xx, @yy, @tt) ) {
> > print "i: $i, x: $x, y: $y, t: $t\n";
> > }
> >
> > Of course this is utterly fragile when applied to more than one set
> > of arrays, but for the given purpose it would do.
>
> To remove some of the fragility (untested):
Runs here.
> BEGIN {{
> my %i;
>
> sub rewind (;\@\@\@\@\@) { $i{join "", @_} = -1 }
>
> sub them (;\@\@\@\@\@) {
> my $set = join "", @_;
> $i{$set} = -1 unless defined $i{$set}; # $i{$set} //= -1;
> $i{$set}++;
> rewind(@_), return if grep { $i{$set} >= @$_ } @_;
> # I'm not sure what we want here... 'if @_ == grep ...' may be better.
So far, I have taken the first array to be the measure of all. Oh, so the
prototype should be (\@;\@...), a bug. Yours take the maximum or the
minimum length of all arrays offered, which is more thorough. There may
be other useful strategies.
> ( $i{$set}, map $_->[$i{$set}], @_);
> }
> }}
The code can be simplified a bit with re-introduction of a plain scalar $i.
In particular, we can lose the pesky initialization to -1.
my %i;
sub rewind (;\@\@\@\@\@) { delete $i{ join "", @_} }
sub them (;\@\@\@\@\@) {
my $i = $i{ join "", @_} ++; # increment for next time, use current
rewind(@_), return if grep { $i >= @$_ } @_;
( $i, map $_->[$i], @_);
}
I think only a purist would complain about the use of $i and %i here.
Anno
------------------------------
Date: 5 Dec 2003 11:56:06 -0800
From: haffi_get_beaten@hotmail.com (Mike_red)
Subject: perl against ms access
Message-Id: <9813b8ce.0312051156.7bf00eb6@posting.google.com>
Hello,
I hope someone can solve my problem here.
I am currently working/programming a website using Debian Linux + Perl
+ PHP + PostgreSQL + Apache.
Some data in the postgreSQL server must be updated from a Microsft
Access database once a week.
The Micorosft Access database is running on a Windows NT 4 platform.
How can i connect to this database with PERL.
The PERL script will be running on the Debian server.
What is the appropriate module in perl that can access a ms access
database trought the network and retrieve the data?
/ mike
------------------------------
Date: Fri, 5 Dec 2003 20:30:59 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: perl against ms access
Message-Id: <bqqpu3$p9m$2@wisteria.csv.warwick.ac.uk>
haffi_get_beaten@hotmail.com (Mike_red) wrote:
> How can i connect to this database with PERL.
Perl or perl, not PERL.
> The PERL script will be running on the Debian server.
>
> What is the appropriate module in perl that can access a ms access
> database trought the network and retrieve the data?
With MS SQL Server I've had success with DBI and DBD::Sybase. Access
may be the same...
Ben
--
It will be seen that the Erwhonians are a meek and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based
on the strictest morality. [Samuel Butler, paraphrased] ben@morrow.me.uk
------------------------------
Date: Fri, 05 Dec 2003 21:17:48 -0000
From: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: perl against ms access
Message-Id: <Xns9448A5C79DBFDdkwwashere@216.168.3.30>
Mike_red <haffi_get_beaten@hotmail.com> wrote:
> I am currently working/programming a website using Debian Linux +
> Perl + PHP + PostgreSQL + Apache.
>
> Some data in the postgreSQL server must be updated from a Microsft
> Access database once a week.
> The Micorosft Access database is running on a Windows NT 4
> platform.
>
> How can i connect to this database with PERL.
> The PERL script will be running on the Debian server.
>
> What is the appropriate module in perl that can access a ms access
> database trought the network and retrieve the data?
You might check the docs for DBD::ODBC. Here's a quote:
How do I access an MS-Access database from Linux?
I believe you can use the multi-tier drivers from
http://www.openlinksw.com, however, I have not tested this.
Also, I believe there is a commercial solution from
http://www.easysoft.com. I have not tested this.
My copy of DBD::ODBC may not be the most recent version, so check the
CPAN.
--
David Wall
------------------------------
Date: 5 Dec 2003 11:08:22 -0800
From: mdfoster44@netscape.net (Martin Foster)
Subject: read file with while and then scan lines into array
Message-Id: <6a20f90a.0312051108.2559ac0f@posting.google.com>
Hello
I'm scanning text files into a database.
My perl script looks like this:
# start loop of file to scan for data
while (defined ($_2 = <INFILE>)){
# Find cell data
if ($_2 =~ m/_cell_length_a\s+(-?([0-9]+(\.[0-9]*)?|\.[0-9]+))/){
$cell[0] = $1;
print "Found cell parameter a= ", $cell[0], " ";
print "For str_id number ", $au_id, "\n";
# Insert data
$stmt1 = "UPDATE bgb_data SET latpar_a = ? WHERE str_id = ?";
$sth = $dbh->prepare($stmt1);
$sth->execute($cell[0], $au_id);
}
# get sequences
if ($_2 =~ m/_Sequence/){
# start loop to scan in sequences
So now I've found a tag and the next few lines are number sequences
which
I want in an array.
I want to scan in those lines into until a blank line appears and then
continue scanning for further data, in the while loop.
How can I do this?
Many thanks for any help!
Cheers,
Martin
------------------------------
Date: Fri, 05 Dec 2003 19:09:59 GMT
From: Mike Flannigan <mikeflan@earthlink.net>
Subject: Re: Regex Question
Message-Id: <3FD0D8C9.4A94B985@earthlink.net>
Tad McClellan wrote:
> Please:
>
> Speak Perl rather than English, when possible
> Perl is much more precise than natural language. Saying it in Perl
> instead will avoid misunderstanding your question or problem.
Still working on that, and not doing too good at it yet.
> Why should the 1st one end with a comma while the 2nd one doesn't?
Actually, it was all one line and I forgot to add a comma
at the end.
> You get a list of all memories when you do a m//g in list context.
>
> ----------------------------------------------------------
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @data = (
> 'condition=" OR (subject,contains,can we meet) OR '
> . '(subject,contains,size does matter) OR '
> . '(subject,contains,aging pill) OR '
> . '(subject,contains,gain inch) OR '
> . '(subject,contains,failure announc)"',
>
> 'condition=" OR (subject,contains,Email Storage) OR '
> . '(from,contains,Net Delivery) OR '
> . '(from,contains,Network) OR '
> . '(subject,contains,bug ) OR '
> . '(body,contains,Undeliverable mail to )"'
> );
>
> foreach ( @data ) {
> my @parts = /\(subject,[^,]+,([^)]+)/g; # m//g in a list context
> print join(', ', @parts), "\n";
> }
>
Thanks Tad. My first question was going to be why the ()
around [^)]+
but I see why now that I tried it out.
Very interesting. It's not going to be super easy for
me to remember this, but I suspect I will after studying
it all this time. I think I've go to perldoc and see where
they explain this.
And if you have a 2nd or 3rd set of parens, it just
keeps stuffing into the array. Cool.
Mike
------------------------------
Date: Fri, 05 Dec 2003 19:28:55 -0000
From: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: SGML/HTML syntax trivia (was Re: any idea how to optimize this regex?)
Message-Id: <Xns94489351D25B1dkwwashere@216.168.3.30>
Alan J. Flavell <flavell@ph.gla.ac.uk> wrote:
> On Fri, 5 Dec 2003, Tad McClellan wrote:
>
>> Note also that <!> is a "comment declaration" as well.
>
> And, currently, a sure-fire indicator of spam in HTML-formatted
> emails - they evidently intend it to disrupt content scanners. It
> won't last, of course - as soon as they realise that we're rating
> it for rejection, rather than letting ourselves be fooled by the
> obfuscation.
With that in mind, here's an infinite loop:
while ($spammer < $pond_scum) {
$spammer++;
}
------------------------------
Date: 5 Dec 2003 13:31:47 -0800
From: charley@pulsenet.com (Chris Charley)
Subject: Re: sorting file names
Message-Id: <4f7ed6d.0312051331.28cd9abb@posting.google.com>
mixo <mixo@beth.uniforum.org.za> wrote in message news:<bqpdjd$86k$1@ctb-nnrp2.saix.net>...
> I have file name that have a prefix "t-", which
> is followed by a number (integer), like
> t-1
> t-2
> t-3
> t-10
> t-21
> and so on.
>
> How can I sort this in numeric order?
You could use the Schwartzian Transform. It is easier to follow if you
read from the end of the expression (because this is the order of
execution).
Note that each file name, and the digits to sort on are placed in an
anonymous array in the bottom map which is then passed to the sort
line, and so forth.
Chris
#!/usr/bin/perl
use strict;
use warnings;
my @files = qw / t-1.txt t-4.txt t-10.txt t-14.txt t-11.txt /;
my @sorted = map {$_->[0]}
sort {$a->[1] <=> $b->[1]}
map { [ $_, /-(\d+)/ ] }
@files;
print "@sorted\n";
__END__
output: t-1.txt t-4.txt t-10.txt t-11.txt t-14.txt
------------------------------
Date: Fri, 05 Dec 2003 21:00:44 +0200
From: Tassos <chatasos@yahoo.com>
To: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: subroutine parameter with regex
Message-Id: <3FD0D5DC.1000101@yahoo.com>
Ben Morrow wrote:
> Tassos <chatasos@yahoo.com> wrote:
>
>>$var1 = "Se2:5";
>>if ( $var1 =~ /^Se\d/ ) {
>> $var1 =~ s/Se/Serial/;
>> &routine1($var1);
>
>
> Don't call subs with & unless you know why you do it.
>
I'm using it because the sub is written after this part of code. Is this wrong?
>
>> $var1 =~ s/Serial/Se/;
>>}
>>
>>Is there a way i can call routine1 using the previous $var1 regex as
>>its parameter, so i don't have to double regex?
>
>
> Err... I understand your Perl better than your English. I think what
> you are asking for is
>
> if( $var1 =~ /^Se\d/ ) {
> (my $var2 = $var1) =~ s/Se/Serial/;
> routine1($var2);
> }
>
That's what i'm trying to do, but i was hoping of a better way...ex. to include the regex
into the () of the sub call.
> , but I'm not entirely sure.
>
> Ben
>
------------------------------
Date: Fri, 5 Dec 2003 19:28:10 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: subroutine parameter with regex
Message-Id: <bqqm8a$mpn$2@wisteria.csv.warwick.ac.uk>
Tassos <chatasos@yahoo.com> wrote:
> Ben Morrow wrote:
> > Don't call subs with & unless you know why you do it.
>
> I'm using it because the sub is written after this part of code. Is
> this wrong?
Yes. You don't need it for this.
If you predeclare your subs with 'use subs' you don't need the () either.
> > if( $var1 =~ /^Se\d/ ) {
> > (my $var2 = $var1) =~ s/Se/Serial/;
> > routine1($var2);
> > }
>
> That's what i'm trying to do, but i was hoping of a better
> way...ex. to include the regex into the () of the sub call.
Well, I suppose you could do
routine1(grep s/Se/Serial/, (my $var2 = $var1));
but that's a little obscure.
Ben
--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces mollit animos, tristesque mentes erigit. | ben@morrow.me.uk
Musica vel ipsas arbores et horridas movet feras. |
------------------------------
Date: Fri, 5 Dec 2003 14:16:09 -0500
From: "Glenn J. Rowe" <undisclosed_sender@undisclosed_domain.com>
Subject: Syntax problem
Message-Id: <bqqlho$jdp19@nrn2.NRCan.gc.ca>
I need to read in a variable and make sure it is in the exact format of
YYYY_MMM_DD_hh:mm:ss
Example... 2003_DEC_05_14:05:26
I tried the following code but it doesn't work...
if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2})(:)(\d{2})(:)(\d{2})/)
{
#Continue with code
}
else
{
#Display an error
}
I am a self taught programmer so any help you can give me would be
appreciated.
Thanks,
Glenn
------------------------------
Date: Fri, 05 Dec 2003 14:01:52 -0600
From: Robert Dover <dover@nortelnetworks.com>
Subject: Re: Syntax problem
Message-Id: <bqqo4u$njk$1@zcars0v6.ca.nortel.com>
Glenn J. Rowe wrote:
> I need to read in a variable and make sure it is in the exact format of
> YYYY_MMM_DD_hh:mm:ss
> Example... 2003_DEC_05_14:05:26
>
>
> I tried the following code but it doesn't work...
> if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2})(:)(\d{2})(:)(\d{2})/)
> {
> #Continue with code
> }
> else
> {
> #Display an error
> }
I didn't rigorously test it, but it seems to work for me. What problems are you
seeing?
-Bob
------------------------------
Date: Fri, 05 Dec 2003 14:08:31 -0600
From: Robert Dover <dover@nortelnetworks.com>
Subject: Re: Syntax problem
Message-Id: <bqqohe$ns9$1@zcars0v6.ca.nortel.com>
Robert Dover wrote:
> Glenn J. Rowe wrote:
>
> I didn't rigorously test it, but it seems to work for me. What problems
> are you seeing?
Sorry to follow-up on my own post, but here's what I did:
#!/usr/bin/perl -w
$variable = "2003_DEC_05_14:05:26";
if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2})(:)(\d{2})(:)(\d{2})/)
{print "ok\n";}
else {print "nope\n";}
Some points:
-Your regex will also match "abc 1234_SOP_13_99:99:99 something else".
-If you're getting your $variable from STDIN, don't forget to chomp it.
HTH,
BD
------------------------------
Date: Fri, 5 Dec 2003 20:29:14 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Syntax problem
Message-Id: <bqqpqq$p9m$1@wisteria.csv.warwick.ac.uk>
"Glenn J. Rowe" <undisclosed_sender@undisclosed_domain.com> wrote:
> I need to read in a variable and make sure it is in the exact format of
> YYYY_MMM_DD_hh:mm:ss
> Example... 2003_DEC_05_14:05:26
>
> I tried the following code but it doesn't work...
> if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2})(:)(\d{2})(:)(\d{2})/)
my $month = join "|", qw/JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC/;
if( $variable =~ /^ (\d{4}) _ ($month) _ (\d{2}) _
(\d{2}) : (\d{2}) : (\d{2}) $/xo and
$1 > 1900 and $1 < 2500 and $3 > 0 and $3 < 32 and
$4 >= 0 and $4 < 24 and $5 >= 0 and $5 < 60 and $6 >= 0 and $6 < 60 ) {
#pass
} else {
#fail
}
Or I would use one of the date-parsing modules from CPAN.
Ben
--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
ben@morrow.me.uk <=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=> (Kate Rusby)
------------------------------
Date: Fri, 5 Dec 2003 20:28:42 +0000 (UTC)
From: "John J. Trammell" <trammell+usenet@hypersloth.invalid>
Subject: Re: Syntax problem
Message-Id: <slrnbt1qjq.9nv.trammell+usenet@hypersloth.el-swifto.com.invalid>
On Fri, 5 Dec 2003 14:16:09 -0500, Glenn J. Rowe wrote:
> I need to read in a variable and make sure it is in the exact format
> of YYYY_MMM_DD_hh:mm:ss
> Example... 2003_DEC_05_14:05:26
>
> I tried the following code but it doesn't work...
[aside: "doesn't work" is usually not the best choice of
phrase to describe a programming problem...]
> if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2})(:)(\d{2})(:)(\d{2})/)
Apart from removing the unnecessary parens, I would skip the {N}
and add ^, $ anchors for clarity (assuming that doesn't cause
other problems), and maybe tweak the "month" field:
/^\d\d\d\d_\w\w\w_\d\d_\d\d:\d\d:\d\d$/ # untested
Of course this doesn't verify that you have a valid date at all.
But I doubt this would fix your "doesn't work" problem. Code like:
warn "\$variable is '$variable'";
if ($variable =~ /whatever/) { ... }
can help determine exactly what text you're using in the match.
------------------------------
Date: Fri, 5 Dec 2003 15:50:05 -0500
From: "Glenn J. Rowe" <undisclosed_sender@undisclosed_domain.com>
Subject: Re: Syntax problem
Message-Id: <bqqr21$p2r22@nrn2.NRCan.gc.ca>
Hmmm - I "just" noticed something...
Up till now I was testing the code by changing the first part to either 203
or 20003 instead of 2003 and the code didn't work.
I just tried messing up other parts and it worked fine. (ie. NOV changed to
NV)
I changed the code to
if($variable=~m/2(\d{3})_(\D{3})_(\d{2})_(\d{2}):(\d{2}):(\d{2})/) and all
is fine.
Not sure why it only works if I specify the first character but hey it
works.
Thanks for your help,
Glenn
"Robert Dover" <dover@nortelnetworks.com> wrote in message
news:bqqohe$ns9$1@zcars0v6.ca.nortel.com...
> Robert Dover wrote:
>
> > Glenn J. Rowe wrote:
> >
> > I didn't rigorously test it, but it seems to work for me. What problems
> > are you seeing?
>
> Sorry to follow-up on my own post, but here's what I did:
>
> #!/usr/bin/perl -w
> $variable = "2003_DEC_05_14:05:26";
>
if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2})(:)(\d{2})(:)(\d{2})/)
> {print "ok\n";}
> else {print "nope\n";}
>
> Some points:
>
> -Your regex will also match "abc 1234_SOP_13_99:99:99 something else".
> -If you're getting your $variable from STDIN, don't forget to chomp it.
>
> HTH,
> BD
>
------------------------------
Date: Fri, 05 Dec 2003 14:06:56 -0500
From: Tim Heaney <theaney@cablespeed.com>
Subject: Re: Using a hash for the post data in LWP::useragent?
Message-Id: <871xrj13u7.fsf@mrbun.watterson>
awilhite@cableone.net (Andrew) writes:
>
> I would like to to be able to use the same subroutine for multiple
> post data hashes by calling
>
> my %posthash = [ username => 'bob',host => 'localhost', other => '1',
> other2 => '2', other3 => '3']
>
>
> $ua->request(POST $url, \%posthash); # something like this
>
>
>
> anyone have any ideas on how to do this?
Something like this ought to do it
my %posthash = ( username => 'bob',host => 'localhost', other => '1',
other2 => '2', other3 => '3');
$ua->request(POST $url, [ %posthash ] );
I hope this helps,
Tim
------------------------------
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 5900
***************************************