[19880] in Perl-Users-Digest
Perl-Users Digest, Issue: 2075 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Nov 6 03:05:49 2001
Date: Tue, 6 Nov 2001 00:05:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1005033912-v10-i2075@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Tue, 6 Nov 2001 Volume: 10 Number: 2075
Today's topics:
Re: a good perl editor - know one?? (Gene Smilgiewicz)
Re: a good perl editor - know one?? <djberg96@hotmail.com>
Re: apostrophe character in text boxes and mysql <aneely@softouch.on.ca>
Re: Best language for low IQ programmers? <tim@vegeta.ath.cx>
Re: Best language for low IQ programmers? <nospam@nosite.zzz>
Re: Best language for low IQ programmers? (Thaddeus L Olczyk)
Re: Dynamically Assigning Scalars On The Fly (Eric Bohlman)
Re: Dynamically Assigning Scalars On The Fly <djberg96@hotmail.com>
Finding country extensions (.de, .co.uk, etc.) of users (Rob G.)
Re: Finding country extensions (.de, .co.uk, etc.) of u (Garry Williams)
Re: Finding country extensions (.de, .co.uk, etc.) of u <tony_curtis32@yahoo.com>
Re: helo! (Mark Taylor)
Re: helo! <wyzelli@yahoo.com>
moving files <pmi@iname.com>
Re: moving files (Garry Williams)
Naive Q: Why Java speed >> Perl speed? <kj0@mailcity.com>
Re: Naive Q: Why Java speed >> Perl speed? (Chris Fedde)
need help with simple script <khirv@hotmail.com>
Re: need help with simple script (Eric Bohlman)
Re: need help with simple script <uri@stemsystems.com>
Re: Newbie help please with free perl form scripts <wyzelli@yahoo.com>
Next page after an upload field is expired when I hit t <js@hotmail.com>
Next page after an upload field is expired when I hit t <js@hotmail.com>
Re: One for the Perl Wizards... <mgjv@tradingpost.com.au>
Re: Question on File uploading. <aneely@softouch.on.ca>
Re: regex help please <willis3140@yahoo.com>
Re: regex help please (Garry Williams)
Scalar Context Automatically Provided to Function Argum (David Reiss)
Re: Simple XML Parser example <ckruse@freenet.de>
Re: sorry ! (Chris Fedde)
Re: Teaching Perl to Middle School Students <ritchie@svs.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 5 Nov 2001 18:20:56 -0800
From: smig1@visto.com (Gene Smilgiewicz)
Subject: Re: a good perl editor - know one??
Message-Id: <89d13a67.0111051820.54d197a4@posting.google.com>
Paul Spitalny <pauls@cascadelinear.com> wrote in message news:<3BE6CAB2.647FD684@cascadelinear.com>...
> Hi,
> Anyone know of a text editor (For a windows platform) specifically
> targeted to PERL that has features like color coding of key works and
> other nice development aids?
>
> Thanks
>
> Paul
If you've got a fast machine, go to http://www.ActiveState.com and
download Komodo 1.1 -- it's "free" if you go for the noncommercial
version. Komodo is not just an editor but an IDE. In addition to
color coding, it flags lines with syntax errors. You can group files
into projects. It's got a visual debugger... step through code,
capture console output, watch variables. It's also cross platform
(Windows, Linux, ???).
This may not be the best editor out there but, since I'm still
basically learning Perl, I find the extra features very useful.
------------------------------
Date: Tue, 06 Nov 2001 04:02:18 GMT
From: "Daniel Berger" <djberg96@hotmail.com>
Subject: Re: a good perl editor - know one??
Message-Id: <ehJF7.26852$CN5.1821104@typhoon.mn.mediaone.net>
>
> If you've got a fast machine, go to http://www.ActiveState.com and
> download Komodo 1.1 -- it's "free" if you go for the noncommercial
> version. Komodo is not just an editor but an IDE. In addition to
> color coding, it flags lines with syntax errors. You can group files
> into projects. It's got a visual debugger... step through code,
> capture console output, watch variables. It's also cross platform
> (Windows, Linux, ???).
>
> This may not be the best editor out there but, since I'm still
> basically learning Perl, I find the extra features very useful.
Actually, the 1.2 Beta is out now, and it's at least usable on Windows. 1.1
was a miserable piece of dung, IMO. RAM hungry, slow, the cursor sometimes
randomly disappeared and the debugger flat out *just didn't work*. 1.2
seems to have fixed the big issues, but it's still kinda slow. But, hey,
it's free.
Not really "cross-platform", though. It works on Windows and Linux only
(i.e. there's no Solaris or FreeBSD version). Last time I tried it, the
Linux version was even clunkier than the windows version (though I haven't
tried the latest). I also don't like any program that forces me to manually
dump errors to /dev/null when I start it from the command line. Puke.
Regards,
Dan
------------------------------
Date: Tue, 06 Nov 2001 01:23:46 -0500
From: Amer Neely <aneely@softouch.on.ca>
Subject: Re: apostrophe character in text boxes and mysql
Message-Id: <3BE781F2.EEF709F0@softouch.on.ca>
hugo wrote:
>
> Hi
>
> (Apologies - I am reposting this as I didn't have a proper subject line
> in the
> message I sent previously.)
>
> I have a perl file with text boxes that users can fill in. The contents
> of the text boxes is stored in a mysql database. This all works fine,
> except when someone puts a apostrophe ' character in a text box e.g.,
> something like:
>
> Company's web site.
>
> When you have something with a ', all entries from all text boxes on the
> perl page are emptied out of mysql, presumably because the ' character
> messes with the INSERT statement and terminates it early. Note that the
> contents of the text boxes are retrieved with perl's param function
> using CGI.pm and stored in variables. Subsequently, these variables are
> used in the mysql INSERT statement when a call is made to the mysql
> database.
>
> How can I have people include the ' character without it causing
> problems for mysql?
>
> Any help will be greatly appreciated.
>
> Thanks
I ran into this exact same problem with my first Perl / MySQL script. I
don't however use CGI.pm.
First, I suggest you get a couple of books if you don't already: "MySQL"
by Paul DuBois; "Programming the Perl DBI" by Alligator Descartes & Tim
Bunce.
I found a few ways to get around this, but using 'quote' seems to solve
all the problems. For example:
&parse;
$InTitle=$FORM{Title};
# use this to properly quote single quotes (apostrophes) in the incoming
string
$qInTitle=$dbh->quote("$InTitle");
# don't quote the quoted string for output else it will have too many
quotes
print "\$qInTitle => " . $qInTitle . "<br>";
So if $InTitle was "Books From O'Reilly", pushing it through quote would
actually produce:
"Books From O\'Reilly".
Then you could:
$records=$dbh->do(qq{INSERT INTO Titles VALUES ($InTitle) } );
This uses the qq construct, making sure the characters you use to open
and close the construct don't appear in your string obviously:
$id=14;
$day="Friday";
$query= qq{INSERT something VALUES ($id,"$date")}; # braces used instead
of double quotes
In my case I didn't do anything to them at all, and all apostrophes are
retained and don't mess up anything. I was plesantly surprised. You can
see for yourself at:
http://web.softouch.on.ca/mycd-8.pl
Hope this helps.
--
Amer Neely aneely@softouch.on.ca
Softouch Information Services: www.softouch.on.ca/
Perl / CGI programming for shopping carts, data entry forms.
"We make web sites work!"
------------------------------
Date: Tue, 06 Nov 2001 02:05:22 GMT
From: Tim Hammerquist <tim@vegeta.ath.cx>
Subject: Re: Best language for low IQ programmers?
Message-Id: <slrn9ueho3.o7a.tim@vegeta.ath.cx>
Brian Metc <BrianMetc@aol.com> graced us by uttering:
> Would any one sugest the best language for me.
If you want to learn a language that solves any problem that today's
world can throw at you, look at COBOL. There's not a language out there
that is as widely used as COBOL. Not only does it offer "classic"
programming experience to the user, but it offers more methods of
reinventing the wheel than any other programming language!
--
How dare the government intervene to stifle innovation in the computer
industry! That's Microsoft's job, dammit!
------------------------------
Date: Tue, 06 Nov 2001 06:50:32 GMT
From: "Paul Lutus" <nospam@nosite.zzz>
Subject: Re: Best language for low IQ programmers?
Message-Id: <YKLF7.150471$b47.16083062@bin3.nnrp.aus1.giganews.com>
"Tim Hammerquist" <tim@vegeta.ath.cx> wrote in message
news:slrn9ueho3.o7a.tim@vegeta.ath.cx...
> Brian Metc <BrianMetc@aol.com> graced us by uttering:
> > Would any one sugest the best language for me.
>
> If you want to learn a language that solves any problem that today's
> world can throw at you, look at COBOL. There's not a language out there
> that is as widely used as COBOL. Not only does it offer "classic"
> programming experience to the user, but it offers more methods of
> reinventing the wheel than any other programming language!
You know, the relatively inexperienced OP may not get the satirical intent.
No little smiley to give anything away. :)
--
Paul Lutus
www.arachnoid.com
------------------------------
Date: Tue, 06 Nov 2001 07:11:26 GMT
From: olczyk@interaccess.com (Thaddeus L Olczyk)
Subject: Re: Best language for low IQ programmers?
Message-Id: <3bec8d19.343729046@nntp.interaccess.com>
On 5 Nov 2001 16:46:00 -0800, byhoe@greenlime.com (Adrian Hoe) wrote:
>No one is stupid in this world!
Yeah there are. Those who take the original post seriously to name a
few.
------------------------------
Date: 6 Nov 2001 02:23:30 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: Dynamically Assigning Scalars On The Fly
Message-Id: <9s7hj2$oq1$1@bob.news.rcn.net>
Mike Mackay [Ultrafusion] <newsgroup_mike@ultrafusion.co.uk> wrote:
> I have a 2 line csv file, displayed below as follows :
> first,last,country
> Mike,Mackay,England
> What I want my perl script to do is take the top line, and change each one
> of the field names into a scalar to reference it's value, for example my
> script would now have the following scalars : $first, $last, & $country.
> I would then have a print statement in my script just like the following :
> print "$first $last, comes from $country";
> and it should output : "Mike Mackay, comes from England".
> However I want this script to create the scalars and assign the data
> automatically for any csv file with any amount of fields without me having
> to open up the script and manually enter the scalar names. Can anyone offer
> any help of how I should go about : a) creating the scalars on the fly, and
> b) assigning a value to that scalar.
> I know how to open the script and get the top line only etc. It's just this
> dynamically assigned scalars that I can't figure out. If you need a further
> explanation then I can provide more info. Thanks for any advice/help.
You're really better off *not* trying to do it that way. Use a hash
instead. It's really quite dangerous to dynamically create variable names
based on things that aren't under your control as a programmer (for
example, a typo in the file could trash one of Perl's built-in variables,
causing some *very* difficult-to-track-down bugs, or one of the field
names in the file could be the same as the name of a variable that you
were using for something else).
Besides, if your variable names are dynamically generated based on your
input, how do you know what names to use in your program? You'd have to
modify the script anyway to print the right things. Perhaps if you told
us a little more about what you're trying to do, we could suggest some
alternatives.
------------------------------
Date: Tue, 06 Nov 2001 03:45:09 GMT
From: "Daniel Berger" <djberg96@hotmail.com>
Subject: Re: Dynamically Assigning Scalars On The Fly
Message-Id: <91JF7.26851$CN5.1820133@typhoon.mn.mediaone.net>
"Mike Mackay [Ultrafusion]" <newsgroup_mike@ultrafusion.co.uk> wrote in
message news:WoHF7.2919$Yk5.507617@news1.cableinet.net...
> I have a 2 line csv file, displayed below as follows :
> first,last,country
> Mike,Mackay,England
>
> What I want my perl script to do is take the top line, and change each one
> of the field names into a scalar to reference it's value, for example my
> script would now have the following scalars : $first, $last, & $country.
>
> I would then have a print statement in my script just like the following :
> print "$first $last, comes from $country";
> and it should output : "Mike Mackay, comes from England".
>
> However I want this script to create the scalars and assign the data
> automatically for any csv file with any amount of fields without me having
> to open up the script and manually enter the scalar names. Can anyone
offer
> any help of how I should go about : a) creating the scalars on the fly,
and
> b) assigning a value to that scalar.
>
> I know how to open the script and get the top line only etc. It's just
this
> dynamically assigned scalars that I can't figure out. If you need a
further
> explanation then I can provide more info. Thanks for any advice/help.
>
> Regards,
> Mike Mackay.
>
Can you do that? Yes - see "symbolic references".
Should you do that? No. Symbolic references are generally a "bad idea", if
for no other reason than you'll probably just end up confusing yourself
either in this program or future programs if you get into the habit now.
Use a hash instead, using the field names as keys
e.g.
# Some simple (though untested) code
my $n = 0;
my %hash;
while($line = <FH>){
my @fields;
if($n == 0){ # Grab the field names if this is the
first line
@fields = split(',',$line);
}
else{
@hash{@fields} = split(',',$line); # Use a hash slice
print "$hash{$field[0]} $hash{$field[1]}, comes from
$hash{$field[2]}\n"
}
$n++;
}
You could also consider DBD::CSV, though that may be overkill for this
problem.
Regards,
Dan
------------------------------
Date: 5 Nov 2001 18:44:32 -0800
From: rgoerss@khaoticproductions.com (Rob G.)
Subject: Finding country extensions (.de, .co.uk, etc.) of users calling script?
Message-Id: <e7d98463.0111051844.44a19349@posting.google.com>
Hi,
I won't go into details - Not necessary, but I need to be able to
find domain extension of people calling the script. REMOTE_HOST seems
to only return the IP address. Is there a way to resolve the hostmask
from the IP?
Thanks
------------------------------
Date: Tue, 06 Nov 2001 05:49:59 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: Finding country extensions (.de, .co.uk, etc.) of users calling script?
Message-Id: <slrn9ueug7.3qg.garry@zfw.zvolve.net>
On Mon, 05 Nov 2001 20:49:48 -0600, Tony Curtis <tony_curtis32@yahoo.com> wrote:
>>> On 5 Nov 2001 18:44:32 -0800,
>>> rgoerss@khaoticproductions.com (Rob G.) said:
>
> If it's a CGI question, please say so up front, it means a
> whole slew of problems and restrictions are clear
> immediately.
Actually, if it's a CGI question, find a more appropriate news group.
>> Hi, I won't go into details - Not necessary, but I need
>> to be able to find domain extension of people calling
>> the script. REMOTE_HOST seems to only return the IP
[ snip ]
> Net::DNS is probably the easiest way to go to find the
> fully-qualified name of the machine, *if one or more
> exist(s)*.
Well, maybe this is easier:
use Socket;
my $iaddr = "10.1.3.2";
my $host = gethostbyaddr( inet_aton($iaddr), AF_INET ) || $iaddr;
print "$host\n";
--
Garry Williams
------------------------------
Date: Mon, 05 Nov 2001 20:49:48 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Finding country extensions (.de, .co.uk, etc.) of users calling script?
Message-Id: <87ofmgzk8j.fsf@limey.hpcc.uh.edu>
>> On 5 Nov 2001 18:44:32 -0800,
>> rgoerss@khaoticproductions.com (Rob G.) said:
If it's a CGI question, please say so up front, it means a
whole slew of problems and restrictions are clear
immediately.
> Hi, I won't go into details - Not necessary, but I need
> to be able to find domain extension of people calling
> the script. REMOTE_HOST seems to only return the IP
Not possible in all circumstances, meaningless in some
circumstances even if you get an answer (e.g. proxy).
You should also not really make any assumptions about the
geographical location of the person (if there is one).
> address. Is there a way to resolve the hostmask from
> the IP?
(hostmask isn't a usual term, I think you meant
"hostname".)
Net::DNS is probably the easiest way to go to find the
fully-qualified name of the machine, *if one or more
exist(s)*.
hth
t
--
Oh! I've said too much. Smithers, use the amnesia ray.
------------------------------
Date: 5 Nov 2001 19:52:37 -0600
From: mtaylor@lrim.com (Mark Taylor)
Subject: Re: helo!
Message-Id: <Xns9150CCEB83C28maintainersetifaqorg@128.242.171.114>
weiwe1@yeah.net (hugh1) wrote in
<7dcf30ba.0111051752.52f732e8@posting.google.com>:
>#!/usr/local/bin/perl -wT
>
> print"Content-type: text/html";
>
> my $var_name;
> foreach $var_name (sort keys %ENV) {
> print "<P><B>$var_name</B></P>";
> print $ENV{$var_name};
> }
>
>this cgi program on the server B, i use netscape (on workstation A)
>
>location: http://serverB/cgi-bin/do_charge.cgi
>
>it is display " Internal Server Error
>
>The server encountered an internal error or misconfiguration and was
>unable to complete your request"
>
>if i telnet B from A ,and do it as " ./do_charge.cgi ". it display
>"# ./do_charge.cgi
>Content-type:
>text/html<P><B>DISPLAY</B></P>ghost.dbwindow.com:0<P><B>HOME</B></P>/ex
>port/home/gulong<P><B>HZ</B></P>100<P><B>LOGNAME</B></P>gulong<P><B>MAIL<
>/B></P>/var/
>mail/gulong<P><B>PATH</B></P>/usr/sbin:/usr/bin<P><B>PS1</B></P>#
><P><B>SHELL</B></P>
>/bin/sh<P><B>TERM</B></P>xterm<P><B>TZ</B></P>PRC<P><B>_INIT_PREV_LEVEL</
>B></P>S<P><B "
>it looks maybe right!! but why i can not get this answer on my nescape
>(workstation A)
>
Check your permissions first of all. When the httpd server executes the
program it does so as the user it is told to in the httpd.conf file.
Probably 'nobody' or 'apache'. It requires an x permission for others.
When you telnet into the server you do so as yourself which obviously has
the proper permissions.
Cheers,
Mark
______________________________________________________________________________
Posted Via Binaries.net = SPEED+RETENTION+COMPLETION = http://www.binaries.net
------------------------------
Date: Tue, 6 Nov 2001 12:24:09 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: helo!
Message-Id: <EgIF7.30$t46.211@wa.nnrp.telstra.net>
"hugh1" <weiwe1@yeah.net> wrote in message
news:7dcf30ba.0111051752.52f732e8@posting.google.com...
> #!/usr/local/bin/perl -wT
>
> print"Content-type: text/html";
print"Content-type: text/html\n\n";
>
> my $var_name;
> foreach $var_name (sort keys %ENV) {
> print "<P><B>$var_name</B></P>";
> print $ENV{$var_name};
> }
>
> this cgi program on the server B, i use netscape (on workstation A)
>
> location: http://serverB/cgi-bin/do_charge.cgi
Wyzelli
--
push@x,$_ for(a..z);push@x,' ';
@z='092018192600131419070417261504171126070002100417'=~/(..)/g;
foreach $y(@z){$_.=$x[$y]}y/jp/JP/;print;
------------------------------
Date: Tue, 06 Nov 2001 05:22:23 GMT
From: bubba <pmi@iname.com>
Subject: moving files
Message-Id: <jsKF7.77812$ez.10056973@news1.rdc1.nj.home.com>
I need to write this in perl (part of a larger program)
in Bash shell: mv `ls | head -50` ../dir
"Move the first 50 files from this directory to another directory
------------------------------
Date: Tue, 06 Nov 2001 07:08:41 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: moving files
Message-Id: <slrn9uf33o.3tg.garry@zfw.zvolve.net>
On Tue, 06 Nov 2001 05:22:23 GMT, bubba <pmi@iname.com> wrote:
> I need to write this in perl (part of a larger program)
>
> in Bash shell: mv `ls | head -50` ../dir
>
> "Move the first 50 files from this directory to another directory
^^^^^^^^^^^^^^
I assume you know that the bash command above will move directories as
well as files.
I assume you know the ls command (without any options) sorts the
directory entries.
I assume you know the ls command (without any options) skips any files
with names that begin with a period (.).
I assume you know the head -50 command will silently print all of its
input, if the number of lines of its input is less than 50.
Please read the perlfunc manual page section on rename() for cautions
about mutiple file systems and platform dependencies. (You might want
to consider the File::Find module.)
[ NOT tested ]
#!/usr/bin/perl -w
use strict;
opendir CWD, "." or die "can't open current directory: $!\n";
my $cnt;
my @files = grep { !/^\./ && $cnt++ < 50 } sort readdir CWD;
closedir CWD;
rename $_, "../dir/$_" for @files;
Expand the last statement into an explicit for loop to test for
errors.
--
Garry Williams
------------------------------
Date: 6 Nov 2001 03:03:31 GMT
From: kj0 <kj0@mailcity.com>
Subject: Naive Q: Why Java speed >> Perl speed?
Message-Id: <9s7ju3$kqj$3@news.panix.com>
Why is it that, typically, the Java VM runs programs so much faster
than the Perl interpreter? Is it because of Java's strong typing? Or
is it some other difference between the languages that accounts for
this disparity?
Thanks,
kj
------------------------------
Date: Tue, 06 Nov 2001 05:25:04 GMT
From: cfedde@fedde.littleton.co.us (Chris Fedde)
Subject: Re: Naive Q: Why Java speed >> Perl speed?
Message-Id: <QuKF7.460$X3.188713472@news.frii.net>
In article <9s7ju3$kqj$3@news.panix.com>, kj0 <kj0@mailcity.com> wrote:
>
>Why is it that, typically, the Java VM runs programs so much faster
>than the Perl interpreter? Is it because of Java's strong typing? Or
>is it some other difference between the languages that accounts for
>this disparity?
>Thanks,
>kj
>
That seems like a pretty broad statement. Have an example that
you want to show us? Maybe we can figure out a way to speed up the perl.
--
This space intentionally left blank
------------------------------
Date: Tue, 6 Nov 2001 13:07:02 +0800
From: "blongk" <khirv@hotmail.com>
Subject: need help with simple script
Message-Id: <3be77067$1_2@news.tm.net.my>
Hi
I need some help to see what is wrong with my script. This script is
supposed to show all keys and values in hash array %allsex. Here is the
script:
#!/usr/local/bin/perl -w
#This file extracts user ids and their respective genders from hypsex file
that contains user ids and genders.
my $B = "hypsex";
my %allsex;
open (B, "< $B") || die ("can not open $B\n");
my $id;
my $sex;
while ($id = <B>) {
$sex = <B>;
$allsex{$id} = $sex;
}
close (B);
foreach $id (keys %allsex) {
chomp($id);
print "$id $allsex{$id} \n";
}
The hypsex file contains id's and genders like this:
id1
M
id2
F
id3
M
id4
M
id5
F
...and so on...
and here is part of the the error:
Use of uninitialized value in concatenation (.) or string at ./hypmlpl line
32.
id7
Use of uninitialized value in concatenation (.) or string at ./hypmlpl line
32.
id5
Use of uninitialized value in concatenation (.) or string at ./hypmlpl line
32.
id2
Use of uninitialized value in concatenation (.) or string at ./hypmlpl line
32.
id8
Use of uninitialized value in concatenation (.) or string at ./hypmlpl line
32.
id4
...and so on...
If I dont use chomp, the result is like this:
id7
M
id5
F
id2
F
id8
M
id4
M
id5
F
..and so on..
which looks ok, but I want the gender to be on the same line with its id.
Another thing, why is the id's are not ordered as in the hypsex file.?
I appreciate any help. Thank you.
------------------------------
Date: 6 Nov 2001 05:27:33 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: need help with simple script
Message-Id: <9s7sc5$quj$1@bob.news.rcn.net>
blongk <khirv@hotmail.com> wrote:
> #!/usr/local/bin/perl -w
> #This file extracts user ids and their respective genders from hypsex file
> that contains user ids and genders.
> my $B = "hypsex";
> my %allsex;
> open (B, "< $B") || die ("can not open $B\n");
You should include $! in your error message so that if you can't open the
file, you'll know why.
> my $id;
> my $sex;
> while ($id = <B>) {
> $sex = <B>;
> $allsex{$id} = $sex;
> }
Right now both the keys and values of %allsex have trailing newlines.
Normally you'd want to chomp() both $id and $sex here before putting them
into the hash.
Better style would be to declare the two variables inside the loop rather
than outside it:
while (my $id = <B>) {
my $sex = <B>;
...
}
No big deal here, but scoping variables as tightly as possible is a good
habit to get into.
> close (B);
> foreach $id (keys %allsex) {
> chomp($id);
> print "$id $allsex{$id} \n";
Here you get a warning about uninitialized values because the actual keys
in the hash have trailing newlines, but you're stripping them off before
trying to index into the hash, so none of them match.
> }
> Another thing, why is the id's are not ordered as in the hypsex file.?
Because hash entries are stored in a seemingly random order (in reality,
it's an order that makes them quick too look up rather than one that makes
them look good).
------------------------------
Date: Tue, 06 Nov 2001 05:43:58 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: need help with simple script
Message-Id: <x7g07sv4fi.fsf@home.sysarch.com>
>>>>> "b" == blongk <khirv@hotmail.com> writes:
b> #!/usr/local/bin/perl -w
good -w
b> #This file extracts user ids and their respective genders from hypsex file
b> that contains user ids and genders.
b> my $B = "hypsex";
b> my %allsex;
i see my but no use strict. i assume it wasn't copied here.
b> open (B, "< $B") || die ("can not open $B\n");
good, check on open.
b> my $id;
b> my $sex;
b> while ($id = <B>) {
b> $sex = <B>;
b> $allsex{$id} = $sex;
b> }
b> close (B);
b> foreach $id (keys %allsex) {
b> chomp($id);
b> print "$id $allsex{$id} \n";
b> }
another post fixed your bug. but here is a far simpler solution:
%allsex = map { chomp, $_ } <B> ;
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
-- Stem is an Open Source Network Development Toolkit and Application Suite -
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Tue, 6 Nov 2001 17:30:27 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: Newbie help please with free perl form scripts
Message-Id: <VLMF7.6$Kd6.25@wa.nnrp.telstra.net>
"Dynamo" <robin1@otenet.gr> wrote in message
news:9s182g0178l@enews1.newsguy.com...
Hi,
I asked my ISP for the location of my sendmail prog and got the following
reply:
It might be off topic for this NG and if so I apologise but since I am a
total newbie I don't know where else to go cos my ISP offers no support for
CGI or perl scripts and as such have the following questions:
1) Can I use formmail.pl on my site and if so what do I need to do to get it
to work? If not can somebody please direct me to a free perl form script
that will work on WIN 2000 NT
Dont. formmail.pl is buggy and has security holes.
2) What or who is Fasthosts and what is Activestate Perl compared to perl?
Dunno about Fasthosts, but Activestate make a Windows port of Perl.
3) What is Blat?
Supposedly a 'sendmail' clone, actually a commandline MTA, the syntax is
different from sendmail so whilst it works, it requires re-writing of
scripts to handle the different syntax. I generally recommend one of the
'sendmail for windows' variants, my favourite being the one from
Dynamicstate. This is a command line equivalent, so very few script changes
are needed.
Wyzelli
--
push@x,$_ for(a..z);push@x,' ';
@z='092018192600131419070417261504171126070002100417'=~/(..)/g;
foreach $y(@z){$_.=$x[$y]}y/jp/JP/;print;
------------------------------
Date: Tue, 06 Nov 2001 05:33:48 GMT
From: "Kenneth Eide" <js@hotmail.com>
Subject: Next page after an upload field is expired when I hit the back button. Strange problem.
Message-Id: <0DKF7.1858$Sa4.311447@news01.chello.no>
Hello, I have created a simple upload script with CGI.pm.
It works like this:
Page one has an upload field, and a submit button called "Upload".
Page two (printed after i hit the upload button) shows the filename from the
upload field, and it has a text field, and a submit button called "Write
text".
Page threee (printed after i hit the write text button) shows the filename,
and the text i entered into the textfield.
My PROBLEM occurs when I'm hitting the back button from page three, page two
is then expired.
I assume this have something to do with the filefield, becuase when I
removed it then I could hit the back button from page three, and return to
page two (with the text I entered into the textfield).
It has to work exactly like this, with three different pages. The file I'm
uploading will sometimes be big, and I have to be able to correct the text I
entered into the textfield without having to upload the file again.
Note: I haven't made my script save the uplaoded file yet, becuase I need
this other stuff to work first.
Hopefully someone can help.thanks.
Regards, Kai Pettersen
Here's the script:
-----------------
#!/usr/bin/perl -w
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use strict 'vars';
my $q = new CGI;
# Global vars
my $action = $q->param('action');
my $fh = $q->upload('file');
my $text = $q->param('text');
my $filename = $q->param('filename');
# CGI stuff
print "Content-type: text/html","\n\n",
"<html><head>",
"<title>..testscript</title>",
"</head>";
if ($action =~ /^Upload|^Write/) {
if ($action eq "Upload") { save_files(); }
if ($action eq "Write text") { write_text(); }
}
else {
upload_form();
}
# page one
sub upload_form {
print $q->start_multipart_form,
"File: ", $q->filefield(-name=>'file'),
"<br>",
$q->submit(-name=>'action',-value=>'Upload'),
$q->endform;
}
sub save_files {
$fh =~ /\\([^\\]*)$/;
$filename = $1;
text_form();
}
# page two
sub text_form {
print $q->start_form,
$q->hidden(-name=>'filename',-default=>$filename),
"File: $filename<br> Text:", $q->textfield(-name=>'text'),
"<br>",
$q->submit(-name=>'action',-value=>'Write text'),
$q->endform;
}
# page three
sub write_text {
print "File: $filename<br>Text: $text<br>";
}
------------------------------
Date: Tue, 06 Nov 2001 05:35:38 GMT
From: "Kenneth Eide" <js@hotmail.com>
Subject: Next page after an upload field is expired when I hit the back button. Strange problem.
Message-Id: <KEKF7.1860$Sa4.311564@news01.chello.no>
Hello, I have created a simple upload script with CGI.pm.
It works like this:
Page one has an upload field, and a submit button called "Upload".
Page two (printed after i hit the upload button) shows the filename from the
upload field, and it has a text field, and a submit button called "Write
text".
Page threee (printed after i hit the write text button) shows the filename,
and the text i entered into the textfield.
My PROBLEM occurs when I'm hitting the back button from page three, page two
is then expired.
I assume this have something to do with the filefield, becuase when I
removed it then I could hit the back button from page three, and return to
page two (with the text I entered into the textfield).
It has to work exactly like this, with three different pages. The file I'm
uploading will sometimes be big, and I have to be able to correct the text I
entered into the textfield without having to upload the file again.
Note: I haven't made my script save the uplaoded file yet, becuase I need
this other stuff to work first.
Hopefully someone can help.thanks.
Regards, Kai Pettersen
Here's the script:
-----------------
#!/usr/bin/perl -w
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use strict 'vars';
my $q = new CGI;
# Global vars
my $action = $q->param('action');
my $fh = $q->upload('file');
my $text = $q->param('text');
my $filename = $q->param('filename');
# CGI stuff
print "Content-type: text/html","\n\n",
"<html><head>",
"<title>..testscript</title>",
"</head>";
if ($action =~ /^Upload|^Write/) {
if ($action eq "Upload") { save_files(); }
if ($action eq "Write text") { write_text(); }
}
else {
upload_form();
}
# page one
sub upload_form {
print $q->start_multipart_form,
"File: ", $q->filefield(-name=>'file'),
"<br>",
$q->submit(-name=>'action',-value=>'Upload'),
$q->endform;
}
sub save_files {
$fh =~ /\\([^\\]*)$/;
$filename = $1;
text_form();
}
# page two
sub text_form {
print $q->start_form,
$q->hidden(-name=>'filename',-default=>$filename),
"File: $filename<br> Text:", $q->textfield(-name=>'text'),
"<br>",
$q->submit(-name=>'action',-value=>'Write text'),
$q->endform;
}
# page three
sub write_text {
print "File: $filename<br>Text: $text<br>";
}
------------------------------
Date: Tue, 6 Nov 2001 15:14:29 +1100
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: One for the Perl Wizards...
Message-Id: <slrn9ueot5.flu.mgjv@martien.heliotrope.home>
On Tue, 06 Nov 2001 01:48:17 GMT,
Tad McClellan <tadmc@augustmail.com> wrote:
> Martien Verbruggen <mgjv@tradingpost.com.au> wrote:
>
>
>>Don't put multiple comments on one line.
>
>
> s/comments/statements/; # is what I'm sure Martien meant
Indeed. Thanks.
Martien
--
|
Martien Verbruggen | System Administration is a dirty job,
| but someone said I had to do it.
|
------------------------------
Date: Tue, 06 Nov 2001 01:49:14 -0500
From: Amer Neely <aneely@softouch.on.ca>
Subject: Re: Question on File uploading.
Message-Id: <3BE787EA.91B31991@softouch.on.ca>
Jason Kelley wrote:
>
> To answer my own question, rfc 1867
> (http://www.faqs.org/rfcs/rfc1867.html) States the following summary
> of the format, followed by the details of the format:
>
> 3.3 use of multipart/form-data
>
> The definition of multipart/form-data is included in section 7. A
> boundary is selected that does not occur in any of the data. (This
> selection is sometimes done probabilisticly.) Each field of the
> form
> is sent, in the order in which it occurs in the form, as a part of
> the multipart stream. Each part identifies the INPUT name within
> the
> original HTML form. Each part should be labelled with an
> appropriate
> content-type if the media type is known (e.g., inferred from the
> file
> extension or operating system typing information) or as
> application/octet-stream.
>
> If multiple files are selected, they should be transferred together
> using the multipart/mixed format.
>
> --Snip--
>
> 6. Examples
>
> Suppose the server supplies the following HTML:
>
> <FORM ACTION="http://server.dom/cgi/handle"
> ENCTYPE="multipart/form-data"
> METHOD=POST>
> What is your name? <INPUT TYPE=TEXT NAME=submitter>
> What files are you sending? <INPUT TYPE=FILE NAME=pics>
> </FORM>
>
> and the user types "Joe Blow" in the name field, and selects a text
> file "file1.txt" for the answer to 'What files are you sending?'
>
> The client might send back the following data:
>
> Content-type: multipart/form-data, boundary=AaB03x
>
> --AaB03x
> content-disposition: form-data; name="field1"
>
> Joe Blow
> --AaB03x
> content-disposition: form-data; name="pics";
> filename="file1.txt"
> Content-Type: text/plain
>
> ... contents of file1.txt ...
> --AaB03x--
>
> If the user also indicated an image file "file2.gif" for the answer
> to 'What files are you sending?', the client might client might
> send
> back the following data:
>
> Content-type: multipart/form-data, boundary=AaB03x
>
> --AaB03x
> content-disposition: form-data; name="field1"
>
> Joe Blow
> --AaB03x
> content-disposition: form-data; name="pics"
> Content-type: multipart/mixed, boundary=BbC04y
>
> --BbC04y
> Content-disposition: attachment; filename="file1.txt"
>
> Content-Type: text/plain
>
> ... contents of file1.txt ...
> --BbC04y
> Content-disposition: attachment; filename="file2.gif"
> Content-type: image/gif
> Content-Transfer-Encoding: binary
>
> ...contents of file2.gif...
> --BbC04y--
> --AaB03x--
>
> So the client chooses a boundary that will not appear in the data, and
> you can grab that and use it as a delimiter while parsing the data.
> New delimiters are chosen for each file when sending multiple files,
> in addition to the original delimiter. The filename can be read from
> the "Content-disposition: attachment; filename="..."" line.
>
> Now the question I have left is, if you are transfering a binary file,
> and you set the reading of the data to binmode in your script, how can
> you read the ascii of the other form elements? Is there a perl
> function that will convert them back, or can you switch modes in mid
> read? Someone help please? =\
First of all, you only need binmode if you're dealing with an MS-DOS /
NT box. It won't hurt if you're working with *nix though.
Second, the binmode only applies to the uploaded file, not the data from
other form fields. Here's how I've done it:
$file = $q->param('Upload');
open (OUTPUT,">$filename");
$length=0;
while (read($file,$data,BUFFER_SIZE)) # this works for binary and text
files
{
$length += length($_);
print OUTPUT $data;
}
close OUTPUT;
$filesize = (stat($filename))[7];
where $filename is something *I* create. NEVER USE THE FILENAME THE USER
SUPPLIES. If you don't believe me read the warnings in "CGI Programming
with Perl" and elsewhere about accepting strings from users.
BUFFER_SIZE is a constant previously defined.
Another must-have if use CGI.pm: "Official Guide to Programming with
CGI.pm" by Lincoln Stein.
--
Amer Neely aneely@softouch.on.ca
Softouch Information Services: www.softouch.on.ca/
Perl / CGI programming for shopping carts, data entry forms.
"We make web sites work!"
------------------------------
Date: Tue, 06 Nov 2001 02:41:06 GMT
From: W i l l <willis3140@yahoo.com>
Subject: Re: regex help please
Message-Id: <9ajeutkqeajpht5171fq7vusqr8233vu2k@4ax.com>
I don't know why I didn't do that in the first place. I really havn't
used DBI much yet, but I will be soon
Thanks,
W i l l
BTW -- here is another response I got
[maybe it will help someone too ] = > >
Answer: Regular Expression help, MS Access Pipe delimited export gone
bad contributed by hawson
How about this:
#!/usr/bin/perl -w
use strict;
my ($line, $spacer);
while(defined($_=<>)) {
s/\n//g;
if ($_ =~ /^[0-9]{3,5}/) {
print "$line\n" if defined($line);
$line=$_;
} else {
$spacer = (/^\|/ || $line =~ /\|$/) ? "" : " ";
$line.="$spacer$_";
}
}
print "$line\n"; #get last one...
If a line starts or ends with a pipe ("|"), then it assumes you do not
want extra whitespace around it. Otherwise, it should honor any
leading and trailing whitespace on each line (so if one line ends with
a tab, and the next starts with one, the code will spit out two tabs
in a row).
**** That did the trick.
On 5 Nov 2001 20:00:33 -0600, logan@cs.utexas.edu (Logan Shaw) wrote:
>In article <nteeuts03n2u09v1kb0t8plo2sglprrqfc@4ax.com>,
>W i l l <willis3140@yahoo.com> wrote:
>>I was given an Access database which I am trying to get into MySQL, I
>>read through the documentation on mysqlimport and it said that it was
>>only capable of importing text files. So I export my Access DB into a
>>pipe delimited file,
> :
> :
>>which resulted single lines looking like the following
>>
>>
>>3884|MC1|GW|Graphworks/Mirandas Needle|kit
>>|Mice Skating|EACH|$1.50|$0.75|MC1.JPG
>>
>>
>>My question is, how do I remove the newline '\n' on a line if the next
>
>I suggest that you don't. Instead, write a Perl program
>that connects to both databases (using DBI, DBD-mysql, and
>DBD-ODBC), does a "select * from foo" for each Access
>table, and then inserts the results into the mysql tables.
>
>Or if you can't do that (which you should be able to), have Perl
>extract everything from the database and then use something like
>Data::Dumper to spit it into a file format that Perl can handle. Then
>transfer that, suck it back in, and spit it into the target database.
>
>Or use one of those programs that's specifically designed
>to transfer tables between different databases.
>
> - Logan
------------------------------
Date: Tue, 06 Nov 2001 05:36:22 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: regex help please
Message-Id: <slrn9uetml.3qg.garry@zfw.zvolve.net>
On Tue, 06 Nov 2001 01:24:56 GMT, W i l l <willis3140@yahoo.com> wrote:
> I was given an Access database which I am trying to get into MySQL, I
> read through the documentation on mysqlimport and it said that it was
> only capable of importing text files. So I export my Access DB into a
> pipe delimited file, pipes delimiting the fields, and newlines
> delimiting the records. Well, to make a long story short, M$ Access's
> Export feature sucks, many of my rows were divided on whitespaces
> which resulted single lines looking like the following
>
>
> 3884|MC1|GW|Graphworks/Mirandas Needle|kit
>|Mice Skating|EACH|$1.50|$0.75|MC1.JPG
>
> My question is, how do I remove the newline '\n' on a line if the next
> line doesn't start with a 3-5 digit number and then print that new
> complete line to a seperate file?
>
> This is what I have so far, and it doesn't work.
You were so close...
No warnings? No strict? Perl can help.
#!/usr/bin/perl -w
use strict;
> open (PROD,"<products.txt");
> open (OUT,">out.txt");
*Always* check the result of opening files.
open (PROD,"<products.txt") || die "can't open 'products.txt': $!\n";
open (OUT,">out.txt") || die "can't open 'out.txt': $!\n";
> @array = <PROD>;
> foreach $line (@array){
Why read the whole file into memory only to process each line
individually?
while (my $line = <PROD>) {
> unless ($line =~/JPG?/){ chomp; }
> print OUT "$line"; }
The match succeeds whenever "JP" occurs in $line (with or without a
"G" following). I don't think that is what you mean to do. This also
does not seem to do what you said you needed to do. ("... remove the
newline '\n' on a line if the next line doesn't start with a 3-5 digit
number and then print that new complete line to a seperate file") You
are not checking the beginning of the records you read for a 3-5 digit
number.
Which is it?
Since you supply no parameter to chomp, it will chomp $_ by default.
Unfortunately, $_ has not been assigned a value. If you had enabled
warnings, perl would have told you that.
I think your intent (based on your code) is to print each line, but
remove the line ending on all lines that do not *end* with "JPG". Is
that correct?
unless ($line =~ /JPG$/) {
chomp $line;
}
print OUT $line;
}
But easier if you think filter.
#!/usr/bin/perl -w
use strict;
while (<>) {
chomp unless /JPG$/;
print;
}
This will read your input on STDIN and write the converted output on
STDOUT. It is called a filter. You could explicitly open a file for
input and output, but this makes it so much cleaner. If you named the
file "convert", you would use it like this:
perl convert products.txt > out.txt
As a matter of fact, you could just type the whole thing on a single
line (taking advatage of the -p option described in the perlrun manual
page):
perl -wpe 'chomp unless /JPG$/' products.txt > out.txt
Hope this helps.
--
Garry Williams
------------------------------
Date: 5 Nov 2001 23:03:55 -0800
From: reiss_david@yahoo.com (David Reiss)
Subject: Scalar Context Automatically Provided to Function Argument?
Message-Id: <4d538b3e.0111052303.1a4d47fd@posting.google.com>
Can I design a function (subroutine) that will automatically provide
scalar context to its argument? For example, how can I modify the
definition of foo() in the following script so that "1" is printed?
#!/usr/bin/perl -w
use strict;
sub foo {
my $var = $_[0];
print $var, "\n" ;
}
sub bar {
return 1 unless wantarray ;
return (2,3); # Only reached if wantarray is true.
}
foo(bar());
__END__
I don't want to have to write foo(scalar(bar)), but I can do another
workaround if necessary. TIA.
------------------------------
Date: Tue, 6 Nov 2001 07:56:42 +0100
From: Christian Kruse <ckruse@freenet.de>
Subject: Re: Simple XML Parser example
Message-Id: <Xns915150D229511ck1@wwwtech.de>
Joho,
anand_ramamurthy@yahoo.com (Anand Ramamurthy) wrote in
news:761041e6.0111051629.1f411a8f@posting.google.com:
> I am trying to write a very simple XML parser, to parse an XML config
> file. I am looking for a good example (perl script & corresponding data).
> Also, I would like to use the simplest Perl module (self contained).
I think you're looking for
http://search.cpan.org/search?dist=XML-Simple
Some examples can be found at
http://www.web.co.nz/~grantm/cpan/
--
Regards,
CK
------------------------------
Date: Tue, 06 Nov 2001 05:34:53 GMT
From: cfedde@fedde.littleton.co.us (Chris Fedde)
Subject: Re: sorry !
Message-Id: <1EKF7.461$X3.262232576@news.frii.net>
In article <7dcf30ba.0111051633.7c473fd6@posting.google.com>,
hugh1 <weiwe1@yeah.net> wrote:
>that is my posted message yestoday. i do not illustrate it clearly
>sorry about it!!!
>
> i want to know in perl programing how should call one cgi program which
>maybe complied with C or some other languages.
> how can i trans argv to it!!
>
Lets see if I understand. Calling another program from inside a perl
script can be done using the system() function or the qx() quote like
operator. Both are documented in the perl manual pages (perlfunc and
perlop respectively)
You might also be able to use the pipe form of open
open(F, "ls |") or die "$0: $!";
which is documented in the perlfunc manual page.
Calling perl from some other language is a different story. You will
probably want to ask a question in a news group about the target language.
It is possible to bind more closely than the above techniques. Look at the
perlembed, Inline and perlxs manual pages for some ideas.
good luck
--
This space intentionally left blank
------------------------------
Date: Mon, 05 Nov 2001 20:14:10 -0600
From: "David J. Ritchie" <ritchie@svs.com>
Subject: Re: Teaching Perl to Middle School Students
Message-Id: <3BE74770.FB47014B@svs.com>
>
> I thought your approach made a lot of sense- it reminded me
> immediately of my middle school age sentence diagrams, since
> that's the only time in my life that they actually mattered :)
Yes, you got it. There's so much going on in the Language Arts
area at this age that if you can connect it to the curriculum in
Language Arts where they are beginning to learn grammar (if
they haven't already) you have a much higher likelihood of
getting some thing up and running.
There was (and is) a big literacy push so the fact that at the end
of the session you had 14 students who had written their own
individual "Choose Your Own Adventure" stories implemented
with Perl print and if statements turned out to be a great success
factor as far as the staff evaluators of the program were concerned.
>
> Now some criticisms:
>
> 1) Maybe you could make better use of space; in places it looked to me
> like the comments were detracting from the program flow.
Yes, I agree. I like your fill-in-the-blank in the comment suggestion.
> 2) Adding a shebang line like
>
> #!/usr/bin/perl -w
> use diagnostics;
Yes. On the Mac where this first started in 1998, you had to check
a menu pull down.
>
> Also, I don't think it would hurt to quickly
> wean them off the "single thing" language and call them "scalars".
Yes, and despite my attempts to defend the English language approach,
(which I think helps some of them) I think it is very important (as
in teaching English as a Second Language) to wean them off the "crutch's"
very quickly.
At this age, I find quite a wide variation in the student's ability to
appreciate abstract terms so a lot depends as to who shows up as to
how quickly the "single thing" language disappears and the programming
terms come in. Certainly, in my interactions with them, I quickly
gravitate to the usual terminology.
>
> Lastly, one suggestion: some "find the bug" exercises might also
> be useful.
Good point.
--D.
--
ritchie@svs.com
http://home.mindspring.com/~djrassoc01/
------------------------------
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 2075
***************************************