[23542] in Perl-Users-Digest
Perl-Users Digest, Issue: 5750 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Nov 4 18:05:44 2003
Date: Tue, 4 Nov 2003 15:05:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 4 Nov 2003 Volume: 10 Number: 5750
Today's topics:
Big problem with @array and Chomp ... I think :o <ducott_99@yahoo.com>
Re: Big problem with @array and Chomp ... I think :o (Tad McClellan)
Net::SSH::Perl <member47009@dbforums.com>
Re: Newbie gets Internal Server Error, among others <usenet@morrow.me.uk>
Re: Newbie gets Internal Server Error, among others <jgibson@mail.arc.nasa.gov>
Re: Newbie question - create a file <erutiurf@web.de>
Perl can't locate a .pm in @INC (Gary Hartl)
perl regex: stopping short when using .* <JohnAtYapandaDotCommercial@no.spam>
Re: perl regex: stopping short when using .* <noreply@gunnar.cc>
Problem Connecting w Perl/DBD::Oracle as SYSDBA <longwill@psmfc.org>
Re: Rounding a float in Perl? (Roy Johnson)
simplify this if loop <fJogham@yahoo.com>
Re: simplify this if loop <skuo@mtwhitney.nsc.com>
Re: simplify this if loop <perl@my-header.org>
Re: simplify this if loop <dperham@wgate.com>
Re: sub confusion <noreply@gunnar.cc>
Re: testing for 'undefined subroutine' <pilsl_usenet@goldfisch.at>
Re: Verbose warnings <erutiurf@web.de>
Re: what is @$? <syscjm@gwu.edu>
Re: what is @$? <bart.lateur@pandora.be>
Re: what is @$? <uri@stemsystems.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 04 Nov 2003 19:22:55 GMT
From: "Robert TV" <ducott_99@yahoo.com>
Subject: Big problem with @array and Chomp ... I think :o
Message-Id: <j0Tpb.290847$6C4.168943@pd7tw1no>
Hi Everyone,
I'm hoping that you might be able to shed some light on a major problem I am
having. I have been working on a few simple lines of code for over 8 hours
and cannot for the life of me figure it out. I'll start off by explaining
what my intent is for the script to do. The big program I have been writing
sends out emails for our employees. My problem has to do with 2 specific
modules of the program.
a) I have an address book module. This is a Web form with two inputs ... a
name input and an email input. Here I can add entires one at a time to the
address book text file.
b) I have a CSV content module. This is a Web form with a text area input. I
can take the contents of a CSV file, paste it into the textarea, and the
data gets saved to the address book text file.
The first module is working correctly. I use the "|" character to separate
names and emails on a single line. Here is the code I use to add the entry
to the address book text file, and then display it on a web page:
<-- Start Code Example -->
$definedname = CGI::param('name');
$definedemail = CGI::param('email');
$newrecipient = "$definedname|$definedemail\n";
open (ADDRESSES, ">>$activeuser/addressbook.txt") or die "Can't open file:
$!";
flock (ADDRESSES, LOCK_EX) or die "Can't lock file: $!";
print ADDRESSES $newrecipient;
close(ADDRESSES);
<-- End Code Example -->
Notice I use the >> open type to "append" data into the addressbook so that
entries accumulate, and I also specify to add a "\n" to $newrecipient so
that each entry is on it's own line. Below is the code I use to get the data
and display it in a Web page:
<-- Start Code Example -->
print "Content-type: text/html \n\n";
print <<PRINTHTML;
<html>
<head>
<title>Email Communications System</title>
</head>
<body>
<table border="0" cellspacing="0" width="100%" cellpadding="0">
PRINTHTML
open (ADDRESSES, "<$activeuser/addressbook.txt") or die "Can't open file:
$!";
@recipients=<ADDRESSES>;
close(ADDRESSES);
foreach $recipient(@recipients) {
chomp ($recipient);
($name,$email)=split(/\|/,$recipient);
print <<PRINTHTML;
<tr>
<td width="279" class="bodytext" height="19"> $name</td>
<td width="279" class="bodytext" height="19"> $email</td>
</tr>
PRINTHTML
}
print <<PRINTHTML;
</table>
</body>
</html>
PRINTHTML
exit;
<-- End Code Example -->
This above seems to be working correctly. The code builds a table separating
the names and email addresses. Notice I need to use the chomp ($recipient);
command so that the @array's "\n" gets removed. I talk abut the chomp
because I believe my main problem may be in this general area. Below is the
source code of the 2 entry html generated page (minus start and end HTML):
<-- Start Code Example -->
<table border="0" cellspacing="0" width="100%" cellpadding="0">
<tr>
<td width="279" class="bodytext" height="19"> Joe Smith</td>
<td width="279" class="bodytext" height="19"> joe@email.com</td>
</tr>
<tr>
<td width="279" class="bodytext" height="19"> Jnae Smith</td>
<td width="279" class="bodytext" height="19"> jane@email.com</td>
</tr>
</table>
<-- End Code Example -->
Looks to be correct. So on to the main problem ... As mentioned above I have
another section in my program where I can paste CSV data into a textarea
input. When submitted, it's supposed to completely overwrite and present
data in the address book text file. Here are some snippets from the CSV
module:
<-- Start Code Example -->
@recipients = CGI::param('csvdata');
open (FILE,">>$activeuser/csvdump.txt");
close(FILE);
open (ADDRESSES, ">$activeuser/csvdump.txt") or die "Can't open file: $!";
print ADDRESSES @recipients;
close(ADDRESSES);
open (ADDRESSES, "<$activeuser/csvdump.txt") or die "Can't open file: $!";
@recipients = <ADDRESSES>;
close(ADDRESSES);
unlink <$activeuser/csvdump.txt>;
<-- End Code Example -->
Ok, right about now your wondering why did I take the form contents, write
it to a temp file, then open and get the data back from the temp file. For
some reason I cannot get the code to work any other way. I will explain why
a bit later ... onto the CSV parsing code:
<-- Start Code Example -->
foreach $recipient (@recipients) {
chomp ($recipient);
($name,$email)=split(/,/,$recipient);
$recipient = "$name|$email\n";
}
open (ADDRESSES, ">$activeuser/addressbook.txt") or die "Can't open file:
$!";
print ADDRESSES @recipients;
close(ADDRESSES);
<-- End Code Example -->
Look reasonable right? Start a loop for each element in the @array, split
the data based on a comma, then redefine $recipient as "$name|$email\n"
Notice I replace the comma with the "|" which my recipient display module
seen about uses. I use the "|" character because sometimes people have
comma's in the name etc and that would screw up the script. So ... did the
above work correctly? When I examine the addressbook.txt file, I see all
entires I typed in the textarea, with comma's replaced, and each entry is on
its own line. So I go back to the recipients display module and this is what
is happening to the HTML source code:
<-- Start Code Example -->
<table border="0" cellspacing="0" width="100%" cellpadding="0">
<tr>
<td width="279" class="bodytext" height="19"> Joe Smith</td>
<td width="279" class="bodytext" height="19"> joe@email.com
</td>
</tr>
<tr>
<td width="279" class="bodytext" height="19"> Jnae Smith</td>
<td width="279" class="bodytext" height="19"> jane@email.com
</td>
</tr>
</table>
<-- End Code Example -->
Notice that the </td> tages that are supposed to appear directly to the
right the email address, are being forced down to their own line. This
suggests to me that the chomp is not working correctly for the CSV data and
a secret/hidden "\n" is still pressent in the $email variable. Remember it
worked fine for the module where I enter names and emails in their own input
forms individually. You might think this is a simple cosmetic annoyance but
it is causing havok on other areas of the program. Lets go over the
different recipient entry adding methods ....
a) Manual Module = I add names and emails individually into separate input
fields. (Single line input fields have no "\n"s) I join the two elements
together and add a "\n" so future entries go on their own line
"$definedname|$definedemail\n" I print the single entry to the address book
text file use the "append" operator >>. When viewing the data, the chomp
command that is used works correctly and the generated HTML is sound.
b) CSV Module = Many entires can be added at once by pasting CSV data into
the textarea. The text area data is assigned to an @array and gets written
to a temp file. The temp file is then opened and the data is "re-retrieved"
overwritting the @array's previous assignment. Through a loop, I split each
line by the comma and reassign the loops string to "$name|$email\n" ... same
format as the Manual Module. This is NOT working ... for some reason the
"\n"s are different, the display module with not remove the "\n" from the
end of the email adddress, thus the </td> html tag getting forces to a line
below. IF I do not add a "\n" to the loop in the CSV module, when I print or
view the @array data, everything is joined to a single line and I get
name|emailname|emailname|email etc etc. rather than each entry on it's own
lines as required.
Oh and about the wierd thing I do above ... assign the CSV form data to an
@array, write to a file, re open the file and get data back ... I do this
because the raw data from the textarea form will not for some reason loop
correctly in the foreach. Here are the results from each method:
Method 1 --- using the raw data. The textarea has this CSV data
Jane,jane@email.com
Mike,mike@email.ca
Kris,kris@yahoo.com
After the loop, the printed results are:
Jane|jane@email.com Mike
-- See names are missing?
Method 2 --- using data read from text file. The textarea has this CSV data
Jane,jane@email.com
Mike,mike@email.ca
Kris,kris@yahoo.com
After the loop, the printed results are:
Jane|jane@email.com
Mike|mike@email.ca
Kris|kris@yahoo.com
-- Correct layout, but is still wrong somwhow
Have you any input on what's going wrong here? After 8 hours of fighting it
I am almost giving up. I'm sorry this message got so long, I wanted to be
thourough. Thank you for your time..
Robert
------------------------------
Date: Tue, 4 Nov 2003 14:40:53 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Big problem with @array and Chomp ... I think :o
Message-Id: <slrnbqg3ml.jvg.tadmc@magna.augustmail.com>
Robert TV <ducott_99@yahoo.com> wrote:
> @recipients=<ADDRESSES>;
You can chomp() them all at once instead of in an explicit loop:
chomp @recipients;
> open (FILE,">>$activeuser/csvdump.txt");
> close(FILE);
What is the purpose of that code?
> unlink <$activeuser/csvdump.txt>;
Is there more than one file involved here?
If not, then why are you glob()ing?
> b) CSV Module = Many entires can be added at once by pasting CSV data into
> the textarea. The text area data is assigned to an @array and gets written
> to a temp file. The temp file is then opened and the data is "re-retrieved"
> overwritting the @array's previous assignment.
What is the purpose of doing that?
> Oh and about the wierd thing I do above ... assign the CSV form data to an
> @array, write to a file, re open the file and get data back ... I do this
> because the raw data from the textarea form will not for some reason loop
^^^^^^^^^^^^^^^
> correctly in the foreach.
Find out the reason, and the strangeness is likely to disappear...
> Here are the results from each method:
>
> Method 1 --- using the raw data. The textarea has this CSV data
> Jane,jane@email.com
> Mike,mike@email.ca
> Kris,kris@yahoo.com
How do you know that that is what it has?
Have you print()ed it out in your program?
Have you tried running the program from the command line?
> After the loop, the printed results are:
> Jane|jane@email.com
> Mike|mike@email.ca
> Kris|kris@yahoo.com
> -- Correct layout, but is still wrong somwhow
^^^^^^^^^^^^^^^^^^^
What is it that is "somehow" wrong?
Looks fine to me...
> Have you any input on what's going wrong here?
It is most likely related to how you are processing the textarea
data. If you can post a short and complete program that duplicates
your problem, we can probably help you solve it...
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 4 Nov 2003 20:16:14 GMT
From: Rick Thiessen <member47009@dbforums.com>
Subject: Net::SSH::Perl
Message-Id: <3559131.1067977390@dbforums.com>
I am attempting to modify a control program to use ssl communicatoins.
It is to a cisco switch and requires a login to a secondary admin
level. This
area is accessed through a password.
I have been accessing this system but using a port 23 connection
socket and need to secure the port 23.
I have tried using the Net::SSH::Perl module to push the commands
through and it works fine to the first layer of commands but is infact
connecting with every request and will not respond if there is no
newline from the remote on a password prompt.
How do I maintain a connection using ssh and how to I automate
the password check and response.
Is there a way of opening a socket connection to simulate
an interactive session with the remote ssh connection.
Rick
--
Posted via http://dbforums.com
------------------------------
Date: Tue, 4 Nov 2003 19:07:14 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Newbie gets Internal Server Error, among others
Message-Id: <bo8td2$qiv$3@wisteria.csv.warwick.ac.uk>
jessis@cobweb.net (Jessica Smith) wrote:
> I'm using Mac OS X, and I'm trying to run a script
<snip>
> It's chmod'ed 755, just like it should be, and the sh-bang line is
> correct. Yet, when I run the program in Terminal, I get:
>
> ./mail_form.cgi: =: command not found
<snap>
>
> When I hit it in Explorer, I get an Internal Server Error, and then my
> error log produces this message:
>
> [Tue Nov 4 13:21:06 2003] [error] (8)Exec format error: exec of
<snop>
The #! line is wrong.
To prove this to yourself, run it in Terminal as 'perl mail_form.cgi'
rather than './mail_form.cgi'.
Do you have a space after the #!? You shouldn't.
What line-ending are you using? I have no idea what the standard is on
OS X (BSD would dictate \012, MacOS would dictate \015) but if it is
not what the kernel wants (*probably* \012, as opposed to the \015
your editor *may* be producing) then it likely won't work. perl
doesn't care, of course.
Ben
--
The cosmos, at best, is like a rubbish heap scattered at random.
- Heraclitus
ben@morrow.me.uk
------------------------------
Date: Tue, 04 Nov 2003 11:41:40 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Newbie gets Internal Server Error, among others
Message-Id: <041120031141406371%jgibson@mail.arc.nasa.gov>
In article <89ed09a8.0311041052.5f924b64@posting.google.com>, Jessica
Smith <jessis@cobweb.net> wrote:
> I'm using Mac OS X, and I'm trying to run a script I copied from a
> tutorial. The script is online at
> http://www.elanus.net/cgi/examples.cgi/view/ex_0302.txt
>
> It's chmod'ed 755, just like it should be, and the sh-bang line is
> correct. Yet, when I run the program in Terminal, I get:
>
> ./mail_form.cgi: =: command not found
> ./mail_form.cgi: =: command not found
> ./mail_form.cgi: =: command not found
> ./mail_form.cgi: =: command not found
> ./mail_form.cgi: =: command not found
> ./mail_form.cgi: line 18: syntax error near unexpected token `qw(:'
> ./mail_form.cgi: line 18: `use CGI qw(:standard);'
> [Jessica-Smiths-Computer:/library/webServer/cgi-executables] jessicas%
>
> When I hit it in Explorer, I get an Internal Server Error, and then my
> error log produces this message:
>
> [Tue Nov 4 13:21:06 2003] [error] (8)Exec format error: exec of
> /Library/WebServer/CGI-Executables/mail_form.cgi failed
> [Tue Nov 4 13:21:06 2003] [error] [client 192.168.7.33] Premature end
> of script headers: /Library/WebServer/CGI-Executables/mail_form.cgi
>
> I've searched newsgroups, Perl references, CGI references and 2
> dead-tree Perl manuals, and I still have no idea what is causing the
> problem.
>
> Please help! Thanks.
Sorry, but it works for me! I downloaded the script, changed the first
line to "!#/opt/perl/bin/perl -w", which is where my perl is installed,
and ran the script from the command line and the installed Apache web
server under Mac OS 10.2.8 and perl 5.8.0. What version of Mac OS and
perl are you using?
I sure looks like the she-bang line is not pointing at the correct perl
executable. Are you sure it is correct?
------------------------------
Date: Tue, 04 Nov 2003 23:01:35 +0100
From: Richard Voss <erutiurf@web.de>
Subject: Re: Newbie question - create a file
Message-Id: <bo97jp$ain$06$1@news.t-online.com>
Blue Cat wrote:
> After toiling over "open" in the Perl docs and the Camel Book with no
> success, I am asking for help:
>
> How do I create a file named "dogs.txt" and write "My dog is a golden
> retriever." into it?
>
>
Because it's so much fun, another version. I prefer it like that:
my $file = 'dogs.txt';
open my $fh,'>', $file
or die "Could not open '$file': $!\n";
print $fh "My dog is a golden retriever."
close $fh
or die "Could not write to '$file': $!\n";
See
$ perldoc -f open
$ perldoc -f close
$ perldoc perlopentut
--
sub{use strict;local$@=sub{select($,,$,,$,,pop)};unshift@_,(45)x 24,split q=8==>
55.52.56.49.49.55.56.49.49.53;do{print map(chr,@_[0..(@_/2-1)]),"\r";$@->(1/6)=>
push@_=>shift}for@_,++$|}->(map{$_+=$_%2?-1:1}map ord,split//,'u!`onuids!Qdsm!'.
'i`bjds') #my email-address is reversed! <http://fruiture.de>
------------------------------
Date: 4 Nov 2003 13:33:28 -0800
From: the-smtpguy@cogeco.ca (Gary Hartl)
Subject: Perl can't locate a .pm in @INC
Message-Id: <99e21f6f.0311041333.1b436e32@posting.google.com>
Hello all again,
I'm not much of a perl programer but I am trying to get this script to
run and it is giving me a headache :)
I'm getting premature end of script header errors and I'm pretty sure
why.
Here is everything I think is important for this situation, if i'm
missing something please let me know and I'll supply anything else.
I have some .pm files ina directory called /home/mach10/dist/lib
the first line of the script calls
use lib ($ENV{'MACH10_LIB'} || '/home/mach10/dist/lib');
then it calls the offending module:
use Skyweb;
seems like that should work for me ( with my limited knowledge )
now if i try to run the script from browswer i get the follow error in
my apache logs:
Can't locate Skyweb.pm in @INC (@INC contains: /home/mach10/dist/lib
/usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0
/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi
/usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl
/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi
/usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at
/usr/local/apache/cgi-bin/sp/mach10.pl line 4.
BEGIN failed--compilation aborted at
/usr/local/apache/cgi-bin/sp/mach10.pl line 4.
[Tue Nov 4 16:23:16 2003] [error] [client 192.168.1.101] Premature
end of script headers: /usr/local/apache/cgi-bin/sp/mach10.pl
I'm so novice to perl that i'm completely lost....anyone have any
ideas.
Thanks for all the help in advance.
Gary
------------------------------
Date: Tue, 04 Nov 2003 20:05:42 GMT
From: "john s" <JohnAtYapandaDotCommercial@no.spam>
Subject: perl regex: stopping short when using .*
Message-Id: <qETpb.772$156.517437543@newssvr30.news.prodigy.com>
I have a string such as:
blah blah <asl*&23fcLK> blah blah
the < and > will always be on the end, IF they are present at all, and
anything can be in between them.
How can I capture everything in between without the < and > if they even
exist?
I tried variations of:
[<\s]+(.*(?=>)?)\s+ and also tried using [^>] in place of (?=>) and
[<\s]+(.*)[<\s]+
but everytime i use the .* it always picks up the > symbol at the end, and
I can get it to stop there, but i cannot get it to exclude the >
So, how can I tell it to "get everything, including symbols, and stop just
before > if it exists, or stop before the space, if > doesnt' exist"?
I am stuck and missing something here, please help.
Thanks,
-John
------------------------------
Date: Tue, 04 Nov 2003 21:50:28 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: perl regex: stopping short when using .*
Message-Id: <bo946m$1a47jr$1@ID-184292.news.uni-berlin.de>
john s wrote:
> I have a string such as:
> blah blah <asl*&23fcLK> blah blah
> the < and > will always be on the end, IF they are present at all,
> and anything can be in between them.
> How can I capture everything in between without the < and > if they
> even exist?
If you don't know whether the < and > characters are there, what is it
that characterizes the part you want to capture? Before we know that,
we can't help you write the regular expression.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Tue, 04 Nov 2003 14:09:59 -0800
From: J R Longwill <longwill@psmfc.org>
Subject: Problem Connecting w Perl/DBD::Oracle as SYSDBA
Message-Id: <bo9807$97a$1@nntp.psmfc.org>
We have two Oracle instances running on one server (say, instance AA and
instance BB). It is a Linux Red-Hat Advanced Server 2.1 system running
Oracle 9.2.0.4.
I've got a perl program which is trying to connect to the databases as
user sys, as sysdba.
Some excerpts from the perl program are here..
----------------------
...
use DBI;
use DBD::Oracle qw(:ora_session_modes);
use diagnostics;
use strict;
my $db_name = "AA"; # or "BB"
my $user = "sys";
my $passwd = "pswd123";
$ENV{ORACLE_SID} = $db_name;
...
my $dbh = DBI->connect("dbi:Oracle:".$db_name ,$user ,$passwd, {
ora_session_mode => ORA_SYSDBA,
RaiseError => 1,
AutoCommit => 0 }
)
or die "Can't open $db_name database: $DBI::errstr";
$dbh->commit();
... <do some SELECT statements, etc...>
$sth->finish();
$dbh->disconnect;
...
----------------------
The crazy thing is, my program (bk3.pl) WORKS for one database (AA) but
NOT for the other (database BB).
I have carefully examined the differences between the two
database/instances and can find none of consequence.
The error I get when running this for database BB seems to be occuring
in the DBI library, at the exact point of connection to the database.
The error is:
Uncaught exception from user code:
DBI connect('bb','sys',...) failed: at ./bk3.pl line 80
Carp::croak('DBI connect(\'bb\',\'sys\',...) failed: ') called
at /usr/lib/perl5/site_perl/5.6.1/i386-linux/DBI.pm line 579
DBI::__ANON__() called at
/usr/lib/perl5/site_perl/5.6.1/i386-linux/DBI.pm line 629
DBI::connect('DBI', 'dbi:Oracle:bb', 'sys', 'pswd123',
'HASH(0x8335710)') called at ./bk3.pl line 80
The main error seems to be line 579 of
/usr/lib/perl5/site_perl/5.6.1/i386-linux/DBI.pm but I can't be sure.
I have tried many variations of the syntax on the connect statement. I
have tried making changes to the tnsnames.ora file and restarting the
Oracle listener several times. I have tried the alternate syntax which
bypasses/ignores the Oracle listener process. But in each scenario, the
program often works for instance AA, but never for instance BB.
Note however that the connect DOES work for both AA and BB when I try to
connect as a regualr user and NOT as "sys". So..
Something is wrong when connecting as "sys" on any instance
other than "AA" with the sysdba directive:
"ora_session_mode => ORA_SYSDBA"
as obtained from the DBD::Oracle library.
SO.. I'm stumped! Does anyone have some ideas on this particular
situation? I am sure that we are using the latest DBD::Oracle library,
version 1.14.
Many thanks for your assistance.
--Jim Longwill :^)
------------------------------
Date: 4 Nov 2003 14:02:08 -0800
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: Rounding a float in Perl?
Message-Id: <3ee08638.0311041402.670ae4a3@posting.google.com>
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message news:<bo8rm2$n4$1@mamenchi.zrz.TU-Berlin.DE>...
> What is the mathematically accurate result of rounding 0.05 to one
> decimal place?
Convention says it rounds up. Maybe that's not mathematics, per se.
Maybe it's not even a universal convention, but it's the only one I
ever heard.
> However, your solution now differs from sprintf when rounding 0.5 to
> an integer: sprintf says 0, stround says 1.
I'm actually happy with that, because it's consistent and
conventional. sprintf's inconsistent rounding is lamented in the FAQ.
> If I had to *prove* an implementation conforms to some standard, I'd
> prefer a numeric solution
Except for the fact that numeric representation is the problem. Using
the strings is the workaround. As an internal number, there may be no
such thing as exactly 3.005, but as a string, there is.
------------------------------
Date: Wed, 05 Nov 2003 06:34:53 +1100
From: Fred <fJogham@yahoo.com>
Subject: simplify this if loop
Message-Id: <3FA7FF5D.4030405@yahoo.com>
my ( $i ,$m ) = ( 0, 2 );
my @group;
foreach my $item ( @data ) {
$i++;
my ( $tot, $avg );
if ( @group == $m ) {
push @group, $item;
shift @group;
foreach my $j ( @group ) { $tot += $j };
$avg = $tot/$m;
print $i, " ", $item, " ", $avg, "\n";
} else {
push @group, $item;
if ( @group == $m ) {
foreach my $j ( @group ) { $tot += $j };
$avg = $tot/$m;
print $i, " ", $item, " ", $avg, "\n";
}
}
}
there is duplicates and I feel there is better way to write a faster
more cleaner code.
thanks for any suggestions
------------------------------
Date: Tue, 4 Nov 2003 12:44:43 -0800
From: Steven Kuo <skuo@mtwhitney.nsc.com>
Subject: Re: simplify this if loop
Message-Id: <Pine.GSO.4.21.0311041243110.851-100000@mtwhitney.nsc.com>
On Wed, 5 Nov 2003, Fred wrote:
> my ( $i ,$m ) = ( 0, 2 );
> my @group;
> foreach my $item ( @data ) {
> $i++;
> my ( $tot, $avg );
> if ( @group == $m ) {
> push @group, $item;
> shift @group;
> foreach my $j ( @group ) { $tot += $j };
> $avg = $tot/$m;
> print $i, " ", $item, " ", $avg, "\n";
> } else {
> push @group, $item;
> if ( @group == $m ) {
> foreach my $j ( @group ) { $tot += $j };
> $avg = $tot/$m;
> print $i, " ", $item, " ", $avg, "\n";
> }
> }
> }
>
> there is duplicates and I feel there is better way to write a faster
> more cleaner code.
>
> thanks for any suggestions
Perhaps something like:
my @data = (1, 1, 2, 2, 3, 3);
my @group = @data;
my $m = 2;
for my $index (0 .. @data-$m) {
@group = @data[$index .. $index+$m-1]; # array slice
my $avg;
$avg += $_ for (@group);
$avg /= $m;
print $index+$m, " $group[-1] $avg\n";
}
--
Hope this helps,
Steven
------------------------------
Date: Tue, 04 Nov 2003 21:57:28 +0100
From: Matija Papec <perl@my-header.org>
Subject: Re: simplify this if loop
Message-Id: <qk4gqvs377vpm8pmjc08j0qks6pr5cbpbt@4ax.com>
X-Ftn-To: Fred
Fred <fJogham@yahoo.com> wrote:
> push @group, $item;
> if ( @group == $m ) {
> foreach my $j ( @group ) { $tot += $j };
> $avg = $tot/$m;
> print $i, " ", $item, " ", $avg, "\n";
> }
> }
>}
>
>there is duplicates and I feel there is better way to write a faster
>more cleaner code.
I'm not sure if it's faster but looks cleaner, and hope it does the same
thing..
my ( $i ,$m ) = ( 0, 2 );
my @group;
foreach my $item ( @data ) {
$i++;
my ( $tot, $avg );
push @group, $item;
if (@group-1 == $m) { shift @group }
if (@group == $m) {
foreach my $j ( @group ) { $tot += $j };
$avg = $tot/$m;
print $i, " ", $item, " ", $avg, "\n";
}
}
--
Matija
------------------------------
Date: 04 Nov 2003 16:48:43 -0500
From: Doug Perham <dperham@wgate.com>
Subject: Re: simplify this if loop
Message-Id: <81d6c724as.fsf@wgate.com>
Fred <fJogham@yahoo.com> writes:
> my ( $i ,$m ) = ( 0, 2 );
> my @group;
> foreach my $item ( @data ) {
> $i++;
> my ( $tot, $avg );
> if ( @group == $m ) {
> push @group, $item;
> shift @group;
> foreach my $j ( @group ) { $tot += $j };
> $avg = $tot/$m;
> print $i, " ", $item, " ", $avg, "\n";
> } else {
> push @group, $item;
> if ( @group == $m ) {
> foreach my $j ( @group ) { $tot += $j };
> $avg = $tot/$m;
> print $i, " ", $item, " ", $avg, "\n";
> }
> }
> }
>
> there is duplicates and I feel there is better way to write a faster
> more cleaner code.
>
> thanks for any suggestions
This is a little overkill if $m is always 2, but it should be faster.
#!/usr/bin/perl -w
use strict;
my ( $i, $m ) = ( 0, 2 );
my @group = (0) x $m;
my $total = 0;
while ( @data )
{
my $item = shift(@data);
$total -= shift(@group);
$total += $item;
push(@group, $item);
$i++;
printf("%3d %f %f\n", $i, $item, $total/$m);
}
--
Doug Perham o{..}o
dperham@wgate.com moo! (oo)___
WorldGate Communications, Inc. (______)\
/ \ / \
------------------------------
Date: Tue, 04 Nov 2003 19:55:01 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: sub confusion
Message-Id: <bo8tdm$1bbm8b$1@ID-184292.news.uni-berlin.de>
walterg@example.com wrote:
> Still haven't figured out TimeToNum() though.
> Given $time = "12:34:56";
>
> sub TimeToNum {
> $_ = shift;
> s/://g;
> }
> TimeToNum($time)
>
> returns "2"
Correct. (See previous post.)
> whereas
>
> $time =~ s/://g;
>
> returns the expected "123456".
No, it doesn't. That expression returns 2 as well.
http://www.perldoc.com/perl5.8.0/pod/perlop.html#s-PATTERN-REPLACEMENT-egimosx
What you mean is that the value of $time gets changed to '123456'.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Tue, 4 Nov 2003 23:49:00 +0100
From: peter pilsl <pilsl_usenet@goldfisch.at>
Subject: Re: testing for 'undefined subroutine'
Message-Id: <3fa82db0$1@e-post.inode.at>
Torsten Mangner wrote:
> Hi group,
>
> i need a capability (via a skript or something) to test my skripts (at
> compile time), if all subroutines that are called in the programm are
> really defined or 'use'd.
>
If you want to avoid the program fail because subroutines are not defined,
you could use an eval-statement around the part in question and query $@
for errors.
I use this method if calling methods defined in configuration-files.
peter
--
peter pilsl
pilsl_usenet@goldfisch.at
http://www.goldfisch.at
------------------------------
Date: Tue, 04 Nov 2003 22:57:26 +0100
From: Richard Voss <erutiurf@web.de>
Subject: Re: Verbose warnings
Message-Id: <bo97c0$9ra$06$1@news.t-online.com>
Greg Bacon wrote:
> In article <3fa6ba78$0$1100$3c090ad1@news.plethora.net>,
> Seebs <seebs@plethora.net> wrote:
>
> : Is there any way to find out WHICH variable is uninitialized, short of
> : breaking a line up into bunches of single lines?
> : [...]
>
> Here's a start:
that will only work with package variables, not with lexicals.
I recommend the debugger.
--
sub{use strict;local$@=sub{select($,,$,,$,,pop)};unshift@_,(45)x 24,split q=8==>
55.52.56.49.49.55.56.49.49.53;do{print map(chr,@_[0..(@_/2-1)]),"\r";$@->(1/6)=>
push@_=>shift}for@_,++$|}->(map{$_+=$_%2?-1:1}map ord,split//,'u!`onuids!Qdsm!'.
'i`bjds') #my email-address is reversed! <http://fruiture.de>
------------------------------
Date: Tue, 04 Nov 2003 14:30:58 -0500
From: Chris Mattern <syscjm@gwu.edu>
Subject: Re: what is @$?
Message-Id: <3FA7FE72.1090704@gwu.edu>
Abigail wrote:
> James Gilbert (jgrg@sanger.ac.uk) wrote on MMMDCCXVII September MCMXCIII
> in <URL:news:bo8k15$fes$1@niobium.hgmp.mrc.ac.uk>:
> ?? I recently found this piece of code in one of my scripts:
> ??
> ?? eval{
> ?? $row->SequenceInfo->drop_Sequence;
> ?? };
> ?? if (@$){
> ?? print "ERROR dropping sequence\n$@";
> ?? }
> ??
> ?? ie: I had meant to put "$@", but typed "@$" in the condition.
> ??
> ?? What does perl interpret "@$" as? I can't see any note of
> ?? a special array called "$". It doesn't produce any warnings,
> ?? and compiles under strict.
>
>
> It refers to the array '$'. There's no note of it because it's
> not special. It isn't any different than '@toodle_poops'.
>
But his question, which Anno answered, was basically, "If it's
not special, why didn't strict whack me for not declaring it?"
Chris Mattern
------------------------------
Date: Tue, 04 Nov 2003 20:23:24 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: what is @$?
Message-Id: <9h2gqvgbte6p05k067dapjv9vfgu8vkl8k@4ax.com>
Chris Mattern wrote:
>But his question, which Anno answered, was basically, "If it's
>not special, why didn't strict whack me for not declaring it?"
Because they're accepted based on the syntax of their name, irrespective
of whether they're used by Perl or not. You can call it a hole in
strict, I think that is correct.
OTOH I think that having to declare @ISA is nothing but ridiculous:
despite its normal syntax for its name, it is used by Perl, and is
special.
--
Bart.
------------------------------
Date: Tue, 04 Nov 2003 21:44:28 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: what is @$?
Message-Id: <x7znfbhkqs.fsf@mail.sysarch.com>
>>>>> "BL" == Bart Lateur <bart.lateur@pandora.be> writes:
BL> OTOH I think that having to declare @ISA is nothing but ridiculous:
BL> despite its normal syntax for its name, it is used by Perl, and is
BL> special.
use base ;
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
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 5750
***************************************