[31840] in Perl-Users-Digest
Perl-Users Digest, Issue: 3103 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Aug 28 14:09:27 2010
Date: Sat, 28 Aug 2010 11:09: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 Sat, 28 Aug 2010 Volume: 11 Number: 3103
Today's topics:
Re: complicated sort <willem@turtle.stack.nl>
Re: Help writing 'simple' loop joey@igetit.invalid
Re: Help writing 'simple' loop joey@igetit.invalid
Re: Help writing 'simple' loop <sbryce@scottbryce.com>
Re: Help writing 'simple' loop <rvtol+usenet@xs4all.nl>
Re: Help writing 'simple' loop <sbryce@scottbryce.com>
Re: Help writing 'simple' loop <rvtol+usenet@xs4all.nl>
Re: Help writing 'simple' loop <uri@StemSystems.com>
Re: Help writing 'simple' loop <uri@StemSystems.com>
Re: Help writing 'simple' loop joey@igetit.invalid
Re: Help writing 'simple' loop <jurgenex@hotmail.com>
Re: Help writing 'simple' loop <uri@StemSystems.com>
Re: report graphing <smallpond@juno.com>
Re: require hangs <xhoster@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 27 Aug 2010 10:53:19 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: complicated sort
Message-Id: <slrni7f68v.1033.willem@turtle.stack.nl>
monkeys paw wrote:
) In the following array of hashrefs, i need to sort by state and sort
) tag. Problem i am having is that the records without sort tags should
) sort first (by state), then the rest with tags sort by state and then
) sort_tag. With the sort routine i have, it sorts the ones without
) sort_tags first, but it is divided by state. The correct sorting would be:
)
) AK(no tag)
) CA(no tag)
) CO(no tag)
) AK sorted by tag
) CA sorted by tag
)
) I can't get the records with no sort_tags to filter out without having
) the sort_tag override my state sort needs. Any ideas?
I already replied, but I'd like to point out that
you are actually sorting on three things, not two:
- existence of tag
- state
- tag
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
------------------------------
Date: Sat, 28 Aug 2010 10:04:24 -0700
From: joey@igetit.invalid
Subject: Re: Help writing 'simple' loop
Message-Id: <oqfi76t3di9r96rvocinej8ff0prlutset@4ax.com>
Tad McClellan wrote:
>JoeyF1276@earthlink.net <JoeyF1276@earthlink.net> wrote:
>> Sherm Pendley wrote:
>>
>>>JoeyF1276@earthlink.net writes:
>>>
>>>> $h1_right through $h10_right are 10 independent integer values.
>>>
>>>That's your problem right there.
>>>
>>>> $hTotalRight = $h1_right + $h2_right + $h3_right + $h4_right + $h5_right +
>>>> $h6_right + $h7_right + $h8_right + $h9_right + $h10_right;
>>>
>>>When you find yourself using variable names like $h1, $h2, ... $hN,
>>>that's a VERY good sign you should be using an array @n instead.
>
>
>When you find yourself using variable names like $h1_right, $h2_right, ... $h10_right,
>that's a VERY good sign you should be using an array @hRight instead.
>
>
>>>> I can't figure out the syntax for $XXX to write a loop to do the addition.
>>>> $hTotalRight = 0;
>>>> for ($i=1;$i<11;$i++) {
>>>> $hTotalRight = $hTotalRight + $XXX;
>>>> }
>>>
>>> my $hTotalRight;
>>> foreach my $h (@hRight) {
>>> $hTotalRight += $h;
>>> }
>>>
>> Thank you, but I don't understand how you are defining @hRight.
>
>my @hRight = ($h1_right, $h2_right, $h3_right, $h4_right, $h5_right,
> $h6_right, $h7_right, $h8_right, $h9_right, $h10_right);
>
>Or, even better, instead of assigning to $h1_right assign to $hRight[0]
>in the first place.
>
>That is: you are using a cumbersome data structure ("independent" scalars).
>
>Modify your program so that it uses a data structure more suited
>to the structure of your data, namely an array.
Thanks for your helpful comments.
I've taken all the pertinent advice offered and come up with:
hRight = ($h1_right, $h2_right, $h3_right, $h4_right, $h5_right,
$h6_right, $h7_right, $h8_right, $h9_right, $h10_right);
addScores(\@hRight);
sub addScores {
$score = 0;
for ($i=0;$i<10;$i++) {
$score = $score + $_[0][$i];
}
}
which works as it should. (I'm using a subroutine as there are six arrays
to perform the same addition on.)
But...now I'm wondering whether in the pursuit of efficiency and elegance
I'm making a mistake by employing the above instead of simply adding the
variables, i.e.,
$score = ($h1_right + $h2_right) + ($h3_right + $h4_right) + ($h5_right +
$h6_right) + ($h7_right + $h8_right) + ($h9_right + $h10_right);
Is there some reason I would want to abandon seeming simplicity in favor
of 'neat-looking?'
--
Joey
------------------------------
Date: Sat, 28 Aug 2010 10:13:16 -0700
From: joey@igetit.invalid
Subject: Re: Help writing 'simple' loop
Message-Id: <tcgi765hd0iacpme9diraboa751s5ij55n@4ax.com>
Jürgen Exner wrote:
>JoeyF1276@earthlink.net wrote:
>>$h1_right through $h10_right are 10 independent integer values.
>>
>>$hTotalRight = $h1_right + $h2_right + $h3_right + $h4_right + $h5_right +
>>$h6_right + $h7_right + $h8_right + $h9_right + $h10_right;
>>
>>I can't figure out the syntax for $XXX to write a loop to do the addition.
>>$hTotalRight = 0;
>>for ($i=1;$i<11;$i++) {
>>$hTotalRight = $hTotalRight + $XXX;
>>}
>>
>>What is the syntax for $XXX?
>
>It is trivial:
>
>my @h_right = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
>my $hTotalRight = 0;
>for my $i (1...10) {
> $hTotalRight = $hTotalRight + $h_right[$i];
>}
>
Thanks for the input. (Shouldn't the $i range be 0...9?)
>Now, before you scream "I don't want to use a f*#%$#$ array"
>
I have no problem using arrays, but as I pointed out in my post today to
Tad McClellan, I'm wondering if simple addition isn't more efficient.
>you may
>want to read up on the many, many previous posts as well as the FAQ
>about "variable as a variable name" regarding why symbolic references
>are a really, really bad idea.
I understand the issue, but the formalism for which I've written the code
specifically uses h1, h2, etc., and I need to maintain syntactical
identity; hence, $h1_right, $h3_left,etc. It's much easier for me to
relate to $hn_side versus $sideh[0].
--
Joey
------------------------------
Date: Sat, 28 Aug 2010 11:26:12 -0600
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: Help writing 'simple' loop
Message-Id: <i5bgrv$f0l$1@news.eternal-september.org>
On 8/28/2010 11:04 AM, joey@igetit.invalid wrote:
> I've taken all the pertinent advice offered and come up with:
>
> hRight = ($h1_right, $h2_right, $h3_right, $h4_right, $h5_right,
> $h6_right, $h7_right, $h8_right, $h9_right, $h10_right);
ITYM @hRight =
BTW, you will find that camel case (mixing upper and lower case in
variable names) quickly becomes a pain. Try using @h_right.
> addScores(\@hRight);
>
> sub addScores {
Camel case again.
sub add_scores {
> $score = 0;
> for ($i=0;$i<10;$i++) {
for my $i (0 .. 9) {
> $score = $score + $_[0][$i];
> }
Or better still
sub add_scores {
my $scores = shift;
my $total = 0;
for my $i (@$scores) {
$total += $i;
}
return $total;
}
> }
> which works as it should. (I'm using a subroutine as there are six
> arrays to perform the same addition on.)
>
> But...now I'm wondering whether in the pursuit of efficiency and
> elegance I'm making a mistake by employing the above instead of
> simply adding the variables, i.e.,
>
> $score = ($h1_right + $h2_right) + ($h3_right + $h4_right) +
> ($h5_right + $h6_right) + ($h7_right + $h8_right) + ($h9_right +
> $h10_right);
>
> Is there some reason I would want to abandon seeming simplicity in
> favor of 'neat-looking?'
I'm wondering why if you are defining @hRight, are you not using it all
along? It seems pointless to have an @hRight variable and still make use
of your $h1_right, etc variables. Get rid of them and just use the
array. Wherever the initial values are coming from, just load them
directly into the array.
------------------------------
Date: Sat, 28 Aug 2010 19:40:52 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Help writing 'simple' loop
Message-Id: <4c794a24$0$22913$e4fe514c@news.xs4all.nl>
On 2010-08-28 19:26, Scott Bryce wrote:
> sub add_scores {
> my $scores = shift;
> my $total = 0;
>
> for my $i (@$scores) {
> $total += $i;
> }
>
> return $total;
> }
Alternative:
sub sum {
my $values = ref $_[0] ? $_[0] : \@_;
my $total;
$total += $_ for @$values;
return $total;
}
Differences:
- it returns undef when the list is empty
- it accepts either an arrayref or a list
See also List::Util::sum()
--
Ruud
------------------------------
Date: Sat, 28 Aug 2010 11:41:50 -0600
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: Help writing 'simple' loop
Message-Id: <i5bhpa$i9l$1@news.eternal-september.org>
On 8/28/2010 11:13 AM, joey@igetit.invalid wrote:
> I understand the issue, but the formalism for which I've written the
> code specifically uses h1, h2, etc., and I need to maintain
> syntactical identity; hence, $h1_right, $h3_left,etc. It's much
> easier for me to relate to $hn_side versus $sideh[0].
Then maybe you are not the one to be writing this code.
And array does a better job of representing your data than individual
scalars. Using an array to represent your data will result in cleaner
code that will be easier to maintain in the future.
It can't be that big a jump in your thinking to go from $h1_right to
$h_right[1].
------------------------------
Date: Sat, 28 Aug 2010 19:43:28 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Help writing 'simple' loop
Message-Id: <4c794ac1$0$22913$e4fe514c@news.xs4all.nl>
On 2010-08-28 19:13, joey@igetit.invalid wrote:
> the formalism for which I've written the code
> specifically uses h1, h2, etc., and I need to maintain syntactical
> identity; hence, $h1_right, $h3_left,etc. It's much easier for me to
> relate to $hn_side versus $sideh[0].
Nonsense. You can use $h[1]{right} or $h{right}[1] or whatever you like
best.
--
Ruud
------------------------------
Date: Sat, 28 Aug 2010 13:46:51 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Help writing 'simple' loop
Message-Id: <8762yuiq9g.fsf@quad.sysarch.com>
>>>>> "j" == joey <joey@igetit.invalid> writes:
j> I've taken all the pertinent advice offered and come up with:
no you haven't.
j> hRight = ($h1_right, $h2_right, $h3_right, $h4_right, $h5_right,
j> $h6_right, $h7_right, $h8_right, $h9_right, $h10_right);
hRight isn't a legal perl variable. that won't compile under strict.
j> addScores(\@hRight);
j> sub addScores {
j> $score = 0;
you are not using strict. that variable is a global. it can be modified
elsewhere. you are learning very bad habits that are best broken now.
j> for ($i=0;$i<10;$i++) {
why are you indexing by integers? you can loop directly over the array
elements. first get the array reference into a variable
j> $score = $score + $_[0][$i];
learn about +=.
j> }
j> }
j> which works as it should. (I'm using a subroutine as there are six arrays
j> to perform the same addition on.)
you are working too hard. maybe it will take as long to learn how to
write simpler code as it took you to learn to use .invalid correctly?
j> But...now I'm wondering whether in the pursuit of efficiency and elegance
j> I'm making a mistake by employing the above instead of simply adding the
j> variables, i.e.,
j> $score = ($h1_right + $h2_right) + ($h3_right + $h4_right) + ($h5_right +
j> $h6_right) + ($h7_right + $h8_right) + ($h9_right + $h10_right);
why all thos extra parens? have you never added numbers in real life? do
you add them in pairs first?
j> Is there some reason I would want to abandon seeming simplicity in favor
j> of 'neat-looking?'
neither of your solutions is simple nor neat looking. as many have said
why are your variables even named with a number?? that is your
fundamental issue - everything else is side stuff. you should almost
never have numbered names like that.
this is neat, simple, fast, clean, properly scoped, etc. see if you can
figure out why is it massively better than your code in several dimensions.
sub add_scores {
my( $scores ) = @_ ;
my $sum ;
$sum += $_ for @{$scores} ;
return $sum ;
}
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Sat, 28 Aug 2010 13:49:31 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Help writing 'simple' loop
Message-Id: <871v9iiq50.fsf@quad.sysarch.com>
>>>>> "j" == joey <joey@igetit.invalid> writes:
j> I understand the issue, but the formalism for which I've written
j> the code specifically uses h1, h2, etc., and I need to maintain
j> syntactical identity; hence, $h1_right, $h3_left,etc. It's much
j> easier for me to relate to $hn_side versus $sideh[0].
that makes no sense at all. arrays are just as formal in all respects to
a list of numbered variables. they are just better, easier to manage,
less clutter and to repeat myself, better. all formalisms have some
array support unless they are aiming at a purist simplicity. as for your
relating, that is your problem and you should fix it. if you don't learn
arrays now, your code will be useless forever and you won't have many
job prospects in the coding world.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Sat, 28 Aug 2010 10:53:44 -0700
From: joey@igetit.invalid
Subject: Re: Help writing 'simple' loop
Message-Id: <thii769v1llru497gjeej3g7d5gfmton4r@4ax.com>
Scott Bryce wrote:
>On 8/28/2010 11:04 AM, joey@igetit.invalid wrote:
>> I've taken all the pertinent advice offered and come up with:
>>
>> hRight = ($h1_right, $h2_right, $h3_right, $h4_right, $h5_right,
>> $h6_right, $h7_right, $h8_right, $h9_right, $h10_right);
>
>ITYM @hRight =
Yes. Typo.
>
>BTW, you will find that camel case (mixing upper and lower case in
>variable names) quickly becomes a pain. Try using @h_right.
Too late. I've been writing with that notation for years. I'm old...and an
old dog.
>
>> addScores(\@hRight);
>>
>> sub addScores {
>
>Camel case again.
>
>sub add_scores {
>
>
>> $score = 0;
>> for ($i=0;$i<10;$i++) {
>
>for my $i (0 .. 9) {
Outside of declaring variables to comply with Perl's 'strict' directive,
why is it important to declare variables as local. Why not global?
>
>> $score = $score + $_[0][$i];
>> }
>
>Or better still
>
>sub add_scores {
> my $scores = shift;
> my $total = 0;
>
> for my $i (@$scores) {
> $total += $i;
> }
>
> return $total;
>}
>
>> }
>> which works as it should. (I'm using a subroutine as there are six
>> arrays to perform the same addition on.)
>>
>> But...now I'm wondering whether in the pursuit of efficiency and
>> elegance I'm making a mistake by employing the above instead of
>> simply adding the variables, i.e.,
>>
>> $score = ($h1_right + $h2_right) + ($h3_right + $h4_right) +
>> ($h5_right + $h6_right) + ($h7_right + $h8_right) + ($h9_right +
>> $h10_right);
>>
>> Is there some reason I would want to abandon seeming simplicity in
>> favor of 'neat-looking?'
>
>I'm wondering why if you are defining @hRight, are you not using it all
>along? It seems pointless to have an @hRight variable and still make use
>of your $h1_right, etc variables. Get rid of them and just use the
>array. Wherever the initial values are coming from, just load them
>directly into the array.
I've only defined @hRight so that I could write the loop. Beyond that, I
have no reason to otherwise collect the variables in an array, except to
add them.
So, my question remains...is it more efficient to simply add the
variables?
--
Joey
------------------------------
Date: Sat, 28 Aug 2010 11:02:02 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Help writing 'simple' loop
Message-Id: <30ji76l0pjutrtjpsfjptmars2s8p08n1i@4ax.com>
joey@igetit.invalid wrote:
>hRight = ($h1_right, $h2_right, $h3_right, $h4_right, $h5_right,
>$h6_right, $h7_right, $h8_right, $h9_right, $h10_right);
I think you meant
@hRight = ....
But that is nuts! You are putting the cart before the horse.
Get rid of those stupid $h1_right .... $h10_right and use the propper
$hRight[0] ... $hRight[9] instead.
>addScores(\@hRight);
>
>sub addScores {
>$score = 0;
>for ($i=0;$i<10;$i++) {
Any specific reason why you are not using
for my $i (0..9) {
> $score = $score + $_[0][$i];
>}
>}
>which works as it should. (I'm using a subroutine as there are six arrays
>to perform the same addition on.)
>
>But...now I'm wondering whether in the pursuit of efficiency and elegance
>I'm making a mistake by employing the above instead of simply adding the
>variables, i.e.,
>
>$score = ($h1_right + $h2_right) + ($h3_right + $h4_right) + ($h5_right +
>$h6_right) + ($h7_right + $h8_right) + ($h9_right + $h10_right);
>
>Is there some reason I would want to abandon seeming simplicity in favor
>of 'neat-looking?'
Yes, you are making a mistake. No, it is not for the reason you believe.
Your mistake is using those stupid numbered variables $h1_right,
$h2_right, $h3_right, $h4_right, $h5_right,$h6_right, $h7_right,
$h8_right, $h9_right, $h10_right in the first place.
As people have trying to tell you many times: don't do that. Use an
array instead.
And if you do so, then you don't even need the home-made sub but can
simply use sum() from List::Util.
jue
------------------------------
Date: Sat, 28 Aug 2010 14:08:40 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Help writing 'simple' loop
Message-Id: <87bp8mhaon.fsf@quad.sysarch.com>
>>>>> "j" == joey <joey@igetit.invalid> writes:
j> Too late. I've been writing with that notation for years. I'm old...and an
j> old dog.
j> Outside of declaring variables to comply with Perl's 'strict' directive,
j> why is it important to declare variables as local. Why not global?
sorry, i can't teach such things to old dogs. why learn something useful
to coding if you can't even change a simple naming style. i won't waste
effort on this. others have tried and you are trying our patience.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Fri, 27 Aug 2010 12:29:08 -0400
From: Steve C <smallpond@juno.com>
Subject: Re: report graphing
Message-Id: <i58p51$d3q$1@news.eternal-september.org>
On 08/26/2010 11:09 AM, androidUser78 wrote:
> On Aug 26, 10:08 am, Steve C<smallp...@juno.com> wrote:
>> On 08/26/2010 09:35 AM, androidUser78 wrote:
>>
>>> I am not sure where else to post this but I am running a LAMP
>>> environment and my users now want historical graphical reporting. For
>>> instance, normally the user would have a web interface and submit a
>>> bunch of parameters to it and I would spit back a report. But now they
>>> want this report to be graphical and they want it to keep historical
>>> information of their parameters. So for instance at 9am they want to
>>> see how many hits a site got (assume for example purposes we store in
>>> the DB). Then at 9:10, the report is refreshed and they see how many
>>> hits we have gotten since 9am, then 9:20, etc, etc. The interface and
>>> backend stuff is already done but does anyone use or know of any tools
>>> that would help me plot this stuff? I looked at Cacti but it seems
>>> like it only takes a script that you need to enter into the web
>>> interface.
>>
>> use Chart;
>> if you want to have the db and just want to roll your own graphics.
>>
>> use rrdtool if you want to set up something new to store the data for you and
>> to generate plots.
>
> I am reading about rrdtool. Will it be able to handle a result set
> from a DB and be able to store it in its own DB?
I've never used it that way, but you could. It seems kind of inefficient to
copy the data, but you could generate the rrd database by scanning your db
and including the timestamp option in the rrdupdate calls.
------------------------------
Date: Fri, 27 Aug 2010 21:31:03 -0700
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: require hangs
Message-Id: <4c788edf$0$13826$ed362ca5@nr5-q3a.newsreader.com>
monkeys paw wrote:
> I have a few dependant modules, too extensive to post at this
> point. A general question, what could make a 'require' of
> a module hang, when a 'use' works?
Accidentally? I have no idea. Maliciously? How about this:
package foo;
while ($foo::bar) {};
1;
__END__
perl -wle 'use strict; $foo::bar=1; use foo'
(does not hang)
perl -wle 'use strict; $foo::bar=1; require foo'
(hangs)
> I could try to boil it
> down to something more specific, but the mystery lies
> within several thousand lines of code.
How do you know that it hangs at the require and not something else?
How do you know that without knowing which of the specific ones it hangs at?
Is it burning CPU when hung, or is it idle?
> Any suggestions
> are appreciated. Thanks.
strace (on Linux, other OSes have other tools) often gives a very good
idea of where things went off the rails.
Xho
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 3103
***************************************