[19150] in Perl-Users-Digest
Perl-Users Digest, Issue: 1345 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jul 20 21:05:30 2001
Date: Fri, 20 Jul 2001 18:05:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <995677509-v10-i1345@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 20 Jul 2001 Volume: 10 Number: 1345
Today's topics:
Re: about image::Magick quality. <stephh@nospam.com>
Determing if a variable is a number? (Stan Brown)
Re: dumb question gls@byu.edu
FAQ: Where can I learn about CGI or Web programming in <faq@denver.pm.org>
fetchall_arrayref (cmavroudis)
Function that returns the number of rows in a hash? (Ken Morris)
Re: Function that returns the number of rows in a hash? (John Almberg)
localtime() returns GMT <Patrick_member@newsguy.com>
Re: localtime() returns GMT <Patrick_member@newsguy.com>
Re: localtime() returns GMT (Craig Berry)
NET::POP3 Help plz <spikey@telia.com>
Re: New to PERL/CGI question (el)
Problem wih DBD::Sybase 09.1- Set Autocommit fails afte (MunishK)
Re: read csv <bloke6789@hotmail.com>
Re: Redirecting STDERR <ddunham@redwood.taos.com>
redirecting system() call output into variable (Graham)
Re: Regex help <krahnj@acm.org>
Repetitive if statements <greenbd@u.washington.edu>
Re: Repetitive if statements <marshall@chezmarshall.com>
Re: skipping a duplicate (Troy Lachinski)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 21 Jul 2001 02:17:28 +0200
From: stephh <stephh@nospam.com>
Subject: Re: about image::Magick quality.
Message-Id: <210720010217283654%stephh@nospam.com>
Got it, thanks!
I didn't see the @_ in your list!
And then you call it like that :
mysub ( 'foo' => 'bar', 'anything' => 'really' );
I'm not used to this approch..
But I'm shure moving to it
cause I guess it makes subs easier to read..
I often write wery small subs with 1 or 2 args,
so $arg = shift; is very practical...
Anyway, I'm wandering whether you miss quotes around hash keys ?
Thanks,
Stephh
In article <3B54B2F6.1257C3E7@home.com>, Michael Carman
<mjcarman@home.com> wrote:
> stephh wrote:
> >
> > In article <3B54586F.D8109779@home.com>, Michael Carman
> > <mjcarman@home.com> wrote:
> >
> > > sub mysub {
> > > my %args = (
> > > foo => 'alpha',
> > > bar => 'beta',
> > > baz => 42,
> > > @_,
> > > );
> > >
> > > print "foo/bar/baz = $arg{foo}/$arg{bar}$arg{baz}\n";
> > > }
> >
> > OK, but I do't get how you pass the values to %args. Can you write
> > down the line getting @_ ?
>
> The my %args = (...) *is* the line getting @_. Admittedly, I thought
> that this was fiendishly clever the first time I saw it, so I can
> understand some initial confusion.
>
> You're probably used to seeing hashes intialized using the "fat comma"
> or "comma arrow" syntax. Syntactically, => is just a comma. The only
> thing special about it is that it will automatically quote barewords on
> the left side. So saying this:
>
> my %args = (foo => 'alpha');
>
> is no different than saying this:
>
> my %args = ('foo', 'alpha');
>
> That right side of the second one looks a lot like a list, doesn't it?
> Well, it is. When Perl turns a list into a hash, it turns each pair of
> elements (at indices 0/1, 2/3, etc.) into key/value pairs. If you go the
> other direction and flatten a hash, Perl will turn each key/value pair
> into adjacent array elements.
>
> Once you realize that you can represent a hash with a list, you can also
> write the above as:
>
> my @array = ('foo', 'alpha');
> my %args = @array;
>
> It looks like it should be a error -- trying to copy an array into a
> hash -- but it's not. Perl treats @array as a flattened hash and copies
> it into %args as such. This is what we're doing in the sub above, except
> that we're also specifying default values for anything that's not given
> in the array passed in. The my %args = (...) copies the contents of @_
> into the hash as key/value pairs.
>
> Maybe an example will help. Let's say that you call mysub(foo => 'zap').
> The first line of the sub will be parsed like this:
>
> my %args = (
> foo => 'alpha',
> bar => 'beta',
> baz => 42,
> foo => 'zap', # This is the contents of @_
> );
>
> Notice how there are two key/value pairs for foo? The first one is the
> default. The second is the one provided in @_. By placing @_ at the end
> of the initialization of %args, anything it contains will overwrite the
> defaults hard-coded above it.
>
> -mjc
--
$tephh;
Don't forget to visit http://www.grc.com/dos/grcdos.htm
------------------------------
Date: 20 Jul 2001 20:24:18 -0400
From: stanb@panix.com (Stan Brown)
Subject: Determing if a variable is a number?
Message-Id: <9jai3i$nrq$1@panix3.panix.com>
I'm writing a small PerlTK script to allow a user to edit some Oracle
tables. The user will input data using PerlTK Entry widgets, and the script
will then create the resultant SQL.
Since numbers don't need to be quoted in SQL, and strings do, I need o
figure out which of these variables are numbers.
I looke in the Perl cookbook, and came up with a solutin using //, but it
complains when I hand it a non numeric value.
How can I make this work withou perl complaining?
------------------------------
Date: 20 Jul 2001 16:26:48 -0600
From: gls@byu.edu
Subject: Re: dumb question
Message-Id: <uy9pj8cev.fsf@SNOW.i-did-not-set--mail-host-address--so-shoot-me>
tiltonj@erols.com (Jay Tilton) writes:
> On 19 Jul 2001 16:19:45 GMT, anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
> wrote:
>
> >According to Jay Tilton <tiltonj@erols.com>:
> >> On Thu, 19 Jul 2001 10:56:24 -0400, David Coppit <newspost@coppit.org>
> >> wrote:
> >> >Won't that bias the elements of @ary to be high? In other words, it's
> >> >pretty unlikely that you'll get elements with the value of 1.
> >>
> >> As long as each value in the list is scaled by an identical factor, the
> >> probability that some values will appear more or less frequently than others
> >> does not change. The bias is the same as for the rand() function.
> >
> >That doesn't follow. In effect, you are creating (equally distributed)
> >random numbers in a range that is itself random [ 0, 1/$ary_sum ).
> >The distribution of $ary_sum is, uh... a binomial distribution, I
> > think.
No, the distribution does not have a name, but if you add together
enough uniform random numbers then the distribution can be
approximated with a normal distribution (Gaussian, bell-curve), where
enough means more than 5.
> >It would also change if one were to use rand( $some_number) instead
> >of the implicit rand( 1).
No (other than changes due to round off error), essentially you will
be multiplying and dividing by $some_number canceling it out to give
the same answer.
> >I'd say the final distribution needs some
> >more analysis.
If we want to generate the numbers so that every possible combination
of 10 numbers has equal probability of being generated
(i.e. 10,10,10,10,10,10,10,10,10,10; 100,0,0,0,0,0,0,0,0,0; and
0,0,0,100,0,0,0,0,0,0 all have the same chance of being the final set)
then we want a specific form of the Dirichlet distribution.
For lots of gory details on generating data from a general Dirichlet
distribution, see:
@article{nara:1990,
Author = {Narayanan, A.},
Title = {Computer Generation of {D}irichlet Random Vectors},
Year = 1990,
Journal = {Journal of Statistical Computation and Simulation},
Volume = 36,
Pages = {19--30}
}
Luckily, we only need a small modification to the script to make it
generate data from the "Uniform" Dirichlet. Replace rand with
-log(1-rand) (this makes it so we are generating numbers from an
exponential distribution (a form of the gamma distribution) instead of
the uniform and dividing a set of gamma random numbers by their sum
gives a Dirichlet).
Another option would be to check to see if the sum of the 10 uniform
random numbers was below 1 and regenerate all 10 numbers if the sum is
greater than 1, but that would really slow things down.
>
> The analogy with rand($some_number) and even distribution within a set are
> the points I should have emphasized. Within a single set I don't see a
> problem, but the combined results from two or more sets will most definitely
> show bias.
I made some changes to the below code to remove the bias:
> #!perl -w
> use strict;
>
> my %nums_count;
> for (1..100000) {
> $nums_count{int $_}++ foreach rand_sum(10, 100);
> }
>
> printf "%2d: %s\n", $_, '*'x($nums_count{$_} / 2000)
> foreach (sort { $a <=> $b } keys %nums_count );
replace the above 2 lines with:
my $string;
foreach (sort { $a <=> $b } keys %nums_count ){
$string = ' 'x70;
substr $string, 0, $nums_count{$_} / 2000 - 1, '*'x($nums_count{$_} / 2000);
substr( $string, (5*9*(1-$_/100)**8)-1,1,'|')
if((5*9*(1-$_/100)**8)-1 < 70) ;
printf "%2d: %s\n", $_, $string;
}
This prints out your histogram, but includes the theoretical values
for comparison (the |s)
>
> sub rand_sum {
> my ($num_elements, $desired_sum) = @_;
> my (@ary, $ary_sum);
> for (1..$num_elements) {
> push @ary, rand;
replace the above line with:
push @ary, - log( 1 - rand );
> $ary_sum += $ary[-1];
> }
> map { $_ * $desired_sum / $ary_sum } @ary;
> }
>
> Sample output:
> 0: **********************
> 1: ***********************
> 2: ***********************
[snip]
Just out of curiosity, what did you expect the output to look like if
there were no bias?
--
Greg Snow, PhD Office: 223A TMCB
Department of Statistics Phone: (801) 378-7049
Brigham Young University Dept.: (801) 378-4505
Provo, UT 84602 email: gls@byu.edu
------------------------------
Date: Sat, 21 Jul 2001 00:18:03 GMT
From: PerlFAQ Server <faq@denver.pm.org>
Subject: FAQ: Where can I learn about CGI or Web programming in Perl?
Message-Id: <%S367.26$zH9.188448768@news.frii.net>
This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with every Standard Distribution of
Perl.
+
Where can I learn about CGI or Web programming in Perl?
For modules, get the CGI or LWP modules from CPAN. For textbooks, see
the two especially dedicated to web stuff in the question on books. For
problems and questions related to the web, like ``Why do I get 500
Errors'' or ``Why doesn't it run from the browser right when it runs
fine on the command line'', see these sources:
WWW Security FAQ
http://www.w3.org/Security/Faq/
Web FAQ
http://www.boutell.com/faq/
CGI FAQ
http://www.webthing.com/tutorials/cgifaq.html
HTTP Spec
http://www.w3.org/pub/WWW/Protocols/HTTP/
HTML Spec
http://www.w3.org/TR/REC-html40/
http://www.w3.org/pub/WWW/MarkUp/
CGI Spec
http://www.w3.org/CGI/
CGI Security FAQ
http://www.go2net.com/people/paulp/cgi-security/safe-cgi.txt
-
Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.
If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.
Answers to questions about LOTS of stuff, mostly not related to
Perl, can be found by pointing your news client to
news:news.answers
or to the many thousands of other useful Usenet news groups.
Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.
The perlfaq manual page contains the following copyright notice.
AUTHOR AND COPYRIGHT
Copyright (c) 1997-1999 Tom Christiansen and Nathan
Torkington. All rights reserved.
This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.
03.27
--
This space intentionally left blank
------------------------------
Date: 19 Jul 2001 04:28:38 -0700
From: costas@othermedia.com (cmavroudis)
Subject: fetchall_arrayref
Message-Id: <48dc5908.0107190328.2891041e@posting.google.com>
Im querying a db and want to retrieve an array of hashes. BUt am
having trouble and cant seem to quite get it right. This is the syntax
i am using to query and retrieve the db.
my $sql = "SELECT url, status, tag, category FROM url";
my $sth = $dbh->prepare( $sql );
$sth->execute();
my $goodrow = ();
$goodrow = $sth->fetchall_arrayref;
I am then getting an error message
"Can't coerce array into hash at addpage_details.cgi line 70. " when i
try to loop through the array as so.
my $row;
foreach $row ($goodrow)
{
$goodrow->{'status'};
}
Note: that the database does have sufficent and correct data in it.
THanks costas
------------------------------
Date: 19 Jul 2001 07:11:12 -0700
From: kenneth_i_morris@yahoo.com (Ken Morris)
Subject: Function that returns the number of rows in a hash?
Message-Id: <74e96e35.0107190611.54eb7b85@posting.google.com>
Hello:
I was wondering if there is a function that returns the number of rows
in a hash table?
Any help would be great!
Thanks in advance,
Ken Morris
------------------------------
Date: 19 Jul 2001 10:00:10 -0700
From: jalmberg@identry.com (John Almberg)
Subject: Re: Function that returns the number of rows in a hash?
Message-Id: <c396bdba.0107190900.6c6bf2f0@posting.google.com>
the function 'keys' used in a scalar context returns the number of
keys in the hash . . .
scalar(keys(%hash))
or
$count = keys(%hash);
John
kenneth_i_morris@yahoo.com (Ken Morris) wrote in message news:<74e96e35.0107190611.54eb7b85@posting.google.com>...
> Hello:
>
> I was wondering if there is a function that returns the number of rows
> in a hash table?
>
> Any help would be great!
>
> Thanks in advance,
> Ken Morris
------------------------------
Date: 20 Jul 2001 14:07:37 -0700
From: Patrick Flaherty <Patrick_member@newsguy.com>
Subject: localtime() returns GMT
Message-Id: <9ja6ip0fl2@drn.newsguy.com>
ActiveState Perl on W2000. Localtime() returns GMT while I'm in PST.
www.perl.com tells me that there's a module Time::Zone but I don't find it with
PPM. This would appear to need to be set - is that the case and how?
thanx.
pat
------------------------------
Date: 20 Jul 2001 15:16:48 -0700
From: Patrick Flaherty <Patrick_member@newsguy.com>
Subject: Re: localtime() returns GMT
Message-Id: <9jaakg0voj@drn.newsguy.com>
Curiouser and curioser. When I compare the output of localtime() with gmtime(),
I find that localtime() is in fact returning gmt+1 when in fact I'm in
California (gmt-8 or 9 I believe).
My code and its results are as follows:
use strict;
print "local ";
print scalar localtime();
print "\n";
print "gmt ";
print scalar gmtime();
L:\XX\Perl>perl localtime.pl
perl localtime.pl
local Fri Jul 20 23:15:37 2001
gmt Fri Jul 20 22:15:37 2001
L:\XX\Perl>
pat
In article <9ja6ip0fl2@drn.newsguy.com>, Patrick says...
>
>
>ActiveState Perl on W2000. Localtime() returns GMT while I'm in PST.
>www.perl.com tells me that there's a module Time::Zone but I don't find it with
>PPM. This would appear to need to be set - is that the case and how?
>
> thanx.
>
> pat
>
------------------------------
Date: Fri, 20 Jul 2001 23:29:28 -0000
From: cberry@cinenet.net (Craig Berry)
Subject: Re: localtime() returns GMT
Message-Id: <tlhfmo5e3s7343@corp.supernews.com>
Patrick Flaherty (Patrick_member@newsguy.com) wrote:
: Curiouser and curioser. When I compare the output of localtime() with gmtime(),
: I find that localtime() is in fact returning gmt+1 when in fact I'm in
: California (gmt-8 or 9 I believe).
PST is GMT-8, PDT (current) is GMT-7.
Sounds like the system has the right DST settings but the wrong default
timezone set.
--
| Craig Berry - http://www.cinenet.net/~cberry/
--*-- "Brute force done fast enough looks slick."
| - William Purves
------------------------------
Date: Fri, 20 Jul 2001 23:23:40 GMT
From: "Spikey" <spikey@telia.com>
Subject: NET::POP3 Help plz
Message-Id: <04367.6413$z21.741793@newsc.telia.net>
Hey!
Know anyone where i can find NET::POP3 examples?
Or do u maybe got some?
\ spikey
@
Hwa - Security
------------------------------
Date: 18 Jul 2001 19:44:39 -0700
From: el@wilmington.net (el)
Subject: Re: New to PERL/CGI question
Message-Id: <58929d2c.0107181844.388c5ae6@posting.google.com>
Steve <news@privacy.net> wrote in message news:<njvblt4qui3kls6s2t7cg458ert86ofgbr@4ax.com>...
> I am experimenting with the use of PERL and server side includes. My
> first investigations are getting a freebie text counter running,
> however the only output I get on my pages is: [an error occurred
> while processing this directive]
>
> If I run the script from Telnet I get this
>
> Content-type: text/html
>
> 2
>
>
> Which looks fine to me. Can anyone suggest what I may be doing wrong
> or how to find out why I am getting this error?
>
> TIA
> Steve
It could be a number of things ranging from the incredibly simple to
... well, just hard to say without seeing the code you are using and
the HTML call. First thing I would check is to make sure that your
HTML call actually points to the correct location of the script. Then
use test your script with -w (warnings on) and see if that helps
identify the error.
el
------------------------------
Date: 18 Jul 2001 20:06:53 -0700
From: mkfaltu@yahoo.com (MunishK)
Subject: Problem wih DBD::Sybase 09.1- Set Autocommit fails after prepare
Message-Id: <ef37b807.0107181906.8adf6eb@posting.google.com>
Hi
Basic
Sun Solaris 8
Sybase ASE 12.0 Client
Perl 5.6.0
DBD::Sybase 0.91
DBI 1.13
Sybase ASE 12.0 (on different Box)
I have to do a transaction after preparing a statement but before
executing them. For that I set Autocommit off, that fails giving error
that says
"panic: can't set AutoCommit with active statement handles"
Is it a bug or limitation? Can somebody suggest a work around, Idea is
to prepare the statement once and use within or without transaction.
H.E.L.P. !!
Here is test perl code
-----------------------------------------------------------------------------
use DBI;
my $dbh = DBI->connect("dbi:Sybase:SYBDEV","sa","sa",{AutoCommit =>1,
RaiseError => 1})
or die "can't connect : $DBI::errstr\n";
my $sth = $dbh->prepare("select * from MyTable");
my $commitflag = $dbh->{AutoCommit};
$dbh->{AutoCommit} = 0;
print "Autocommit : $dbh->{AutoCommit}\n";
do{
----
execute; # Prepared statement gets executed
----
commit;
else
rollback;
}something;
$dbh->{AutoCommit} = $commitflag;
print "Autocommit : $dbh->{AutoCommit}\n";
$dbh->disconnect();
exit;
------------------------------
Date: Fri, 20 Jul 2001 23:19:06 +0100
From: "Pete" <bloke6789@hotmail.com>
Subject: Re: read csv
Message-Id: <Ce267.57722$WS4.8704048@news6-win.server.ntlworld.com>
"Agoradesign.com" <info@agoradesign.com> wrote in message
news:ou9hlto8jvvb6pufmvgua0eva42d714apm@4ax.com...
> I am a newbie in perl, I am doing a mailing list form, it can write in
> a csv file correctly (I need this to imprt in outlook) put I cant make
> the confirmation page with the new person confirmation in it, neither
> make a reply auto email.
>
> here is the code I put in the html file
>
> <form method="GET" action="cgi-bin/readcsv.cgi">
>
> <template>
> <<nom>>
> </template>
>
>
> </form>
>
>
> any other idea would help
> thanks
I was a newbie last week too. Now I'm an old hand! I think you'll have to
give a bit more info or else you are going to get flamed. Your cgi script
has to take variables of some sort, these will appear in your html a bit
like this: <input type="hidden" name="fred" value="pete,joe">
<input type="text" name="mailto" value="fred@here.com">
And if your script is in perl, then tell the group what it says and you
might get some help. In fact, you'll certainly get some help 'cause these
people know their onions.
Pete
------------------------------
Date: Fri, 20 Jul 2001 22:32:42 GMT
From: Darren Dunham <ddunham@redwood.taos.com>
Subject: Re: Redirecting STDERR
Message-Id: <ek267.128$0D1.106422@news.pacbell.net>
Steve Allan <stevea@wrq.com> wrote:
>>I am trying to redirect the errors generated by one script on win32. I use
>>the following command:
>>
>> scriptname > out.txt 2>&1
>>
>>If the STDOUT is correctly redirected, none of the error messages are sent
>>to the file.
>>
>>Any idea why??
> I don't know why, but I experience the same behavior using
> ActiveState's Perl. Oddly enough, it works correctly for me if I
> invoke the interpreter directly,
> % scriptname > out.txt 2>&1 # donesn't work
> % perl scriptname > out.txt 2>&1 # works
version of Windows?
It works okay for me on W2KPro / P 5.6.1 build 626
C:\Devel>out.pl > foo.out 2>&1
C:\Devel>type foo.out
output text
error text
--
Darren Dunham ddunham@taos.com
Unix System Administrator Taos - The SysAdmin Company
Got some Dr Pepper? San Francisco, CA bay area
< How are you gentlemen!! Take off every '.SIG'!! >
------------------------------
Date: 19 Jul 2001 05:02:38 -0700
From: graham@aledrinker.co.uk (Graham)
Subject: redirecting system() call output into variable
Message-Id: <6172ee17.0107190402.77e44fa4@posting.google.com>
Hi,
I am using the system() call within perl (I am writing a simple menu
driven shell), but I wish to do something semantically the same as the
following:
$output = system("command");
Where the output of the command is placed into $output, and not just
printed on the terminal. I would like to do this in order to reformat
the output of various commands. Of course, $output in the above
example at the moment will return the return code of command, which I
don't need yet.
I have also tried the following:
open (CMD, "|command 2>&1");
@lines = <CMD>;
close(CMD);
and variants, but with no luck. A nasty hack would be to write the
output to a file, then read it back in within Perl...
Thanks for any help.
Graham
------------------------------
Date: Fri, 20 Jul 2001 23:32:38 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Regex help
Message-Id: <3B58BFEB.B74B271D@acm.org>
Ed Napier wrote:
>
> I'm trying to extract "running" from the html code below. My regex is
> just not cutting it. Can someone help?
>
> my $txt = '<html> <head> <title>LCF Daemon</title></title> <!<body
>
> [snip]
>
> (171.177.193.248+9494)</font><br> Status : <FONT
> color="#ff0000">running</font><br> Last Restart : <FONT
>
> [snip]
>
> <!END_PAGE>';
>
> $txt =~ /Status\s*\:\s*<[\w\s]+>(\w+)/g;
> #$txt =~ /(<[\w\s]+>)/;
> $status = $1;
> print "Status: $status\n";
> $txt =~ /Status\: [^<]*/; print("$&\n");
if ( $txt =~ /Status .+>(\w+)<.+ Last Restart/s ) {
$status = $1;
}
John
--
use Perl;
program
fulfillment
------------------------------
Date: Fri, 20 Jul 2001 16:44:43 -0700
From: "Brian D. Green" <greenbd@u.washington.edu>
Subject: Repetitive if statements
Message-Id: <Pine.A41.4.33.0107201643310.129254-100000@dante28.u.washington.edu>
Hello,
I'm trying to write some code that will take the results of a survey and
translate each of the five possible values of the survey into a different
graphic. I've figured out something that works, but it seems very
repetitive and inefficient. Right now, I have it doing:
if ($q1 == 1) {
$q1 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
}
if ($q2 == 1) {
$q2 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
}
if ($q3 == 1) {
$q3 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
}
if ($q4 == 1) {
$q4 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
}
if ($q5 == 1) {
$q5 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
}
if ($q6 == 1) {
$q6 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
}
if ($q7 == 1) {
$q7 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
}
if ($q1 == 2) {
$q1 = "<img src='/legsim/img/da.gif' height='18' width='180'>";
}
...and so on for each of the five values. Could someone suggest how I
might simplify it?
Thanks much,
Brian Green http://students.washington.edu/greenbd/
O-----------------------------------------------------------------------O
|"I am not ashamed of the gospel, because it is the power of God for the|
| salvation of everyone who believes: first for the Jew, then for the |
| Gentile." --Romans 1:16, NIV |
O-----------------------------------------------------------------------O
Brian Green http://students.washington.edu/greenbd/
O-----------------------------------------------------------------------O
|"I am not ashamed of the gospel, because it is the power of God for the|
| salvation of everyone who believes: first for the Jew, then for the |
| Gentile." --Romans 1:16, NIV |
O-----------------------------------------------------------------------O
------------------------------
Date: Sat, 21 Jul 2001 00:50:53 GMT
From: David Marshall <marshall@chezmarshall.com>
Subject: Re: Repetitive if statements
Message-Id: <MPG.15c29bf9a05de0f4989688@news.earthlink.net>
OK, for some moderately strong "simplification," supposing that the five
graphics are 'sd.gif', 'da.gif', 'g3.gif', 'g4.gif', and 'g5.gif'...
@images = (undef, 'sd.gif', 'da.gif', 'g3.gif', g4.gif', 'g5.gif');
$tag = q(<img src='/legsim/img/%s' height='18' width='180'>);
foreach ($q1, $q2, $q3, $q4, $q5, $q6, $q7) {
($_ >= 1) && ($_ <= @images) && ($_ = sprintf $tag, $images[$_]);
}
But wait! Put the responses in an array too...
$_ >= 1 && $_ <= @images && $_ = sprintf $tag, $images[$_] foreach @q;
Add more or less range checking as suits your knowledge of whether the
responses can contain nonsense values.
But if I'm absolutely sure that the values of @q are in the range 1 ..
scalar(@images), I can go for some rather extreme "simplification":
@q = map {sprintf $tag, $images[$_]} @q;
In article <Pine.A41.4.33.0107201643310.129254-
100000@dante28.u.washington.edu>, greenbd@u.washington.edu says...
> Hello,
>
> I'm trying to write some code that will take the results of a survey and
> translate each of the five possible values of the survey into a different
> graphic. I've figured out something that works, but it seems very
> repetitive and inefficient. Right now, I have it doing:
>
> if ($q1 == 1) {
> $q1 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
> }
> if ($q2 == 1) {
> $q2 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
> }
> if ($q3 == 1) {
> $q3 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
> }
> if ($q4 == 1) {
> $q4 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
> }
> if ($q5 == 1) {
> $q5 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
> }
> if ($q6 == 1) {
> $q6 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
> }
> if ($q7 == 1) {
> $q7 = "<img src='/legsim/img/sd.gif' height='18' width='180'>";
> }
>
> if ($q1 == 2) {
> $q1 = "<img src='/legsim/img/da.gif' height='18' width='180'>";
> }
> ...and so on for each of the five values. Could someone suggest how I
> might simplify it?
>
> Thanks much,
> Brian Green http://students.washington.edu/greenbd/
------------------------------
Date: 18 Jul 2001 22:07:39 -0700
From: troylachinski@my-deja.com (Troy Lachinski)
Subject: Re: skipping a duplicate
Message-Id: <f9490d56.0107182107.663b232@posting.google.com>
Bart,
Thanks for the point in the right direction. I also REALLY appreciate
the explanation of why this works. With your help, I finally have
everything working properly.
Thanks again.
> Skipping your code... here's how I would do this:
>
> $\ = "\n";
> print "PLU:";
> while(<DATA>) {
> my($plu, $ppy) = /^([A-Z]+)(\S+)/ or next;
> $ppy_id{$ppy} ||= keys %ppy_id;
> print "$plu$ppy_id{$ppy}";
> }
>
> print "\nPPY:";
> foreach my $ppy (sort { $ppy_id{$a} <=> $ppy_id{$b} }
> keys %ppy_id) {
> print "$ppy ($ppy_id{$ppy})";
> }
------------------------------
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 1345
***************************************