[21986] in Perl-Users-Digest
Perl-Users Digest, Issue: 4208 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Dec 3 03:05:48 2002
Date: Tue, 3 Dec 2002 00: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 Dec 2002 Volume: 10 Number: 4208
Today's topics:
Re: A regex 'whoa' <mgjv@tradingpost.com.au>
Re: A regex 'whoa' <wksmith@optonline.net>
Re: A regex 'whoa' <vilmos@vilmos.org>
Re: A regex 'whoa' (Jay Tilton)
Re: A regex 'whoa' <mgjv@tradingpost.com.au>
Re: A regex 'whoa' <wsegrave@mindspring.com>
Re: A regex 'whoa' <wsegrave@mindspring.com>
Re: advanced html form question <jurgenex@hotmail.com>
Re: advanced html form question <wsegrave@mindspring.com>
Re: advanced html form question <tassilo.parseval@post.rwth-aachen.de>
Re: Closing an unsaved modded Excel spreadsheet? <bwalton@rochester.rr.com>
Re: How to split to scalar? <krahnj@acm.org>
Re: How to split to scalar? btam01@ccsf.edu
Re: mysql <darkage@freeshellzzzz.org>
perl 5.8 problem (venkat)
reference to a tied hash <lois@hotmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 03 Dec 2002 02:38:20 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: A regex 'whoa'
Message-Id: <slrnauo6df.4ru.mgjv@verbruggen.comdyn.com.au>
On Tue, 03 Dec 2002 01:51:43 GMT,
Bryan <bryan@akanta.com> wrote:
> I saw this in a colleagues code today (I SWEAR it's not mine!):
>
> $part1 =~
> /^(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*\d+\s*\t\s*(\d+)\s*\t\s*\d+\s*\t\s*([-|\+])\s*\t*$/;
>
> Comments?
Yes.
It's ugly. It has no documentation. At the least for a regex of this
length, the /x option should be used, which also allows you to embed
documentation explaining what is being matched, and why.
The \s class already includes \t. Is he trying to express that a tab
is mandatory, but it could be enclosed by other whitespace, including
more tabs? With the use of /x, this could be documented. Otherwise, a
\s+ would probably look better.
The final \t* will never match anything (because of the preceding
\s*)
my @numbers = $part1 =~
m{ ^
(\d+) # some documentation
\s*\t\s* # explain why this looks like this.
(\d+)
\s*\t\s*
(\d+)
\s*\t\s*
(\d+)
\s*\t\s*
(\d+)
\s*\t\s*
\d+ # we're not capturing this, because...
\s*\t\s*
(\d+)
\s*\t\s*
\d+
\s*\t\s*
([-|\+]) # more documentation
\s* # I'd probably document this as well
$
}x;
I suspect that
my @numbers = (split " ", $part1)[0..4,6,8]; # [1]
would do just as well, and it is much more readable. Or if you insist
on doing a check on the number of fields,
my @numbers = split " ", $part1;
die "Incorrect format in '$_'\n" unless @numbers == 9;
@numbers = @numbers[0..4,6,8];
or insist on a tab:
my @numbers = split /\s*\t\s*/, $part1;
die "Incorrect format in '$_'\n" unless @numbers == 9;
@numbers = @numbers[0..4,6,8];
The result of this last one is slightly different from what you
posted, if there are trailing spaces without a tab, and if these
indeed need to be stripped off. That can be done either before or
after the split, depending on whether you want to keep the original
string intact.
my @numbers = split /\s*\t\s*/, $part1;
die "Incorrect format in '$_'\n" unless @numbers == 9;
$numbers[8] =~ s/\s+$//;
@numbers = @numbers[0..4,6,8];
Which still is more readable, to me.
Of course, the assignments could be done to a list of variables
directly. It's just that your example didn't make it clear whether the
rest of the code assigned these things somewhere, or whether it just
went off to use $1 etc..
Martien
[1] " " as a split pattern is special. See perlfunc, entry for split.
--
|
Martien Verbruggen | I took an IQ test and the results were
Trading Post Australia | negative.
|
------------------------------
Date: Tue, 03 Dec 2002 02:49:09 GMT
From: "Bill Smith" <wksmith@optonline.net>
Subject: Re: A regex 'whoa'
Message-Id: <FYUG9.86202$Kk2.4950@news4.srv.hcvlny.cv.net>
"Bryan" <bryan@akanta.com> wrote in message
news:3DEC0E2F.7050105@akanta.com...
> I saw this in a colleagues code today (I SWEAR it's not mine!):
>
> $part1 =~
>
/^(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*\d+\s
*\t\s*(\d+)\s*\t\s*\d+\s*\t\s*([-|\+])\s*\t*$/;
>
> Comments?
>
I find it useful to form an overview first. It may not be totally
correct, but it guides my thinking. In this view, your regex matches a
string consisting only of eight integers and a sign, separated by tabs.
The first five integers, the seventh integer, and the sign are captured
in $1 through $7.
Now lets look at the first field and its separator in detail. The
string starts with one or more digits (which are captured into $1).
Next comes zero or more white space characters followed by a tab
followed by zero or more white space characters.
This pattern is repeated for next four fields. The pattern is repeated
again, but without capturing, for the sixth field. The original pattern
is repeated for the seventh field. The non-capturing pattern is repeated
for the eighth field. This is followed by a sign (which is captured
into $7). The remainder of the string consists of zero or more white
space characters followed by zero or more tab characters.
This expression is rather long, but very clear. I cannot comment on
whether it is appropriate without knowing more about the problem it is
designed to solve.
Bill
------------------------------
Date: 02 Dec 2002 20:13:20 -0800
From: Vilmos Soti <vilmos@vilmos.org>
Subject: Re: A regex 'whoa'
Message-Id: <87d6ojagf3.fsf@my.vilmos.lan>
Martien Verbruggen <mgjv@tradingpost.com.au> writes:
> On Tue, 03 Dec 2002 01:51:43 GMT,
> Bryan <bryan@akanta.com> wrote:
> > I saw this in a colleagues code today (I SWEAR it's not mine!):
> >
> > $part1 =~
> > /^(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*\d+\s*\t\s*(\d+)\s*\t\s*\d+\s*\t\s*([-|\+])\s*\t*$/;
Will this actually match anything? The problem is that "\s*\t\s*"
will fail since the first \s* will consume the tab. Thus the \t
will not match anything, game over.
Vilmos
------------------------------
Date: Tue, 03 Dec 2002 04:45:00 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: A regex 'whoa'
Message-Id: <3dec3610.362342592@news.erols.com>
On 02 Dec 2002 20:13:20 -0800, Vilmos Soti <vilmos@vilmos.org> wrote:
: Martien Verbruggen <mgjv@tradingpost.com.au> writes:
:
: > On Tue, 03 Dec 2002 01:51:43 GMT,
: > Bryan <bryan@akanta.com> wrote:
: > > I saw this in a colleagues code today (I SWEAR it's not mine!):
: > >
: > > $part1 =~
: > > /^(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*\d+\s*\t\s*(\d+)\s*\t\s*\d+\s*\t\s*([-|\+])\s*\t*$/;
:
: Will this actually match anything? The problem is that "\s*\t\s*"
: will fail since the first \s* will consume the tab. Thus the \t
: will not match anything, game over.
Did you test that hypothesis?
The matching engine is smarter than you think it is.
------------------------------
Date: Tue, 03 Dec 2002 05:18:26 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: A regex 'whoa'
Message-Id: <slrnauofog.222.mgjv@verbruggen.comdyn.com.au>
On 02 Dec 2002 20:13:20 -0800,
Vilmos Soti <vilmos@vilmos.org> wrote:
> Martien Verbruggen <mgjv@tradingpost.com.au> writes:
>
>> On Tue, 03 Dec 2002 01:51:43 GMT,
>> Bryan <bryan@akanta.com> wrote:
>> > I saw this in a colleagues code today (I SWEAR it's not mine!):
>> >
>> > $part1 =~
>> > /^(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*\d+\s*\t\s*(\d+)\s*\t\s*\d+\s*\t\s*([-|\+])\s*\t*$/;
>
> Will this actually match anything? The problem is that "\s*\t\s*"
> will fail since the first \s* will consume the tab. Thus the \t
> will not match anything, game over.
Didn't you read the rest of my message, which is the one you are
replying to. \s*\t\s* will require a tab to be matched. If there is no
tab, the whole match will fail. The \s* will only eat as much
whitespace (greedily) as can be eaten before a tab matches.
$ perl -wMstrict
my $test1 = "1234 \t 2345";
my $test2 = "1234 2345";
print "\$test1:($1)($2)\n" if $test1 =~ /^(\d+)\s*\t\s*(\d+)$/;
print "\$test2:($1)($2)\n" if $test2 =~ /^(\d+)\s*\t\s*(\d+)$/;
__END__
$test1:(1234)(2345)
Martien
--
|
Martien Verbruggen | My friend has a baby. I'm writing down all
Trading Post Australia | the noises the baby makes so later I can ask
| him what he meant - Steven Wright
------------------------------
Date: Mon, 2 Dec 2002 23:39:32 -0600
From: "William Alexander Segraves" <wsegrave@mindspring.com>
Subject: Re: A regex 'whoa'
Message-Id: <ashg6e$6c5$2@slb2.atl.mindspring.net>
"Bryan" <bryan@akanta.com> wrote in message
news:3DEC0E2F.7050105@akanta.com...
> I saw this in a colleagues code today (I SWEAR it's not mine!):
>
> $part1 =~
>
/^(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*\d+\s*\t\
s*(\d+)\s*\t\s*\d+\s*\t\s*([-|\+])\s*\t*$/;
>
> Comments?
>
Well, it appears to do nothing to $part1.
What it does do is parse:
eight sets of
one or more digits,
followed by zero or more spaces, a tab, and zero or more spaces
followed by either a + or - sign
followed by zero or more spaces and zero or more tabs
remembering the sets of digits, except for the sixth and eight set, and the
+/- in $1 through $7
Its intended use is not stated, although it could be used to remove
spaces/tabs from as tab-sep set of values, e.g.,
#!perl -w
use strict;
my $part1 = '1 2 3 4 5 6 7 8 - '; # digits 1-8 and -, tab-sep
$part1 =~
/^(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*(\d+)\s*\t\s*\d+\s*\t\
s*(\d+)\s*\t\s*\d+\s*\t\s*([-|\+])\s*\t*$/;
print $1,$2,$3,$4,$5,$6,$7;
produces the result:
123457-
showing the sixth and eighth digits dropped (not remembered).
Bill Segraves
------------------------------
Date: Tue, 3 Dec 2002 00:06:55 -0600
From: "William Alexander Segraves" <wsegrave@mindspring.com>
Subject: Re: A regex 'whoa'
Message-Id: <ashhou$3eq$1@slb6.atl.mindspring.net>
"William Alexander Segraves" <wsegrave@mindspring.com> wrote in message
news:ashg6e$6c5$2@slb2.atl.mindspring.net...
<snip>
Oops! Slight correction needed, I think.
> Well, it appears to do nothing to $part1
directly, as it doesn't change $part1.
>
> What it does do is parse
$part1, looking for
>
> eight sets of
<snip>
Bill Segraves
------------------------------
Date: Tue, 03 Dec 2002 04:02:20 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: advanced html form question
Message-Id: <g1WG9.26844$ic6.23351@nwrddc01.gnilink.net>
Fred wrote:
> "Jürgen Exner" <jurgenex@hotmail.com> wrote in message
> news:J6MG9.5281$361.1956@nwrddc04.gnilink.net...
>> Fred wrote:
[...]
>>> What is your favorite color?
[...]
>> print "What is your favourite colour?";
>> my $colour = <STDIN>;
>>
>>> The answer the respondent gave to the first question was yellow, so
>>> the next question should read:
>>>
>>> Why is your favorite color yellow?
[...]
>> print "Why is your favourite colour $colour?";
>> my $colourreason = <STDIN>;
>
> In the first question from my example, I am going to force the
> respondent to answer exactly 3 checkboxes. What would i use in the
> result page to ask three follow up questions based on the first three
> answers?
Perl does not have check boxes. Perl does not have pages. What beast of GUI
are you talking about? Perl/Tk maybe? Then please consult the documentation
for Perl/Tk about how to display check boxes and how to display a "result"
page.
jue
------------------------------
Date: Mon, 2 Dec 2002 22:40:29 -0600
From: "William Alexander Segraves" <wsegrave@mindspring.com>
Subject: Re: advanced html form question
Message-Id: <ashg6d$6c5$1@slb2.atl.mindspring.net>
"Jürgen Exner" <jurgenex@hotmail.com> wrote in message
news:g1WG9.26844$ic6.23351@nwrddc01.gnilink.net...
<snip>
Fred wrote:
> > In the first question from my example, I am going to force the
> > respondent to answer exactly 3 checkboxes. What would i use in the
> > result page to ask three follow up questions based on the first three
> > answers?
>
jue wrote:
> Perl does not have check boxes. Perl does not have pages.
True. OTOH, the OP should take note that, with CGI.pm, a Perl script can
employ the shortcuts 'radio_group' and 'checkbox_group' to generate the
corresponding HTML code. These groups were the ones that seemed most
appropriate, based on the OP's description of his problem.
> What beast of GUI
> are you talking about? Perl/Tk maybe?
No. Actually, the OP asked about php and mySQL in his original post, IIRC.
Naturally, we "listened" Perl. ;-)
<snip> how to display a "result"
> page.
The example I posted earlier shows how to ask a follow-up question based on
the answer to the first question. What the user sees in the browser window
is a "result" page, IMO.
Cheers.
Bill Segraves
------------------------------
Date: 3 Dec 2002 07:58:12 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: advanced html form question
Message-Id: <asho6k$t0e$1@nets3.rz.RWTH-Aachen.DE>
Also sprach William Alexander Segraves:
> "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de> wrote in
> message news:asgq6c$aum$1@nets3.rz.RWTH-Aachen.DE...
[ CGI.pm's HTML generation ]
>> I am feeling far more comfortable with large here-doc sections for
>> spitting out HTML. HTML-wise this could be called WYSIWYG.
>
> I see. And you have no trouble keeping up with all the HTML tags?
Oh, I do actually. I am doing CGI-things too seldom so my HTML is always
a little rusty when I am up to it. But I always have a few old reference
HTML files around to look up the syntax of a table or so. And
fortunately millions of HTML references can be found on the internet so
I never quite felt the urge to learn that by heart.
It's no different with CGI.pm though so I usually have 'perldoc CGI'
running on some other console when using it.
>>Along with
>> Perl's curious @{[ ... ]} interpolation of arbitrary Perl code within
>> strings that makes up for a very basic yet powerful templating
>> mechanism.
>
> I just felt the OP would be better served if he learned to use CGI.pm _and_
> the shortcuts it includes, as long as he's using it.
Consistency isn't the worst for a beginner. It probably requires some
experience to come up with one's own preferences and style.
Tassilo
--
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;
------------------------------
Date: Tue, 03 Dec 2002 04:40:33 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: Closing an unsaved modded Excel spreadsheet?
Message-Id: <3DEC3595.30400@rochester.rr.com>
Richard S Beckett wrote:
...
> I'm using Win32::OLE to load and populate an Excel spreadsheet. The tests
> involved take a long time, and it's possible for the user to stop the tests
> before they complete.
>
> This leaves me with;
> my template modified, which I don't want to overwrite;
> a partly populated spreadsheet, which I don't want to save;
> a huge spreadsheet opened, which I don't want to save and delete, as it
> takes a very long time to save it.
>
> So, I want to be able to close the spreadsheet, without saving it.
> Unfortunately, I get a prompt from Excel, asying that the spreadsheet has
> changed, and do I want to save it. I don't want this prompt to appear.
>
> I have searched the web, and Google, and have found many "solutions" to this
> problem. Unfortunately none of them actually work!
>
> I have tried:
>
> $excel -> {DisplayAlerts} = 0;
> and
> undef $_book;
> and
> $_book -> {Saved} = 1;
> undef $_book;
> and
> $book -> Close({SaveChanges => DoNotSaveChanges});
> and
> $book -> Close (SaveChanges => "0");
> and
> $book = $excel -> Workbooks -> Close ();
> undef $excel;
>
> Does anyone know a way to do this that actually works?
...
> R.
[untested]:
Try setting the Saved property of your workbook to true (-1 in VBA),
and then doing a close. That makes Excel think the workbook was
previously saved and is up-to-date. Maybe something like [untested]:
$book->{Saved}=-1;
$excel->Close;
And BTW, your question is a VBA question, not a Perl question, so it
is off-topic in this newsgroup.
--
Bob Walton
------------------------------
Date: Tue, 03 Dec 2002 02:12:25 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: How to split to scalar?
Message-Id: <3DEC12D3.FA153AFD@acm.org>
Bob wrote:
>
> How do I split a string, and instead of dumping the results into an
> array, put a specific scalar from the array into another scalar? In one
> step?
>
> So I have a string something like this:
> my $s = "My string is 4";
>
> And I want to do something like this:
> my $num = $(split / /, $s)[3];
>
> so that $num is 4;
If you always want the fourth element:
my $num = ( split ' ', $s )[3];
If you always want the last element:
my $num = ( split ' ', $s )[-1];
If you always want the numeric element:
my ( $num ) = $s =~ /(\d+)/;
John
--
use Perl;
program
fulfillment
------------------------------
Date: Mon, 2 Dec 2002 21:31:28 -0800
From: btam01@ccsf.edu
Subject: Re: How to split to scalar?
Message-Id: <Pine.HPX.4.44.0212022130250.26542-100000@hills.ccsf.cc.ca.us>
On Tue, 3 Dec 2002, Bob wrote:
> An easy question:
>
> How do I split a string, and instead of dumping the results into an
> array, put a specific scalar from the array into another scalar? In one
> step?
>
> So I have a string something like this:
> my $s = "My string is 4";
>
> And I want to do something like this:
> my $num = $(split / /, $s)[3];
>
> so that $num is 4;
>
> Sadly, this doesn't work. I know this is simple but I'm missing the
> syntax. Maybe this must be a two step operation?
>
> Thanks,
> Bob
Not very efficient, but it works.
#! /usr/local/bin/perl
use strict "vars";
my $s = "My string is way too short";
my @items = split ( / /, $s );
my $last = pop ( @items );
print "\nline : ", $s;
print "\nlast value : ", $last, "\n\n";
-Bill
------------------------------
Date: Tue, 3 Dec 2002 13:05:19 +1100
From: "^darkage" <darkage@freeshellzzzz.org>
Subject: Re: mysql
Message-Id: <ash3hp$ifh$1@perki.connect.com.au>
I seem to be missing 1500 records from a 5600 record insert. using the
$mysql->query(qq!
INSERT INTO logs (field1, field2, field3, field4, field5, field6) VALUES
($fields[0],$fields[1],$fields[2],$fields[3],$fields[4],$fields[5])!);
method.
hmm in small amounts 50 or so records its ok.
"^darkage" <darkage@freeshellzzzz.org> wrote in message
news:asgrjk$c5l$1@perki.connect.com.au...
> jesus! it works!!!! (: but not sue if its missing out some transactions.
> Is this a reliable method?
>
> "Avantage" <mgiga@logava.com> wrote in message
> news:l%NG9.783$rv5.355936@news20.bellglobal.com...
> > > ie. $mysql->query(q{
> > > INSERT INTO logs (field1, field2, field3, field4, field5, field6)
> VALUES
> > > ($fields[0],$fields[1],$fields[2],$fields[3],$fields[4],$fields[5])
> > > });
> >
> > Why not simply use (notice the doubled "q" followed by "!") :
> >
> > $mysql->query(qq!
> > INSERT INTO logs (field1, field2, field3, field4, field5, field6)
> VALUES
> > (@fields)
> > !);
> >
> > have a nice one,
> >
> > Michel
> >
> >
>
>
------------------------------
Date: 2 Dec 2002 20:17:33 -0800
From: vvenkatagr@yahoo.com (venkat)
Subject: perl 5.8 problem
Message-Id: <fe69c8ea.0212022017.2da79b53@posting.google.com>
Hi,
I have downloaded a simple http proxy from the net and using it with
perl 5.8. When my browser sends a request to the proxy, the proxy
gives the following error and doesn't process the request.
--
socket: Invalid argument at proxy.pl line 194, <CHILD> line 1.
--
It used to work fine with pel 5.6. Can someone tell me what the
problem is and suggest me a solution. The proxy code is at
http://crypto.stanford.edu/~dabo/proxy/proxy.pl
TIA,
Venkat
------------------------------
Date: Tue, 03 Dec 2002 08:04:57 GMT
From: "Lois" <lois@hotmail.com>
Subject: reference to a tied hash
Message-Id: <IAZG9.210999$NH2.14745@sccrnsc01>
Hi all,
Is $r a reference to a tied hash?
my $r = \tie %event, "DB_File", "events.db", O_CREAT | O_RDWR or die "$!\n";
and when this is passed to a sub, how can %event be used.
sub testing_sub($r)
sub testing_sub
{
$ref = $_[0];
## How to call %event hash???
}
Thanks,
lois
------------------------------
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 4208
***************************************