[22229] in Perl-Users-Digest
Perl-Users Digest, Issue: 4450 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jan 22 18:15:41 2003
Date: Wed, 22 Jan 2003 15:15:17 -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 Wed, 22 Jan 2003 Volume: 10 Number: 4450
Today's topics:
Re: Send a file w/ CGI <remccart@uiuc.edu>
Re: Send a file w/ CGI (Tad McClellan)
Re: Send a file w/ CGI <total_jnk@hotmail.com>
Re: Send a file w/ CGI <remccart@uiuc.edu>
Re: Send a file w/ CGI <bongie@gmx.net>
Re: slow grep <mpapec@yahoo.com>
Re: slow grep <skuo@mtwhitney.nsc.com>
Re: slow grep (Anno Siegel)
Re: split with pattern matching question (Christopher Hamel)
Re: To extract a specific portion of a text file (Tad McClellan)
Re: To extract a specific portion of a text file (David James)
Re: To extract a specific portion of a text file (Stefan Adams)
Re: Win32::API convert code from C (Mike Solomon)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 22 Jan 2003 11:06:38 -0600
From: Ryan <remccart@uiuc.edu>
Subject: Re: Send a file w/ CGI
Message-Id: <s9AX9.13704$Vf3.147042@vixen.cso.uiuc.edu>
Tad McClellan wrote:
> Harald was not talking about error checking there.
> He was talking about taint checking.
> Are you using taint checking (-T) in your CGI program?
Yes. Do you have any insight to my question?
------------------------------
Date: Wed, 22 Jan 2003 11:45:41 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Send a file w/ CGI
Message-Id: <slrnb2tm65.7ps.tadmc@magna.augustmail.com>
Ryan <remccart@uiuc.edu> wrote:
> Tad McClellan wrote:
>> Harald was not talking about error checking there.
>> He was talking about taint checking.
>> Are you using taint checking (-T) in your CGI program?
>
> Yes.
You are not doing it right then.
Your code snippet does not untaint any of the tainted data.
> Do you have any insight to my question?
No.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 22 Jan 2003 14:22:36 -0500
From: Ben <total_jnk@hotmail.com>
Subject: Re: Send a file w/ CGI
Message-Id: <b0mr1s$rfpc4$1@ID-121117.news.dfncis.de>
Harald H.-J. Bongartz wrote:
> Let Perl tell you the reason why the open fails:
> .. or die "cannot open $filepath: $!";
die in a CGI script? Doesn't sound very user friendly.
regards,
Ben
------------------------------
Date: Wed, 22 Jan 2003 16:54:57 -0600
From: Ryan <remccart@uiuc.edu>
Subject: Re: Send a file w/ CGI
Message-Id: <yfFX9.13850$Vf3.148812@vixen.cso.uiuc.edu>
Tad McClellan wrote:
> You are not doing it right then.
> Your code snippet does not untaint any of the tainted data.
From previous post:
>>> I omitted the error checking for simplicity.
>>Do you have any insight to my question?
> No.
Then thanks for nothin'
------------------------------
Date: Thu, 23 Jan 2003 00:02:08 +0100
From: "Harald H.-J. Bongartz" <bongie@gmx.net>
Subject: Re: Send a file w/ CGI
Message-Id: <2907141.7tuq1SSEWJ@nyoga.dubu.de>
Ben wrote:
> Harald H.-J. Bongartz wrote:
>> Let Perl tell you the reason why the open fails:
>> .. or die "cannot open $filepath: $!";
>
> die in a CGI script? Doesn't sound very user friendly.
The OP need not take this die() verbatim. He can do whatever he wants
to present an error message to the user, unless he doesn't continue to
work with the filehandle, and this is all the die() should express.
I don't know which kind of error handling the OP prefers. When using
CGI::Carp, he might even stay with the die(), but this is rarely a good
choice for production use.
Ciao,
Harald
--
Harald H.-J. Bongartz <bongie@gmx.net>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Did you ever walk in a room and forget why you walked in? I
think that's how dogs spend their lives. -- Sue Murphy
------------------------------
Date: Wed, 22 Jan 2003 19:20:15 +0100
From: Matija Papec <mpapec@yahoo.com>
Subject: Re: slow grep
Message-Id: <r6ot2vg2h0ckf7m1aakd32ujrq4j4u68o8@4ax.com>
X-Ftn-To: Steven Kuo
Steven Kuo <skuo@mtwhitney.nsc.com> wrote:
>Return value of the two subroutines aren't the same:
>
>my @you_say_potato = sub1();
>my @i_say_potato = sub3();
>print "@you_say_potato : @i_say_potato";
>
>Add 'return @rest;' at the end of sub1 and try again.
Yes, I did it and grep was still 10% slower. Interesting is that after I put
zero at the end of both subs, foreach was again faster by whole 20%
sub sub1 {
my @rest=();
!/^mato/ ? push @rest, $_ : 0 for @arr;
0;
}
sub sub3 {
my @rest = grep {!/^mato/} @arr;
0;
}
After that I added
my @rest=();
on the top of sub3(just for fairness) and sub1 with foreach was over _30%_
faster. Can you try this on your machine?
It seems that grep really sucks(or foreach makes very good job :)).
--
Matija
------------------------------
Date: Wed, 22 Jan 2003 13:04:00 -0800
From: Steven Kuo <skuo@mtwhitney.nsc.com>
Subject: Re: slow grep
Message-Id: <Pine.GSO.4.21.0301221258270.26833-100000@mtwhitney.nsc.com>
On Wed, 22 Jan 2003, Matija Papec wrote:
> Steven Kuo <skuo@mtwhitney.nsc.com> wrote:
>
> > ...
> >Add 'return @rest;' at the end of sub1 and try again.
>
> Yes, I did it and grep was still 10% slower.
My results are about the same:
Benchmark: timing 50000 iterations of a, b...
a: 5 wallclock secs ( 2.48 usr + 0.00 sys = 2.48 CPU) @ 20161.29/s (n=50000)
b: 4 wallclock secs ( 2.71 usr + 0.00 sys = 2.71 CPU) @ 18450.18/s (n=50000)
Rate b a
b 18450/s -- -8%
a 20161/s 9% --
(snipped some)...
>
> Can you try this on your machine?
>
> It seems that grep really sucks(or foreach makes very good job :)).
>
Yes, it does look like grep is slow(er) compared to for(each). I
also ran a comparison with -d:SmallProf:
% cat -n profile.pl
1 #! /usr/local/bin/perl
2
3
4 time_it(qw(tato fato mato gato mato3));
5
6 sub time_it {
7 for (0 .. 50) {
8 my @foreach;
9 my @grep;
10 !/^mato/ ? push @foreach, $_ : 0 for @_;
11 @grep = grep {!/^mato/} @_;
12 }
13 }
14
% perl -d:SmallProf ./profile.pl
% cat smallprof.out
================ SmallProf version 0.9 ================
Profile of ./profile.pl Page 1
=================================================================
count wall tm cpu time line
0 0.000000 0.000000 1:#! /usr/local/bin/perl
0 0.000000 0.000000 2:
0 0.000000 0.000000 3:
1 0.000188 0.000000 4:time_it(qw(tato fato mato gato mato3));
0 0.000000 0.000000 5:
1 0.000000 0.000000 6:sub time_it {
52 0.000435 0.000000 7: for (0 .. 50) {
51 0.000421 0.010000 8: my @foreach;
51 0.000328 0.000000 9: my @grep;
357 0.004940 0.010000 10: !/^mato/ ? push @foreach, $_ : 0 for
306 0.005139 0.020000 11: @grep = grep {!/^mato/} @_;
0 0.000000 0.000000 12: }
0 0.000000 0.000000 13:}
0 0.000000 0.000000 14:
--
Regards,
Steven
------------------------------
Date: 22 Jan 2003 22:26:02 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: slow grep
Message-Id: <b0n5pq$gfa$2@mamenchi.zrz.TU-Berlin.DE>
Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:
> sub sub1 {
> my @rest=();
> !/^mato/ ? push @rest, $_ : 0 for @arr;
> 0;
> }
> sub sub3 {
> my @rest = grep {!/^mato/} @arr;
> 0;
> }
Errrm... are you aware that sub1() and sub3() don't do the same
thing? For an emulation of grep, use something like this:
sub sub1 {
my @rest;
!/^mato/ and push @rest, $_ for @arr;
@rest;
}
Anno
------------------------------
Date: 22 Jan 2003 13:39:24 -0800
From: hamelcd@hotmail.com (Christopher Hamel)
Subject: Re: split with pattern matching question
Message-Id: <4f60d5b3.0301221339.3a09d059@posting.google.com>
hamelcd@hotmail.com (Christopher Hamel) carelessly wrote
>
> my ($line = $_) =~ s/\s*\|\s*/\|/g;
>
when he meant to say:
(my $line = $_) =~ s/\s*\|\s*/\|/g;
------------------------------
Date: Wed, 22 Jan 2003 10:03:52 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: To extract a specific portion of a text file
Message-Id: <slrnb2tg78.76k.tadmc@magna.augustmail.com>
John Smith <clearguy02@yahoo.com> wrote:
> I am giving the file contents here one more time:
Why?
What was wrong with the answers you got the first time around?
> Test Key : test_hello_there
> test_log_long_one
> test_short_my_song
> How are you doing there?
> Test Key : test_hello_howareyou
> test_log_hithere
> test_hi_my_song
> This time I have no typos.
It looks to me like you do.
You will never get the implementation correct if you do not
first get the specification correct.
> I need to have the output with only first
> "Test Key" field and its corresponding values.
> The output should be just as below:
> --------------------------------------------------------
> Test Key : test_hello
> test_log_hithere
> test_short_my_song
> ---------------------------------------------------------------------------
You said you wanted the first one.
That is not the first one.
In fact, it is not any of the ones!
> Can any of you help me fixing me the issue?
You do not have any open issues that I can see.
I already gave you 2 different ways of coding it that will work.
Why don't you use one of them?
Is there anybody in there? Just nod if you can hear me.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 22 Jan 2003 11:07:31 -0800
From: moneran@hotmail.com (David James)
Subject: Re: To extract a specific portion of a text file
Message-Id: <ce0257d.0301221107.3f05adac@posting.google.com>
> #! C:\\perl\\bin\\perl.exe
>
> open (INPUT1, "C:\\scripts\\extract.txt") ||
> die "Can not open the file: $!";
>
> @array = <INPUT1>;
>
> foreach (@array)
> {
> next unless ($_ =~ /\s*Test Key/ || $_ =~ /^\s*test_/);
> print;
> }
Instead, put:
#! C:/perl/bin/perl.exe
open (INPUT1, "C:/scripts/extract.txt") or die "Can not open the file:
$!";
my $found_test_key = 0;
while (<INPUT1>) {
last if $found_test_key and /^\s*Test Key/;
if (/^\s*Test Key/ || /^\s*test_/) {
$found_test_key++;
print;
}
}
Comments:
- Replaced use of backslashes in paths with forward slashes. Perl can
use either backslashes or forward slashes for paths, and forward
slashes look nicer
- Switched to not slurp in the whole file at the start of the program,
but instead only load in one line at a time. This saves considerable
memory on large files.
- I added a flag, found_test_key, that is set to true upon first
occurrence of Test Key. Only the first Test Key is printed out.
- I added a begin-string anchor to the "Test Key" regex, because it
looks like that was what you wanted to do. (Presumably I'm fixing a
typo)
------------------------------
Date: 22 Jan 2003 12:57:45 -0800
From: stefan@borgia.com (Stefan Adams)
Subject: Re: To extract a specific portion of a text file
Message-Id: <dcc927de.0301221257.b15bde@posting.google.com>
clearguy02@yahoo.com (John Smith) wrote in message news:<500f84f3.0301220728.30911d01@posting.google.com>...
> Hi all,
>
> Thanks for your kind response for my problem that I had posted here a
> couple of days ago.(in the perl deja news groups). Sorry for my typos
> in that posting.
>
> I am giving the file contents here one more time:
>
> ********************************** (start of the file,extract.txt)
>
> Component : Main TestPiece
> Language : C++
> Creation Date : 20-03-2002
> Test Key : test_hello_there
> test_log_long_one
> test_short_my_song
> How are you doing there?
>
> Copyright (C) Bla bla bla, All Rights Reserved
>
> This software is for only the test purposes.
> Test Key : test_hello_howareyou
> test_log_hithere
> test_hi_my_song
> #ifndef TEST_H
> #endif
> .......
> ..........
> #endif
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (end of file)
>
> This time I have no typos. I need to have the output with only first
> "Test Key" field and its corresponding values. Al these values, start
> with test_ and each value has atleast two underscores in it.
>
> The line following the last value of the first "Test Key" may be a
> blank line or any line with valid text. In this case it happens to be
> a line, " How are you doing there?".
>
> The script should ignore any following "Test Key" fields after the
> first "Test Key". Also it should ignore all the lines that have
> "test_",except those in the first "Test Key" field.
>
> The output should be just as below:
> --------------------------------------------------------
> Test Key : test_hello
> test_log_hithere
> test_short_my_song
> ---------------------------------------------------------------------------
>
> Here is my code:
> ------------------------------
> #! C:\\perl\\bin\\perl.exe
>
> open (INPUT1, "C:\\scripts\\extract.txt") ||
> die "Can not open the file: $!";
>
> @array = <INPUT1>;
>
> foreach (@array)
> {
> next unless ($_ =~ /\s*Test Key/ || $_ =~ /^\s*test_/);
> print;
> }
> ---------------------------------------
>
>
> But the output for this script is:
> -----------------------------------------------------
> Test Key : test_hello_there
> test_log_long_one
> test_short_my_song
> Test Key : test_hello_howareyou
> test_log_hithere
> test_hi_my_song
> --------------------------------------------------
>
> I don't want the last four lines in the output i.e. only the "Test
> Key" that appears for the first time in the text file and its all
> corresponding values.
>
> Can any of you help me fixing me the issue?
>
> Thanks,
> S
Here's my solution. It's a bit different. Rather than printing the
individual line it puts the keys you want into an array. It also gets
the file from the command line.
The gist of the solution is that I use a flag to tell me whether the
"Test Key" Key has been seen already or not. If not, process how you
prefer, if so, don't. My preference was to put the keys in an array,
yours was to print the line exactly as seen in the file as you
indicated in your OP...
I'd reckon that a more elegant solution would be to regex the entire
file as a scalar and use back references, but I don't use them as they
make my head hurt.
Hopefully this can at least get you to where you want to go.
-- Code --
use strict;
my $testkey = 0;
my @testkeys = ();
while ( <> ) {
$testkey++ if /\s*Test Key/;
if ( $testkey == 1 ) {
push @testkeys, $1 if /\s*(test_.*)$/;
}
}
print join "\n", @testkeys;
-- Output --
$ perl -w a.pl extract.txt
test_hello_there
test_log_long_one
test_short_my_song
SA
------------------------------
Date: 22 Jan 2003 10:27:11 -0800
From: mike_solomon@lineone.net (Mike Solomon)
Subject: Re: Win32::API convert code from C
Message-Id: <56568be5.0301221027.7a502d40@posting.google.com>
Bart,
Thanks very much for your help, it has given me a starting position.
Regards
Mike Solomon
Bart Lateur <bart.lateur@pandora.be> wrote in message news:<qjrs2vc271848ocie3h4jphg9j9b2q17b1@4ax.com>...
> Mike Solomon wrote:
>
> >If anyone one could give me some hints on how to write the following
> >code in Perl I would be very grateful
> >
> >It is the API calls that I have no idea about
>
>
> >Here is the part of the C code I have been given:
> >
> > /* Open the QuickAddress Pro databases */
> > iCode = QAPro_Open (psIniFile, psIniSect);
> ...
>
> There is stuff missing: you need the declarations. You must say which
> DLL these functions are in, and also, the API layer to them. For
> example, what parameters does QAPro_Open() expect, what does it return?
> You need that to produce a new function object in Win32::API, for this
> call alone. You can get that info either from the C source of the DLL,
> or at least from its ".h" header file. You need the latter to be able to
> compile the source you've been given into a working program.
>
> I can only assume that this particular function takes two string
> pointers as paramaters, and that it returns an integer. Assuming the DLL
> is called "QAPro.dll" (I don't know), then the function declaration can
> become:
>
> $QAPro_Open = new Win32::API('QAPro.dll', 'QAPro_Open', 'PP', 'N');
>
> and if this call succeeds, you can invoke it using
>
> my $iCode = $QAPro_Open->Call($psIniFile, $psIniSect);
>
> where the two arguments are plain Perl strings containing the path to
> the INI file and the title of the section, if my guess on what it
> represents is correct.
>
> Older versions of Win32::API still require an array reference for the
> third argument, one array item per parameter:
>
>
> $QAPro_Open = new Win32::API('QAPro.dll', 'QAPro_Open',
> ['P', 'P'], 'N');
>
> As my advice on how to tackle this: start by just a few calls, like the
> open, close, and maybe one more call in between. If this works, insert
> the rest of the program one part at a time. Test these smaller programs
> before you proceed.
>
> Things like "qaerr_POSTCODERECODED" look like constants to me. Get their
> value from the same ".h" file, and turn them into Perl constants:
>
> use constant qaerr_POSTCODERECODED => 15; # example value
>
> HTH,
> Bart.
------------------------------
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 4450
***************************************