[22496] in Perl-Users-Digest
Perl-Users Digest, Issue: 4717 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Mar 17 00:05:45 2003
Date: Sun, 16 Mar 2003 21:05:05 -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 Sun, 16 Mar 2003 Volume: 10 Number: 4717
Today's topics:
Anyone For Golf? (Resistor Colour Codes) (Shabble)
Bit off WAY more then... (Glenn)
Re: Bit off WAY more then... <acm2@ukc.ac.uk>
Re: Bit off WAY more then... (Randal L. Schwartz)
Re: Bit off WAY more then... (Glenn)
Re: file parse into hash and writing output with certai <krahnj@acm.org>
Re: file parse into hash and writing output with certai <tzeng@pacbell.net>
Re: file parse into hash and writing output with certai (david)
Re: How to find a word which is palindromica using REGE <qin@freebsd.lzu.edu.cn>
Re: How to find a word which is palindromica using REGE <qin@freebsd.lzu.edu.cn>
Re: How to find a word which is palindromica using REGE <qin@freebsd.lzu.edu.cn>
Re: Out of memory <REMOVEsdnCAPS@comcast.net>
Re: Out of memory <me@privacy.net>
Re: Out of memory (Ritesh Shah)
Re: Out of memory <noreply@gunnar.cc>
Re: Perl - How to parse a data file to display occuranc (shree)
Perl site. <mail@annuna.com>
Strange problem with tied hashes (long) (Miko O'Sullivan)
Re: Strange problem with tied hashes (long) <goldbb2@earthlink.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 16 Mar 2003 18:01:12 -0800
From: da_bisti@yahoo.com (Shabble)
Subject: Anyone For Golf? (Resistor Colour Codes)
Message-Id: <8fc4ae12.0303161801.335dc852@posting.google.com>
For no good reason, I decided to write a resistor colour code
translator in perl.
Well, actually, it was to prove to a random heathen that it was easier
and shorter than doing it in C++ (!)
Anyway, after a brief attempt, I realised its something that could be
quite small, and decided to trim it down.
You can see the initial attempt, and my current work below.
Rules:
take 3 strings on the command line, space seperated, each being the
name of a colour in the following list:
#my %colours = ("black" => 0, "brown" => 1, "red" => 2, "orange" => 3,
"yellow" => 4, "green" => 5, "blue" => 6, "violet" => 7, "grey" => 8,
"white" => 9 );
Convert it to a number and print its value (and a \n)
It should be fairly obvious from the code below how to calculate the
value.
--sample code--
my %colours = ("black" => 0, "brown" => 1, "red" => 2, "orange" => 3,
"yellow"
=> 4, "green" => 5,"blue" => 6, "violet" => 7, "grey" => 8, "white" =>
9 );
my ($band1, $band2, $band3) = @ARGV;
$band1 = $colours{$band1};
$band2 = $colours{$band2};
$band3 = $colours{$band3};
my $futile = (($band1 * 10) + $band2) * (10**$band3);
print "Resistance is $futile\n";
--shorter code (153 chars)--
@c=qw(bla br r o y gree b v g w);for(reverse@ARGV
){$i=0;while(1){if(/^\Q$c[$i]/i){push@x,$i;last}$
i++}}print((@x,pop(@x)*10+pop@x)*10**pop@x)."\n";
Any comments/suggestions/entries?
Have Fnu!
Shabble!
------------------------------
Date: 16 Mar 2003 11:35:33 -0800
From: GHboom1@yahoo.com (Glenn)
Subject: Bit off WAY more then...
Message-Id: <1ab0deb8.0303161135.716916b9@posting.google.com>
I would like to have a perl script that would act like a
browser, and read through 5 seperate web pages,
I would like it to then see if it found any links
matching a name (not a url). If a match was found
display the name and link to the url and also let
me know what page its on. Example (2of5)
Now, I have done some looking and poking,
LWP::Simple,HTML::LinkEtor etc
I did think at first I could do it in the timeframe
I have, but more on that later....
1)I think if it works I will use the script alot,
so I thought that maby I should Open the URLS to
search and save them loacaly every 5 min or so and
time stamp the saved files,
so as not to bombard the other server. Good Idea ?
OK, so the real help needed here part comes in.
What I thought needed to be done in plain english,
(Before writing the plain english below,
I thought I could do this with just a few pointers,
Now Im adding this blurb afterwards, and I realise I need
more then a pointer or two !)
------------
In a config.pl file
$Mytxt="My_TXT_im_Looking_For"
List 5Ext URLS,
List 5 Html files that may be saved localy
Give location of Check.pl
give location of Search.pl
--------------
In the check.pl (check.pl would be called from a browser)
require config.pl
see if 5 htm files are already saved localy
If they are
read TimeStamp.txt and get time stamp
if time stamp < 5min old
end if
Else
open TimeStamp.txt and over right with new time
(I know that line above is prolly not best,
but once set up It should work ok right ?)
foreach URL inconfig
open url print local as 1-5.htm
run search.pl
--------
Search.pl
(would you have to require config.pl here or would
config.pl be already there because check.pl sent us here ?)
Foreach localSavedHtm
Open local htm
search for $Myttxt
If localhtm Has $Mytxt
print to browser $Mytxt and Link it to URL listed
els print to browser "Sorry txt not found"
end sub
----------
Thats about as good as I can do
with the limited amount of knowledge I have
ANY suggestions, Hints,
WishFull thinking, Complete Code Script
WOULD be Apprectiated GREATLY !
Glenn
------------------------------
Date: Sun, 16 Mar 2003 19:45:16 -0000
From: Tony McNulty <acm2@ukc.ac.uk>
Subject: Re: Bit off WAY more then...
Message-Id: <oprl47tqdc2czp9w@news.ukc.ac.uk>
On 16 Mar 2003 11:35:33 -0800, Glenn <GHboom1@yahoo.com> wrote:
> 1)I think if it works I will use the script alot, so I thought that maby
> I should Open the URLS to search and save them loacaly every 5 min or so
> and time stamp the saved files, so as not to bombard the other server.
> Good Idea ?
As an extra thing, it might be better to just get the HEAD from the server
and compare the updated files. Will save bombarding the server even more,
especially if the html pages are large, and will also save you needless
processing time.
Just a little hint :D
Tony
--------------------------------------
web: http://mercutio.digitalrice.com
email: mercutio at digitalrice dot com
--------------------------------------
------------------------------
Date: Sun, 16 Mar 2003 20:11:14 GMT
From: merlyn@stonehenge.com (Randal L. Schwartz)
To: GHboom1@yahoo.com (Glenn)
Subject: Re: Bit off WAY more then...
Message-Id: <710a42bf12d5b9a3cb78892e9447267a@news.teranews.com>
>>>>> "Glenn" == Glenn <GHboom1@yahoo.com> writes:
Glenn> 1)I think if it works I will use the script alot,
Glenn> so I thought that maby I should Open the URLS to
Glenn> search and save them loacaly every 5 min or so and
Glenn> time stamp the saved files,
Glenn> so as not to bombard the other server. Good Idea ?
Use LWP::Simple's "mirror". It changes the timestamp of the local
copy to the time of the remote file, and then uses that information
the next time. The server can respond "not modified" if nothing has
changed, saving the server time and you bandwidth.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: 16 Mar 2003 14:15:58 -0800
From: GHboom1@yahoo.com (Glenn)
Subject: Re: Bit off WAY more then...
Message-Id: <1ab0deb8.0303161415.3180d5b0@posting.google.com>
Tony,
Consider your self hired!
But seriously, thanks Ill amend my game plan !
> As an extra thing, it might be better to just get the HEAD from the server
> and compare the updated files.
Glenn
------------------------------
Date: Mon, 17 Mar 2003 00:15:35 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: file parse into hash and writing output with certain format to new file
Message-Id: <3E751399.A27912D7@acm.org>
david wrote:
>
> Thank you for the explanation - that does make sense. Everything is
> working great. One last question, in my "print OUT" statement, every
> line after the 1st one has a space in front of it (i.e.):
>
> 00000000,$$U23567$$,$$HMJ0002$$
> 00000000,$$U23571$$,$$HMJ0003$$
> 00000000,$$U23572$$,$$HMJ0004$$
>
> Am I missing a chomp somewhere? Any idea where this extra space is
> coming from?
perldoc -q "Why do I get weird spaces when I print an array of lines"
John
--
use Perl;
program
fulfillment
------------------------------
Date: Mon, 17 Mar 2003 00:22:36 GMT
From: Ted Zeng <tzeng@pacbell.net>
Subject: Re: file parse into hash and writing output with certain format to new file
Message-Id: <3E751697.FE0C3910@pacbell.net>
I didn't read it in details. But it seems you should have a chomp here
> while ( <FILE> ) {
----------------> chomp;
> > my @fields = split ( /,/ );
I could be wrong.
david wrote:
> "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de> wrote in message news:<b51a8o$1ej$1@nets3.rz.RWTH-Aachen.DE>...
> > Also sprach david:
> >
> > > This is what I have now. I have found that running this under 'use
> > > strict;' and with -w causes errors if I do not define the variables at
> > > the start of the program. Maybe they dont need to be defined at the
> > > beginning? Is there something im missing here?
> >
> > You have to declare your variables in the scope (or block) where you are
> > using them. A my()ed variable declared in a block exists only for this
> > block and is automatically destroyed when leaving it.
> >
> > > One last thing - where I have the print NEWOUT statements, Is it
> > > possible to define a syntax like:
> > > print <<MYOUT;
> > > User= $ser
> > > Number = $fields[1]
> > > MYOUT
> > >
> > > where I currently have my 'print NEWOUT' statements? I am currently
> > > getting an error "Cant find stirng terminator "MYOUT" anywhere before
> > > EOF at test.pl line 20."
> >
> > Possibly some ill positioned whitespaces or so. The trailing string
> > terminator of here-docs must not be preceeded nor followed by
> > whitespaces. That means that your indenting gets screwed, but that might
> > be tolerable if you have an editor with decent syntax-highlighting. So
> > you can write:
> >
> > while (...) {
> > ...
> > if (...) {
> > print NEWOUT <<MYOUT;
> > User = $ser
> > Number = $fields[1]
> > MYOUT
> > }
> > ...
> > }
> >
> > There's also a FAQ dealing with here-documents. See
> >
> > perldoc -q '<<HERE'
> >
> > > Again, thanks for your help. It has been very useful.
> > >
> > > #!/usr/bin/perl -w
> > > use strict;
> > >
> > > my %records;
> > > my (@ser, @seperate, @fields);
> > > my $ser;
> >
> > You still have far too many variables declared here. Both @fields and
> > $ser are only used within the while-block. Actually, when you declare
> > these two variables again in this block, you create two new variables
> > that don't have anything to do with the variables you declared above.
> > They have the same name but they are still different. Change to:
> >
> > my (%records, @separate, @ser); # don't know what @ser is for, though
> >
> > > open (FILE, "<import0.dat") || die "Couldn't open file; $!\n";
> > > open (OUT, ">output.dat") || die "Couldn't create file; $!\n";
> > > open (NEWOUT, ">newout.dat") || die "Couldn't create; $!\n";
> > >
> > > while ( <FILE> ) {
> > > my @fields = split ( /,/ );
> > > if ( $fields[7] =~ m{^\$\$S\/N:(\w+)} ) {
> > > my $ser = '$$' . $1 . '$$';
> > > ( $fields[1], $ser ) = ( $ser, $fields[1] );
> > > push (@seperate, join (',', @fields));
> > > $records{$ser} = $fields[1];
> > > print NEWOUT "User = $ser\n";
> > > print NEWOUT "Number = $fields[1]\n";
> > > }
> > >
> > > }
> > >
> > > #while ( ($key,$value ) = each %records ) {
> > > # print OUT "$key --> $value\n"
> > > #}
> >
> > If you decide to activate these lines later, you have to declare $key
> > and $value. Restrict their scope to the while-block that way:
> >
> > while (my ($key, $value) = each %records) {
> > print OUT "$key --> $value\n";
> > }
> >
> > > print OUT "@seperate\n";
> >
> > And now you see why you have to declare %records and @separate before
> > the while-loop. These two variables have to be accessible both in the
> > loop and after it. Declare variables in the top-most scope you are going
> > to use them in.
> >
> > Tassilo
>
> Thank you for the explanation - that does make sense. Everything is
> working great. One last question, in my "print OUT" statement, every
> line after the 1st one has a space in front of it (i.e.):
>
> 00000000,$$U23567$$,$$HMJ0002$$
> 00000000,$$U23571$$,$$HMJ0003$$
> 00000000,$$U23572$$,$$HMJ0004$$
>
> Am I missing a chomp somewhere? Any idea where this extra space is
> coming from?
>
> Thanks again,
------------------------------
Date: 16 Mar 2003 16:04:51 -0800
From: dwlepage@yahoo.com (david)
Subject: Re: file parse into hash and writing output with certain format to new file
Message-Id: <b09a22ae.0303161604.fe9dfad@posting.google.com>
"Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de> wrote in message news:<b51a8o$1ej$1@nets3.rz.RWTH-Aachen.DE>...
> Also sprach david:
>
> > This is what I have now. I have found that running this under 'use
> > strict;' and with -w causes errors if I do not define the variables at
> > the start of the program. Maybe they dont need to be defined at the
> > beginning? Is there something im missing here?
>
> You have to declare your variables in the scope (or block) where you are
> using them. A my()ed variable declared in a block exists only for this
> block and is automatically destroyed when leaving it.
>
> > One last thing - where I have the print NEWOUT statements, Is it
> > possible to define a syntax like:
> > print <<MYOUT;
> > User= $ser
> > Number = $fields[1]
> > MYOUT
> >
> > where I currently have my 'print NEWOUT' statements? I am currently
> > getting an error "Cant find stirng terminator "MYOUT" anywhere before
> > EOF at test.pl line 20."
>
> Possibly some ill positioned whitespaces or so. The trailing string
> terminator of here-docs must not be preceeded nor followed by
> whitespaces. That means that your indenting gets screwed, but that might
> be tolerable if you have an editor with decent syntax-highlighting. So
> you can write:
>
> while (...) {
> ...
> if (...) {
> print NEWOUT <<MYOUT;
> User = $ser
> Number = $fields[1]
> MYOUT
> }
> ...
> }
>
> There's also a FAQ dealing with here-documents. See
>
> perldoc -q '<<HERE'
>
> > Again, thanks for your help. It has been very useful.
> >
> > #!/usr/bin/perl -w
> > use strict;
> >
> > my %records;
> > my (@ser, @seperate, @fields);
> > my $ser;
>
> You still have far too many variables declared here. Both @fields and
> $ser are only used within the while-block. Actually, when you declare
> these two variables again in this block, you create two new variables
> that don't have anything to do with the variables you declared above.
> They have the same name but they are still different. Change to:
>
> my (%records, @separate, @ser); # don't know what @ser is for, though
>
> > open (FILE, "<import0.dat") || die "Couldn't open file; $!\n";
> > open (OUT, ">output.dat") || die "Couldn't create file; $!\n";
> > open (NEWOUT, ">newout.dat") || die "Couldn't create; $!\n";
> >
> > while ( <FILE> ) {
> > my @fields = split ( /,/ );
> > if ( $fields[7] =~ m{^\$\$S\/N:(\w+)} ) {
> > my $ser = '$$' . $1 . '$$';
> > ( $fields[1], $ser ) = ( $ser, $fields[1] );
> > push (@seperate, join (',', @fields));
> > $records{$ser} = $fields[1];
> > print NEWOUT "User = $ser\n";
> > print NEWOUT "Number = $fields[1]\n";
> > }
> >
> > }
> >
> > #while ( ($key,$value ) = each %records ) {
> > # print OUT "$key --> $value\n"
> > #}
>
> If you decide to activate these lines later, you have to declare $key
> and $value. Restrict their scope to the while-block that way:
>
> while (my ($key, $value) = each %records) {
> print OUT "$key --> $value\n";
> }
>
> > print OUT "@seperate\n";
>
> And now you see why you have to declare %records and @separate before
> the while-loop. These two variables have to be accessible both in the
> loop and after it. Declare variables in the top-most scope you are going
> to use them in.
>
> Tassilo
Thank you for the explanation - that does make sense. Everything is
working great. One last question, in my "print OUT" statement, every
line after the 1st one has a space in front of it (i.e.):
00000000,$$U23567$$,$$HMJ0002$$
00000000,$$U23571$$,$$HMJ0003$$
00000000,$$U23572$$,$$HMJ0004$$
Am I missing a chomp somewhere? Any idea where this extra space is
coming from?
Thanks again,
------------------------------
Date: 17 Mar 2003 12:26:57 +0800
From: qin jiang wei <qin@freebsd.lzu.edu.cn>
Subject: Re: How to find a word which is palindromica using REGEXP
Message-Id: <86bs0apqi6.fsf@freebsd.lzu.edu.cn>
Garry Williams <garry@ifr.zvolve.net> writes:
> On 15 Mar 2003 23:08:11 +0800, qin jiang wei <qin@freebsd.lzu.edu.cn>
> wrote:
>
> > How can I find the palindromica words in /usr/share/dict/words
> > For example , the word 'level'.
>
> #!/usr/bin/perl -n
> use warnings;
> use strict;
>
> my $half = (length() - 1) / 2;
>
> my $re = '(.)' x $half;
>
> $re .= '.' if length % 2;
>
> $re .= join '', map "\\$_", reverse 1 .. $half;
>
> print if /^$re$/;
>
> --
> Garry Williams
Thank everyone very much!
But now I want to find the word which is not palindrome . The style is like
this : 'abcOTHERcba' , while OTHER can be any length , But the length of 'abc'
and 'cba'is as maximal length as it can. How can I ?
Sorry for my bad expression.
------------------------------
Date: 17 Mar 2003 12:33:58 +0800
From: qin jiang wei <qin@freebsd.lzu.edu.cn>
Subject: Re: How to find a word which is palindromica using REGEXP
Message-Id: <867kaypq6h.fsf@freebsd.lzu.edu.cn>
Thank everyone very much!
But now I want to find the word which is not palindrome . The style is like
this : 'abcOTHERcba' , while OTHER can be any length , But the length of 'abc'
and 'cba'is as maximal length as it can. How can I ?
Sorry for my bad expression.
------------------------------
Date: 17 Mar 2003 12:39:27 +0800
From: qin jiang wei <qin@freebsd.lzu.edu.cn>
Subject: Re: How to find a word which is palindromica using REGEXP
Message-Id: <863clmppxc.fsf@freebsd.lzu.edu.cn>
Thank everyone very much!
But now I want to find the word which is not palindrome . The style is like
this : 'abcOTHERcba' , while OTHER can be any length , But the length of 'abc'
and 'cba'is as maximal length as it can. How can I ?
Sorry for my bad expression.
------------------------------
Date: Sun, 16 Mar 2003 14:08:13 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Out of memory
Message-Id: <Xns934099FB1CFB2sdn.comcast@216.166.71.239>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
ritesh_shah2000@yahoo.com (Ritesh Shah) wrote in
news:243164f.0303160310.4dd834ae@posting.google.com:
> Hi,
> I have 1GB RAM on my computer. When I try to parse a file of
size
> more than 1GB with my perl script I get "Out of memory" message.
Is
> there a way to get around the problem without adding extra
hardware?
Are you reading the entire file into memory? Beginners often do that
when they could simply read the file one line at a time.
- --
Eric
print scalar reverse sort qw p ekca lre reh
ts uJ p, $/.r, map $_.$", qw e p h tona e;
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13
iD8DBQE+dNmpY96i4h5M0egRAgSKAKC5jN271P+k/nJWfiUSF1LQYKxvZgCcDL3n
VsUnWHMchv9q+OBUCFNXxkg=
=hEZd
-----END PGP SIGNATURE-----
------------------------------
Date: Mon, 17 Mar 2003 11:49:26 +1100
From: "Tintin" <me@privacy.net>
Subject: Re: Out of memory
Message-Id: <b5362o$251fqi$1@ID-172104.news.dfncis.de>
"Eric J. Roode" <REMOVEsdnCAPS@comcast.net> wrote in message
news:Xns934099FB1CFB2sdn.comcast@216.166.71.239...
> -----BEGIN xxx SIGNED MESSAGE-----
> Hash: SHA1
>
> ritesh_shah2000@yahoo.com (Ritesh Shah) wrote in
> news:243164f.0303160310.4dd834ae@posting.google.com:
>
> > Hi,
> > I have 1GB RAM on my computer. When I try to parse a file of
> size
> > more than 1GB with my perl script I get "Out of memory" message.
> Is
> > there a way to get around the problem without adding extra
> hardware?
>
> Are you reading the entire file into memory? Beginners often do that
> when they could simply read the file one line at a time.
And there was a long thread about this not so long ago asking where
beginners get the idea that the only way to read a file is to store it in an
array and loop over the array.
Question for the OP, where did you obtain the logic/code to read a file into
memory?
------------------------------
Date: 16 Mar 2003 17:35:43 -0800
From: ritesh_shah2000@yahoo.com (Ritesh Shah)
Subject: Re: Out of memory
Message-Id: <243164f.0303161735.22b2e7ca@posting.google.com>
Thanks for your reply. I am using a similar while loop in my perl
script. I get the "Out of memory" message after my perl script has
parsed around 1GB of the input file. Is there a way to tell perl to
free the buffers after a certain checkpoint so that they can be used
again to store file contents until the next checkpoint. This is the
psuedocode of my perl script.
#declare hashes, arrays
#open input file
while(not end of file) {
#initialize all arrays hashes
while( checkpoint does not occur) {
#read one line from the file
#parse line to obtain data and
#store them in arrays, hashes
}
#perform calculation on obtained data
#print the result ##These results actually get printed until out of
##memory message
}
Thanks in advance,
Ritesh
Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<b51mng$247nsm$1@ID-184292.news.dfncis.de>...
> Ritesh Shah wrote:
> > I have 1GB RAM on my computer. When I try to parse a file of size
> > more than 1GB with my perl script I get "Out of memory" message. Is
> > there a way to get around the problem without adding extra hardware?
>
> By using a 'while' loop like this:
>
> open FH, 'myfile';
> while (<FH>) {
> # parse code
> }
> close FH;
>
> and thus examining one line at a time, I think you prevent the whole
> file contents from being loaded into the memory.
>
> / Gunnar
------------------------------
Date: Mon, 17 Mar 2003 03:37:12 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Out of memory
Message-Id: <b53cf0$24sl65$1@ID-184292.news.dfncis.de>
Ritesh Shah wrote:
> Thanks for your reply. I am using a similar while loop in my perl
> script. I get the "Out of memory" message after my perl script has
> parsed around 1GB of the input file. Is there a way to tell perl to
> free the buffers after a certain checkpoint so that they can be used
> again to store file contents until the next checkpoint.
I think others in this group should help you with that question. But I
think you'd better post (an extract from) the real program.
> #declare hashes, arrays
> #open input file
>
> while(not end of file) {
>
> #initialize all arrays hashes
>
> while( checkpoint does not occur) {
>
> #read one line from the file
> #parse line to obtain data and
> #store them in arrays, hashes
-------------------------------^^^^^^--^^^^^^
How much do you store?
/ Gunnar
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: 16 Mar 2003 19:30:13 -0800
From: srigowrisn@hotmail.com (shree)
Subject: Re: Perl - How to parse a data file to display occurances of each type (hash)?
Message-Id: <49b5740e.0303161930.209225a6@posting.google.com>
Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<b50v5v$23sg42$1@ID-184292.news.dfncis.de>...
> shree wrote:
> > I wrote an html survey form whose action invokes a perl cgi script to
> > append form submission as a new record into a flat file. This is
> > working fine. Now, I'm asked to process this results file. Here I'm
> > running into difficulties and would appreciate if anyone can help
> > share a code snippett.
>
> Here is a try:
>
> my (%question);
>
> open FH, 'results' or die "Couldn't open 'results'\n$!";
> while (<FH>) {
> chomp (my @record = split /\|/);
> for (1..4) { $question{$_}{$record[$_]}++ }
> for (5..6) { push @{$question{$_}}, $record[$_] if $record[$_] }
> }
> close FH;
>
> print "Radio Button results\n",
> 'Questions|Agree|Neutral|Disagree', "\n";
> for (1..4) {
> print "Q$_", '|', $question{$_}{'Agree'} || 0, '|',
> $question{$_}{'Neutral'} || 0, '|',
> $question{$_}{'Disagree'} || 0, "\n";
> }
> print "\n";
> print "Text Area results\n";
> for (5..6) {
> print "Question $_\n";
> for (@{$question{$_}}) { print "$_\n" }
> print "\n";
> }
>
> / Gunnar
Hi Gunnar,
Thank you..the snippett you shared with me..worked perfectly.
Shree
------------------------------
Date: Sun, 16 Mar 2003 17:42:02 -0600
From: Joe Creaney <mail@annuna.com>
Subject: Perl site.
Message-Id: <3E750BCA.8060604@annuna.com>
I put a perl page on my web site. I have been learning perl and writing
a simple adventure game. This is a learning exercise. I am looking
distribute my game and other things I post there. Please check out the
site espically if you are a new programmer.
www.annuna.com/perl5
------------------------------
Date: 16 Mar 2003 12:01:50 -0800
From: miko@idocs.com (Miko O'Sullivan)
Subject: Strange problem with tied hashes (long)
Message-Id: <db27ea77.0303161201.274bad2c@posting.google.com>
Strange problem with tied hashes (long)
I've run into an interesting problem working with tied hashes.
Although I've managed to kludge a solution, it's a sufficiently
strange problem that it would be worthwhile to understand the cause of
it. I don't know if the problem is a bug in perl or in my code. I've
produced the bug in perl 5.6.1 running on NetBSD.
The quick summary of the problem is this. If the FETCH routine in a
tied hash module reads data from a reference to another tied hash, and
then returns yet another reference to a hash (tied or not), and if the
calling code tries to directly modify that returned hash ref, then a
fatal error results: "Can't use an undefined value as a HASH
reference".
Let's walk through an example. The following code example demonstrates
the problem. The test code consists of a module for implementing a
tied hash, and a script that uses that module. I've labelled the
three main pieces of code that are crucial to reproducing the error as
[A], [B], and [C].
Let's start with the script:
------------- begin cut ----------------
#!/usr/local/bin/perl -w
use strict;
use lib './';
use Borman;
use Tie::Hash;
# variables
my (%srchash, %outer, $record);
# [A]
# tie %srchash to anything
# if you do not tie the source hash something, there is no error
tie(%srchash, 'Tie::StdHash') or die 'unable to tie %srchash';
# tie %outer to Borman, passing %srchash as the source of data
tie(%outer, 'Borman', \%srchash) or die 'unable to tie %srchash';
# attempt to store data directly into stored hash
$outer{'miko'}->{'email'} = 'whatever@whatever.com';
# [B]
# if we retrieve the hashref into a scalar,
# THEN modify the hash, there is no error
# $record = $outer{'miko'};
# $record->{'email'} = 'whatever@whatever.com';
------------- end cut ----------------
In the [A] block above, we start by tying %srchash to Tie::StdHash. I
found that it doesn't seem to matter what %srchash is tied to... if
it's tied then the error occurs, otherwise no error occurs.
In the next block the code ties %outer to our example module, Borman,
passing a reference to %srchash. The next block retrieves the 'miko'
element, which should be a hash ref, and attempts to assign a value to
one of the elements of the hash referenced by the return value. I've
found that if the value returned from %outer is first stored in a
scalar, and THEN the referenced hash is modified, no error occurs. See
[B] for (commented out) code that implements this kludge.
OK, now let's look at the module:
------------- begin cut ----------------
package Borman;
use strict;
sub TIEHASH {
my ($class, $source) = @_;
my $self = bless({}, $class);
$self->{'source'} = $source;
return $self;
}
sub FETCH {
my ($self, $key) = @_;
my (%record, $raw);
# [C]
# if we eval the following statement, there is no error
# but if it is run straight, we get the error
# eval '$raw = $self->{\'source\'}->{$key}';
$raw = $self->{'source'}->{$key};
# I use the following line to assure myself that
# we are getting to this line even though the
# error eventually occurs. We do get to this line.
# print '%record: ', \%record, "\n";
return \%record;
}
1;
------------- end cut ----------------
Obviously this isn't a complete tied hash module, but it serves to
show the problem. This module does two things. TIEHASH instantiates
the object underlying the tied hash. It accepts two arguments: the
class name, and a reference to another hash (which we'll call the
"source hash", since the intention is that the source of the data is
stored in that passed in hash). The object holds on to a reference to
the source hash.
In FETCH, the code does just a few, innocent seeming actions. It
accepts two arguments which are stored in $self and $key as usual in
FETCH. But next comes one of the problem areas. See [C] above. In
the real-world production version of this module, the code needs to
pull out the value from the source hash that was passed in to and
stored in TIEHASH. In my original module, the code simply retrieves
the hash element like this:
$raw = $self->{'source'}->{$key};
In the example above, that's all that is done with that data. $raw is
never used again. Again, obviously in the real world the routine
would do something with the data, but it's not necessary to reproduce
the problem. I eventually found that if I ran that line of code in an
eval then I don't get the error. See the (commented out) code in the
[C] block.
The routine then simply returns a reference to a plain hash.
So there's the bug. If any one of three kludges are implemented, no
error occurs. In my project I simply eval the assignment from the
source hash, so my project is proceeding forward. However, this is a
weird problem. Is this a bug in perl?
------------------------------
Date: Sun, 16 Mar 2003 15:48:12 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Strange problem with tied hashes (long)
Message-Id: <3E74E30C.E50F85B4@earthlink.net>
Miko O'Sullivan wrote:
>
> Strange problem with tied hashes (long)
>
> I've run into an interesting problem working with tied hashes.
> Although I've managed to kludge a solution, it's a sufficiently
> strange problem that it would be worthwhile to understand the cause of
> it. I don't know if the problem is a bug in perl or in my code. I've
> produced the bug in perl 5.6.1 running on NetBSD.
[snip]
> So there's the bug. If any one of three kludges are implemented, no
> error occurs. In my project I simply eval the assignment from the
> source hash, so my project is proceeding forward. However, this is a
> weird problem. Is this a bug in perl?
The problem does sound familiar -- I *think* I saw it, or something like
it, in a bug report on the perl5porters mailing list.
However, I don't recall whether it was fixed or not.
If it was, the fix should be in perl5.8.0.
PS: Another workaround would be:
if( my $t = tied %{$$self{source}} ) {
$raw = $t->FETCH($key);
} else {
$raw = $$self{source}{$key};
}
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
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 4717
***************************************