[29556] in Perl-Users-Digest
Perl-Users Digest, Issue: 800 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Aug 27 14:14:19 2007
Date: Mon, 27 Aug 2007 11:14:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Mon, 27 Aug 2007 Volume: 11 Number: 800
Today's topics:
put perl command within if-else <jiehuang001@gmail.com>
Re: put perl command within if-else <mritty@gmail.com>
Re: put perl command within if-else <jiehuang001@gmail.com>
SCP/RCP stats ? <rvlebars@hotmail.com>
Tk::getSaveFile - use specified initialfile <josef.moellers@fujitsu-siemens.com>
Using DBI, Set Select to Variable <jwcarlton@gmail.com>
Re: Using DBI, Set Select to Variable <stoupa@practisoft.cz>
Re: Using DBI, Set Select to Variable <simon.chao@fmr.com>
Re: Using DBI, Set Select to Variable <mritty@gmail.com>
Re: Using DBI, Set Select to Variable xhoster@gmail.com
Re: Why can't I access an Array like this? <bik.mido@tiscalinet.it>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 27 Aug 2007 08:15:11 -0700
From: Jie <jiehuang001@gmail.com>
Subject: put perl command within if-else
Message-Id: <1188227711.498876.71800@d55g2000hsg.googlegroups.com>
Hi,
I want to process a list. However, this list could exist as a provided
file, or in a hash created. I am thinking to use the following code,
first to find if this list exists in a file or in a hash, but this
code does NOT work.... Do I need to put "do something here" into a
function and then call this function? I am not very comfortable with
function though...
Your insight is appreciated!
jie
==============my piece of code==========
if ($list_file) {
open (LIST, "< $list_file") or die "Can't open file";
while (<LIST>) {
} else { ## this list is in a hash created previously
foreach $key (%LIST_HASH) {
}
!!!!do something here!!!;
}
------------------------------
Date: Mon, 27 Aug 2007 08:47:56 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: put perl command within if-else
Message-Id: <1188229676.428000.45350@o80g2000hse.googlegroups.com>
On Aug 27, 11:15 am, Jie <jiehuang...@gmail.com> wrote:
> I want to process a list. However, this list could exist as a
> provided file, or in a hash created. I am thinking to use the
> following code, first to find if this list exists in a file or
> in a hash, but this code does NOT work.... Do I need to put "do
> something here" into a function and then call this function?
> I am not very comfortable with function though...
> ==============my piece of code==========
> if ($list_file) {
> open (LIST, "< $list_file") or die "Can't open file";
> while (<LIST>) {} else { ## this list is in a hash created previously
>
> foreach $key (%LIST_HASH) {
You meant:
foreach my $key (keys %LIST_HASH) {
otherwise, you're iterating over both the keys and the values
>
> }
>
> !!!!do something here!!!;
> }
What you want is not possible. You cannot intersperce a while or
foreach loop within the if statement. All blocks must fully nest.
That is, one block cannot end until all blocks started within it have
ended.
So, yes, the best way to handle what you want is to create a
subroutine and call that subroutine twice, once in the while loop,
once in the foreach.
For example:
if ($list_file) {
open my $LIST, '<', $list_file or die "Can't open $list_file: $!";
while (my $line = <$LIST>) {
chomp $line;
process($line);
}
} else { ## this list is in a hash created previously
foreach my $key (keys %LIST_HASH) {
process($key);
}
}
sub process {
my $item = shift;
#do whatever you want with the hash key or file line
print "Item: $item\n";
}
If, as you say, you are not comfortable with subroutines, you should
read:
perldoc perlsub
Paul Lalli
------------------------------
Date: Mon, 27 Aug 2007 10:36:24 -0700
From: Jie <jiehuang001@gmail.com>
Subject: Re: put perl command within if-else
Message-Id: <1188236184.666511.315890@d55g2000hsg.googlegroups.com>
Thank you very much, Paul! I just learned from you that "shift" is one
to get a passed parameter!
On Aug 27, 10:47 am, Paul Lalli <mri...@gmail.com> wrote:
> On Aug 27, 11:15 am, Jie <jiehuang...@gmail.com> wrote:
>
> > I want to process a list. However, this list could exist as a
> > provided file, or in a hash created. I am thinking to use the
> > following code, first to find if this list exists in a file or
> > in a hash, but this code does NOT work.... Do I need to put "do
> > something here" into a function and then call this function?
> > I am not very comfortable with function though...
> > ==============my piece of code==========
> > if ($list_file) {
> > open (LIST, "< $list_file") or die "Can't open file";
> > while (<LIST>) {} else { ## this list is in a hash created previously
>
> > foreach $key (%LIST_HASH) {
>
> You meant:
> foreach my $key (keys %LIST_HASH) {
>
> otherwise, you're iterating over both the keys and the values
>
>
>
> > }
>
> > !!!!do something here!!!;
> > }
>
> What you want is not possible. You cannot intersperce a while or
> foreach loop within the if statement. All blocks must fully nest.
> That is, one block cannot end until all blocks started within it have
> ended.
>
> So, yes, the best way to handle what you want is to create a
> subroutine and call that subroutine twice, once in the while loop,
> once in the foreach.
>
> For example:
> if ($list_file) {
> open my $LIST, '<', $list_file or die "Can't open $list_file: $!";
> while (my $line = <$LIST>) {
> chomp $line;
> process($line);
> }} else { ## this list is in a hash created previously
>
> foreach my $key (keys %LIST_HASH) {
> process($key);
> }
>
> }
>
> sub process {
> my $item = shift;
> #do whatever you want with the hash key or file line
> print "Item: $item\n";
>
> }
>
> If, as you say, you are not comfortable with subroutines, you should
> read:
> perldoc perlsub
>
> Paul Lalli
------------------------------
Date: Mon, 27 Aug 2007 01:10:44 -0700
From: RV <rvlebars@hotmail.com>
Subject: SCP/RCP stats ?
Message-Id: <1188202244.536628.100860@g4g2000hsf.googlegroups.com>
Hello,
I've made a network application that make a syncronisation of several
computers. This app works with the "RSync" command that is based on
SSH. I would to know the difference between SCP and a simple RCP
concerning CPU load, network load, and the augmentation of the size of
a compressed packet compared to a non-compressed packet.
It is a tool to know this kind of informations ???
Thanks
RV
------------------------------
Date: Mon, 27 Aug 2007 16:58:28 +0200
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Tk::getSaveFile - use specified initialfile
Message-Id: <fauoqr$njv$1@nntp.fujitsu-siemens.com>
In an application which downloads an image from a webcam, I'd like to=20
save an image if it's interesting.
For that, I have a "Save image" button, which calls this sub:
sub save {
my $top =3D shift;
my @tv =3D localtime;
my $base =3D 'webcam-'
. sprintf('%04d.%02d.%02d', $tv[5]+1900, $tv[4]+1, $tv[3])
. '-'
. sprintf('%02d:%02d:%02d', $tv[2], $tv[1], $tv[0])
. '.pnm';
my $dst =3D $top->getSaveFile(-initialdir =3D> $HOME,
-defaultextension =3D> '.pnm',
-initialfile =3D> $base,
-title =3D> 'Save Snapshot',
-filetypes =3D> [ ['Images', '.pnm' ],
['All files', '*' ] ]);
if (defined $dst) {
print STDERR "dst=3D$dst\n";
} else {
print STDERR "dst=3D<undef>\n";
}
if (defined $dst && $dst ne "") {
copy($tmpnam, $dst) or die "Copy failed: $!";
}
}
Unfortunately, I cannot just hit the "Save" button in the getSaveFile=20
window to use the given "webcam-..." name, I have to somehow make it=20
"dirty". How can I convince the getSaveFile widget to use this=20
initialfile without further action (i.e. just hit the "Save" button)?
Josef
--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
------------------------------
Date: Sun, 26 Aug 2007 21:35:58 -0700
From: Jason <jwcarlton@gmail.com>
Subject: Using DBI, Set Select to Variable
Message-Id: <1188189358.400577.173530@22g2000hsm.googlegroups.com>
I know how to load info into an array:
my $sth = $dbh->prepare("SELECT username, password FROM users WHERE
username=?");
$sth->execute($given_username);
while (my ($userID, $passID) = $sth->fetchrow_arrayref()) { ... }
What I can't figure out is how to load it to a single variable if I
know that there's only 1 value, instead of an array. For instance,
what if I only need to read "password" instead of "username,
password"?
Currently, the only thing I have been able to find is:
my $sth = $dbh->prepare("SELECT password FROM users WHERE
username=?");
$sth->execute($given_username);
while (my ($passID) = $sth->fetchrow_arrayref()) { ... }
In this case, "username" is a PRIMARY, so it can only be found once;
meaning, "password" either equals 1 field, or nothing. It seems stupid
to use a loop when I know that there's only one value in it, but I
can't find how to pull it out otherwise.
TIA,
Jason
------------------------------
Date: Mon, 27 Aug 2007 16:09:04 +0200
From: "Petr Vileta" <stoupa@practisoft.cz>
Subject: Re: Using DBI, Set Select to Variable
Message-Id: <faum94$2khr$1@ns.felk.cvut.cz>
Jason wrote:
> Currently, the only thing I have been able to find is:
>
> my $sth = $dbh->prepare("SELECT password FROM users WHERE
> username=?");
> $sth->execute($given_username);
> while (my ($passID) = $sth->fetchrow_arrayref()) { ... }
>
>
my $sth = $dbh->prepare("SELECT password FROM users WHERE username=?");
$sth->execute($given_username);
if($sth->rows > 0)
{
my ($passID) = $sth->fetchrow_arrayref()) { ... }
}
--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
------------------------------
Date: Mon, 27 Aug 2007 15:44:43 -0000
From: it_says_BALLS_on_your forehead <simon.chao@fmr.com>
Subject: Re: Using DBI, Set Select to Variable
Message-Id: <1188229483.566269.31330@o80g2000hse.googlegroups.com>
On Aug 27, 12:35 am, Jason <jwcarl...@gmail.com> wrote:
> I know how to load info into an array:
>
> my $sth = $dbh->prepare("SELECT username, password FROM users WHERE
> username=?");
> $sth->execute($given_username);
> while (my ($userID, $passID) = $sth->fetchrow_arrayref()) { ... }
>
> What I can't figure out is how to load it to a single variable if I
> know that there's only 1 value, instead of an array. For instance,
> what if I only need to read "password" instead of "username,
> password"?
>
> Currently, the only thing I have been able to find is:
>
> my $sth = $dbh->prepare("SELECT password FROM users WHERE
> username=?");
> $sth->execute($given_username);
> while (my ($passID) = $sth->fetchrow_arrayref()) { ... }
>
> In this case, "username" is a PRIMARY, so it can only be found once;
> meaning, "password" either equals 1 field, or nothing. It seems stupid
> to use a loop when I know that there's only one value in it, but I
> can't find how to pull it out otherwise.
my ( $pass ) = $dbh->selectrow_array("SELECT password FROM users WHERE
username=?");
------------------------------
Date: Mon, 27 Aug 2007 08:55:03 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: Using DBI, Set Select to Variable
Message-Id: <1188230103.293490.276700@y42g2000hsy.googlegroups.com>
On Aug 27, 12:35 am, Jason <jwcarl...@gmail.com> wrote:
> I know how to load info into an array:
... no you don't. :-)
> my $sth = $dbh->prepare("SELECT username, password FROM users WHERE
> username=?");
> $sth->execute($given_username);
> while (my ($userID, $passID) = $sth->fetchrow_arrayref()) { ... }
This code is broken. This code sets $userID to be a reference to an
array which contains the username and password, and sets $passID to
undef.
I think you meant either:
while (my $row = $sth->fetchrow_arrayref()) {
my ($userID, $passID) = @{$row};
...
}
OR
while (my ($userID, $passID) = $sth->fetchrow_array()) { ... }
> What I can't figure out is how to load it to a single variable if I
> know that there's only 1 value, instead of an array. For instance,
> what if I only need to read "password" instead of "username,
> password"?
> Currently, the only thing I have been able to find is:
>
> my $sth = $dbh->prepare("SELECT password FROM users WHERE
> username=?");
> $sth->execute($given_username);
> while (my ($passID) = $sth->fetchrow_arrayref()) { ... }
This is similarly broken. Again, $passID is a reference to an array
that contains only one element - the password.
> In this case, "username" is a PRIMARY, so it can only be
> found once; meaning, "password" either equals 1 field, or
> nothing. It seems stupid to use a loop when I know that
> there's only one value in it, but I can't find how to pull
> it out otherwise.
You don't need a loop. If you know there's only one result row, then
just read that one result row. Don't bother putting it into a loop:
my ($passID) = $sth->fetchrow_array();
Paul Lalli
------------------------------
Date: 27 Aug 2007 16:20:01 GMT
From: xhoster@gmail.com
Subject: Re: Using DBI, Set Select to Variable
Message-Id: <20070827122002.874$9D@newsreader.com>
Jason <jwcarlton@gmail.com> wrote:
> I know how to load info into an array:
>
> my $sth = $dbh->prepare("SELECT username, password FROM users WHERE
> username=?");
> $sth->execute($given_username);
> while (my ($userID, $passID) = $sth->fetchrow_arrayref()) { ... }
Presumably you mean fetchrow_array, not fetchrow_arrayref.
> What I can't figure out is how to load it to a single variable if I
> know that there's only 1 value, instead of an array. For instance,
> what if I only need to read "password" instead of "username,
> password"?
You seem to be confusing rows with columns. How you deal with multiple
columns and how you deal with multiple rows are basically orthogonal.
> Currently, the only thing I have been able to find is:
>
> my $sth = $dbh->prepare("SELECT password FROM users WHERE
> username=?");
> $sth->execute($given_username);
> while (my ($passID) = $sth->fetchrow_arrayref()) { ... }
>
> In this case, "username" is a PRIMARY, so it can only be found once;
> meaning, "password" either equals 1 field, or nothing. It seems stupid
> to use a loop when I know that there's only one value in it, but I
> can't find how to pull it out otherwise.
If you don't want the loop, then remove the loop. On the other
hand, what is wrong with having a loop that you know will loop
either 0 or 1 times? If you know it will loop exactly one time, then
it seems pointless, but if 0 times is possible and looping does what you
want done in that case, then use looping. If 0 times is an error
condition, then something like this:
my ($passID) = $sth->fetchrow_array() or die "No $user";
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Mon, 27 Aug 2007 10:42:52 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Why can't I access an Array like this?
Message-Id: <qb35d3tir7tcms8decn2so9fes3mphok76@4ax.com>
On Sun, 26 Aug 2007 17:43:03 -0700, Bill H <bill@ts1000.us> wrote:
>It never occured to me to use sprintf, substr has always worked for
>me, but now that you mention it I may just start using it.
It's just a matter of not reinventing a wheel. (Possibly risking to do
so in a wrong way.) Similarly you could do:
my @odd;
for (my $i=0; $i<=10; $i++) {
push @odd, $i if $i%2;
}
But you may more easily do
my @odd = grep $_%2, 0..10;
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 V11 Issue 800
**************************************