[32881] in Perl-Users-Digest
Perl-Users Digest, Issue: 4159 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Mar 2 14:09:31 2014
Date: Sun, 2 Mar 2014 11:09:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 2 Mar 2014 Volume: 11 Number: 4159
Today's topics:
Re: grep $re gotcha (Tim McDaniel)
Re: grep $re gotcha <ben@morrow.me.uk>
Re: grep $re gotcha (Tim McDaniel)
Re: grep $re gotcha <rvtol+usenet@xs4all.nl>
Re: grep $re gotcha (Tim McDaniel)
Re: grep $re gotcha <rweikusat@mobileactivedefense.com>
Here documents (was: using a library) <hjp-usenet3@hjp.at>
Re: Here documents (was: using a library) (Tim McDaniel)
Re: Here documents (was: using a library) <hjp-usenet3@hjp.at>
Re: Here documents (was: using a library) (Tim McDaniel)
Re: Here documents <rweikusat@mobileactivedefense.com>
Re: last iteration of a for loop <*@eli.users.panix.com>
Re: last iteration of a for loop (Tim McDaniel)
Re: last iteration of a for loop <ben@morrow.me.uk>
Re: last iteration of a for loop <rweikusat@mobileactivedefense.com>
Re: use strict; use warnings; (Tim McDaniel)
Re: use strict; use warnings; <ben@morrow.me.uk>
Re: use strict; use warnings; <ben@morrow.me.uk>
Re: using a library <rweikusat@mobileactivedefense.com>
Re: using a library <rweikusat@mobileactivedefense.com>
Re: using a library (Tim McDaniel)
Re: using a library <triflemenot@protocol.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 1 Mar 2014 00:55:50 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: grep $re gotcha
Message-Id: <lerb6l$kud$1@reader1.panix.com>
In article <878usuygmo.fsf@castleamber.com>,
John Bokma <john@castleamber.com> wrote:
>
>Maybe not a gotcha for everybody, but I bumped into this today:
>
>perl -e '
>my $re = qr/[aeiou]/;
>print join( ", ", grep $re, "a".."z" ), "\n";
>print join( ", ", grep /$re/, "a".."z" ), "\n";
>'
>a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
>a, e, i, o, u
>
>I was expecting grep $re to be short for grep /$re/.
It took me a few minutes to get an idea of what's going on.
grep BLOCK LIST
grep EXPR, LIST
This is similar in spirit to, but not the same as, grep(1) and its
relatives. In particular, it is not limited to using regular
expressions.
Evaluates the BLOCK or EXPR for each element of LIST (locally
setting $_ to each element) and returns the list value consisting
of those elements for which the expression evaluated to true. ...
So I think it's equivalent to
map { EXPR ? ($_) : () } LIST
so it's evaluating $re or /$re/ (depending on the case above) in a
Boolean context. For the bare $re, the value of $re is a regular
expression, which is not 0, '', et cetera, so the condition evaluates
as true for any list value.
If I'm right, the underlying cause is not that "grep $re" is different
from "grep /$re/" per se, but that $re is different from /$re/ except
in a few places.
I gather that $re is equivalent to /$re/ on the right-hand side of
=~. Are there any other places?
The best solution, I think, is simply to always use /$re/ and never
use $re, because (1) /$re/ works regardless of context, and (2)
anywhere where $re does a regular-expression match, so does /$re/, so
I see no reason to avoid /$re/. Right?
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Sat, 1 Mar 2014 22:08:51 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: grep $re gotcha
Message-Id: <jc2aua-tu2.ln1@anubis.morrow.me.uk>
Quoth tmcd@panix.com:
>
> I gather that $re is equivalent to /$re/ on the right-hand side of
> =~. Are there any other places?
The first argument of split.
> The best solution, I think, is simply to always use /$re/ and never
> use $re, because (1) /$re/ works regardless of context, and (2)
> anywhere where $re does a regular-expression match, so does /$re/, so
> I see no reason to avoid /$re/. Right?
I think so. I believe that even though older versions of perl were
unable to reuse the compiled portion of a qr// in expressions like
/x$re/, they will all avoid recompiling for the particular case of
/$re/. At least, -Mre=debug on 5.6.0 seems to indicate there's no
recompile.
Ben
------------------------------
Date: Sat, 1 Mar 2014 23:54:56 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: grep $re gotcha
Message-Id: <lets0g$9qt$1@reader1.panix.com>
In article <jc2aua-tu2.ln1@anubis.morrow.me.uk>,
Ben Morrow <ben@morrow.me.uk> wrote:
>
>Quoth tmcd@panix.com:
>>
>> I gather that $re is equivalent to /$re/ on the right-hand side of
>> =~. Are there any other places?
>
>The first argument of split.
And I just realized !~, just to be pedantically completist.
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Sun, 02 Mar 2014 01:17:41 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
To: tmcd@panix.com
Subject: Re: grep $re gotcha
Message-Id: <531278A5.6010704@xs4all.nl>
On 2014-03-01 01:55, Tim McDaniel wrote:
> It took me a few minutes to get an idea of what's going on.
>
> grep BLOCK LIST
> grep EXPR, LIST
> [...]
>
> So I think it's equivalent to
>
> map { EXPR ? ($_) : () } LIST
Though map returns copies, and grep returns aliases.
perl -wE'
my @a = (42 .. 45);
++$_ for map { $_ } @a;
say "@a"; # unchanged
++$_ for grep { 1 } @a;
say "@a"; # changed
'
42 43 44 45
43 44 45 46
--
Ruud
------------------------------
Date: Sun, 2 Mar 2014 18:03:12 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: grep $re gotcha
Message-Id: <levrov$4b3$1@reader1.panix.com>
In article <531278A5.6010704@xs4all.nl>,
Dr.Ruud <rvtol+usenet@xs4all.nl> wrote:
>On 2014-03-01 01:55, Tim McDaniel wrote:
>
>> It took me a few minutes to get an idea of what's going on.
>>
>> grep BLOCK LIST
>> grep EXPR, LIST
>> [...]
>>
>> So I think it's equivalent to
>>
>> map { EXPR ? ($_) : () } LIST
>
>Though map returns copies, and grep returns aliases.
Hrm! Thank you for the correction!
Is there any way in Perl to have map return something that acts like
an alias, where
>perl -wE'
> my @a = (42 .. 45);
>
> ++$_ for map { some_magic_involving($_) } @a;
> say "@a";
would produce
>43 44 45 46
? Not references per se, because $a[1] would not be 44 but rather
\44.
... rummage rummage ...
At a brief glance at CPAN, it looks like a search for "alias" could
get some useful candidates.
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Sun, 02 Mar 2014 18:21:57 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: grep $re gotcha
Message-Id: <87ppm4tq9m.fsf@sable.mobileactivedefense.com>
tmcd@panix.com (Tim McDaniel) writes:
> Dr.Ruud <rvtol+usenet@xs4all.nl> wrote:
[...]
>>Though map returns copies, and grep returns aliases.
>
> Hrm! Thank you for the correction!
>
> Is there any way in Perl to have map return something that acts like
> an alias, where
>
>>perl -wE'
>> my @a = (42 .. 45);
>>
>> ++$_ for map { some_magic_involving($_) } @a;
>> say "@a";
>
> would produce
>
>>43 44 45 46
>
> ?
Yes. It is called 'a foreach/ for' loop:
for (@a) {
some_magic_changing_$_;
++$_;
}
Since map is supposed to return the result of some function of $_, an
implementation in Perl could look like this,
sub mapcar
{
my $f = shift;
my @r;
push(@r, $f->($_)) for @_;
return @r;
}
it can't possibly return something aliased back to the original array --
it would have to modify the contents of that instead in this case.
------------------------------
Date: Sat, 1 Mar 2014 18:35:29 +0100
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Here documents (was: using a library)
Message-Id: <slrnlh46j5.s18.hjp-usenet3@hrunkner.hjp.at>
On 2014-03-01 14:16, Rainer Weikusat <rweikusat@mobileactivedefense.com> wrote:
> "Peter J. Holzer" <hjp-usenet3@hjp.at> writes:
>> And not only the final OUTPUT marker must appear at the left margin, but
>> the contents start at the left margin, too. So you either have to write
>> code like this:
>>
>> if ($foo) {
>> for (@bar) {
>> if ($_ <= 0) {
>> print <<OUTPUT;
>> Oh, no! The current element of bar is $_,
>> but foo is $foo.
>> OUTPUT
>> } elsif ($_ == 1) {
>> print <<OUTPUT;
>> Much better. The current element of bar is $_,
>> and foo is $foo.
>> OUTPUT
>> } else {
>> print <<OUTPUT;
>> Great. The current element of bar is $_,
>> and foo is still $foo.
>> OUTPUT
>> }
>> }
>> }
>>
>> which I consider totally unreadable.
>
> It isn't much better with integrated strings:
>
> if ($foo) {
> for (@bar) {
> if ($_ <= 0) {
> print("Oh, no! The current element of bar is $_, but foo is $foo.\n");
> } elsif ($_ == 1) {
> print("Much better. The current element of bar is $_, and foo is $foo.\n");
> } else {
> print("Great. The current element of bar is $_, and foo is still $foo.\n");
> }
> }
> }
This isn't the same. Equivalent with single double quoted strings would be:
if ($foo) {
for (@bar) {
if ($_ <= 0) {
print("Oh, no! The current element of bar is $_,\nbut foo is $foo.\n");
} elsif ($_ == 1) {
print("Much better. The current element of bar is $_,\nand foo is $foo.\n");
} else {
print("Great. The current element of bar is $_,\nand foo is still $foo.\n");
}
}
}
which I agree is only slightly better. But Ben was contrasting this
with multiple print statements, so that would be:
if ($foo) {
for (@bar) {
if ($_ <= 0) {
print "Oh, no! The current element of bar is $_,\n";
print "but foo is $foo.\n";
} elsif ($_ == 1) {
print "Much better. The current element of bar is $_,\n";
print "and foo is $foo.\n";
} else {
print "Great. The current element of bar is $_,\n";
print "and foo is still $foo.\n";
}
}
}
which I do consider significantly more readable than with here
documents. In a case like this I might just use print with multiple
arguments:
if ($foo) {
for (@bar) {
if ($_ <= 0) {
print "Oh, no! The current element of bar is $_,\n",
"but foo is $foo.\n";
} elsif ($_ == 1) {
print "Much better. The current element of bar is $_,\n",
"and foo is $foo.\n";
} else {
print "Great. The current element of bar is $_,\n",
"and foo is still $foo.\n";
}
}
}
Although one print per line has the advantage that I can use say
instead of print and remove the \n, so I think I would prefer
if ($foo) {
for (@bar) {
if ($_ <= 0) {
say "Oh, no! The current element of bar is $_,";
say "but foo is $foo.";
} elsif ($_ == 1) {
say "Much better. The current element of bar is $_,";
say "and foo is $foo.";
} else {
say "Great. The current element of bar is $_,";
say "and foo is still $foo.";
}
}
}
> ie, why does it say 'print' all the time even if it prints only one
> message per iteration and why are the conditions chained in this way despite
> there's no code after them?
[...]
> ... and why is there so much code, anyway?
Completely irrelevant question. The loop and the conditions are just
there to get some reasonable indentation and a bit of code between the
here documents for the here documents to obscure. The aren't meant to
do anything useful (and the here document's aren't supposed to be Nobel
price material, either).
> my @msgs = ('Oh, no! The current element of bar is %s, but foo is %s.',
> 'Much better. The current element of bar is %s, and foo is %s.',
> 'Great. The current element of bar is %s, and foo is still %s.');
>
> printf($msgs[($_ <=> 1) + 1]."\n", $_, $foo) for $foo ? @bar : ();
>
> (this is not an entirely serious suggestion, more of a suggestion that
> one should consider alternate approaches before rejecting them).
This is probably more relevant to the topic, since separating the literal
strings from the logic makes the pain of non-indented here documents
somewhat more bearable. But this:
my @msgs = (<<S0,
Oh, no! The current element of bar is %s,
but foo is %s.
S0
<<S1,
Much better. The current element of bar is %s,
and foo is %s.
S1
<<S2,
Great. The current element of bar is %s,
and foo is still %s.
S2
);
is still uglier than using normal strings, and I'm not even talking
about monstrosities such as this:
my @msgs = (<<S0, <<S1, <<S2);
Oh, no! The current element of bar is %s,
but foo is %s.
S0
Much better. The current element of bar is %s,
and foo is %s.
S1
Great. The current element of bar is %s,
and foo is still %s.
S2
hp
--
_ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
|_|_) | | Man feilt solange an seinen Text um, bis
| | | hjp@hjp.at | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
------------------------------
Date: Sat, 1 Mar 2014 23:51:23 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Here documents (was: using a library)
Message-Id: <letrpq$fio$1@reader1.panix.com>
In article <slrnlh46j5.s18.hjp-usenet3@hrunkner.hjp.at>,
Peter J. Holzer <hjp-usenet3@hjp.at> wrote:
>separating the literal strings from the logic makes the pain of
>non-indented here documents somewhat more bearable.
Helps the whole separation of logic from presentation, which many of
my ork-places have struggled with. It also makes it more
internationalizable, if that's ever a concern of yours.
>I'm not even talking about monstrosities such as this:
>
>my @msgs = (<<S0, <<S1, <<S2);
>Oh, no! The current element of bar is %s,
>but foo is %s.
>S0
>Much better. The current element of bar is %s,
>and foo is %s.
>S1
>Great. The current element of bar is %s,
>and foo is still %s.
>S2
... you can actually DO that?! That is so COOL! I HAVE to do that!
Thanks so much!
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Sun, 2 Mar 2014 03:02:05 +0100
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: Here documents (was: using a library)
Message-Id: <slrnlh548t.fij.hjp-usenet3@hrunkner.hjp.at>
On 2014-03-01 23:51, Tim McDaniel <tmcd@panix.com> wrote:
> In article <slrnlh46j5.s18.hjp-usenet3@hrunkner.hjp.at>,
> Peter J. Holzer <hjp-usenet3@hjp.at> wrote:
>>I'm not even talking about monstrosities such as this:
>>
>>my @msgs = (<<S0, <<S1, <<S2);
>>Oh, no! The current element of bar is %s,
>>but foo is %s.
>>S0
>>Much better. The current element of bar is %s,
>>and foo is %s.
>>S1
>>Great. The current element of bar is %s,
>>and foo is still %s.
>>S2
>
> ... you can actually DO that?! That is so COOL! I HAVE to do that!
> Thanks so much!
>
What have I done!?
hp
--
_ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
|_|_) | | Man feilt solange an seinen Text um, bis
| | | hjp@hjp.at | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
------------------------------
Date: Sun, 2 Mar 2014 18:53:56 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Here documents (was: using a library)
Message-Id: <levuo4$ok4$1@reader1.panix.com>
In article <slrnlh548t.fij.hjp-usenet3@hrunkner.hjp.at>,
Peter J. Holzer <hjp-usenet3@hjp.at> wrote:
>On 2014-03-01 23:51, Tim McDaniel <tmcd@panix.com> wrote:
>> In article <slrnlh46j5.s18.hjp-usenet3@hrunkner.hjp.at>,
>> Peter J. Holzer <hjp-usenet3@hjp.at> wrote:
>>>I'm not even talking about monstrosities such as this:
>>>
>>>my @msgs = (<<S0, <<S1, <<S2);
>>>Oh, no! The current element of bar is %s,
>>>but foo is %s.
>>>S0
>>>Much better. The current element of bar is %s,
>>>and foo is %s.
>>>S1
>>>Great. The current element of bar is %s,
>>>and foo is still %s.
>>>S2
>>
>> ... you can actually DO that?! That is so COOL! I HAVE to do that!
>> Thanks so much!
>
>What have I done!?
I believe that expressing that in all earnestness is the membership
criterion for the Frankenstein Society.
Leaving the Society is often done by shouting at the hero,
"No, this cannot be! I AM INVINCIBLE!!!"
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Sun, 02 Mar 2014 14:51:11 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Here documents
Message-Id: <87mwh8u00w.fsf@sable.mobileactivedefense.com>
"Peter J. Holzer" <hjp-usenet3@hjp.at> writes:
[...]
> I'm not even talking about monstrosities such as this:
>
> my @msgs = (<<S0, <<S1, <<S2);
> Oh, no! The current element of bar is %s,
> but foo is %s.
> S0
> Much better. The current element of bar is %s,
> and foo is %s.
> S1
> Great. The current element of bar is %s,
> and foo is still %s.
> S2
I don't think this is so bad, either. Maintaining an array of multi-line
strings ought to be easier with 'syntactical elements of Perl' out of
the way, including that there's no need to quote anything in this case.
------------------------------
Date: Thu, 27 Feb 2014 00:03:17 +0000 (UTC)
From: Eli the Bearded <*@eli.users.panix.com>
Subject: Re: last iteration of a for loop
Message-Id: <eli$1402261858@qz.little-neck.ny.us>
In comp.lang.perl.misc, hymie! <hymie@lactose.homelinux.net> wrote:
> Alternately, maybe I can print the separator at the **top** of my
> loop, except on the **first** iteration? I can probably do that
> with a flag, but again, I was hoping for something more robust and
> perl-ish.
The way I typically handle that sort of thing can be illustrated thus:
$separator = '';
while ( looping() ) {
print $separator;
$separator = "-------\n";
rest_of_loop()
}
It adapts well to having a special message for the first instance
and for loops whose last instance cannot be known in advance (eg,
user input).
Elijah
------
it also works in any language
------------------------------
Date: Thu, 27 Feb 2014 04:47:46 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: last iteration of a for loop
Message-Id: <lemg1i$kjq$1@reader1.panix.com>
In article <lema06$tjn$2@dont-email.me>,
Xho Jingleheimerschmidt <xhoster@gmail.com> wrote:
>I don't know of a clean solution, but have often wanted one. An
>"again" block similar to the "continue" block, which only gets
>executed once it is decided the loop actually will get executed
>again, would be nice.
It would be possible for a loop like
for VAR (LIST)
because it appears that LIST is evaluated once when the loop starts,
so it knows the list of values to be iterated over.
In general (for (EXPR1; EXPR2; EXPR3) or while (EXPR)), I don't see
how the loop structure could know whether it's on the last iteration
or not, because there might be global side effects or dependencies on
the loop iteration and/or termination.
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Sat, 1 Mar 2014 22:02:35 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: last iteration of a for loop
Message-Id: <r02aua-tu2.ln1@anubis.morrow.me.uk>
Quoth tmcd@panix.com:
> In article <u73sg99d477hlqkj2ra7nfqhdvjvoqi6ik@4ax.com>,
> J_rgen Exner <jurgenex@hotmail.com> wrote:
>
> >Or with HOFs you can use a standard reduce.
>
> "HOFs"? Googling just shows that there's a town named Perl in
> Saarland, where German is prevalent, and so there are lots of places
> called .* Hof (.* House).
'Higher-order functions'. That is, functions which take a function as an
argument, like map and reduce. (In Perl, reduce is in List::Util.)
Ben
------------------------------
Date: Sun, 02 Mar 2014 18:09:08 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: last iteration of a for loop
Message-Id: <87wqgctquz.fsf@sable.mobileactivedefense.com>
tmcd@panix.com (Tim McDaniel) writes:
> In article <lema06$tjn$2@dont-email.me>,
> Xho Jingleheimerschmidt <xhoster@gmail.com> wrote:
>>I don't know of a clean solution, but have often wanted one. An
>>"again" block similar to the "continue" block, which only gets
>>executed once it is decided the loop actually will get executed
>>again, would be nice.
>
> It would be possible for a loop like
> for VAR (LIST)
> because it appears that LIST is evaluated once when the loop starts,
> so it knows the list of values to be iterated over.
>
> In general (for (EXPR1; EXPR2; EXPR3) or while (EXPR)), I don't see
> how the loop structure could know whether it's on the last iteration
> or not, because there might be global side effects or dependencies on
> the loop iteration and/or termination.
It doesn't need to 'know' that, all which is necessary is - for each
iteration - execute the ordinary loop body before the condition is
evaluated and the 'again' ('onrepeat' seems a better name to me) block
after evaluating the condition resulted in the descision that another
iteration will be done. Some programming languages support
condition-less loops, eg,
http://www.postgresql.org/docs/8.4/interactive/plpgsql-control-structures.html#PLPGSQL-CONTROL-STRUCTURES-LOOPS
Perl loop control statements work with bare blocks, hence, unconditional
loop can be implemented as
{
# some code here
redo;
}
and this enables putting a test for loop termination anywhere in between
which provides the desired semantics. But this works only for loops
with an explicit condition, not for 'loop over the members of the set'
loops and it requires testing the condition once before entering the
block for loops which start with testing the condition. Also, this is
bound to confuse people who expect that loops 'look' like loops and
don't hide in bare blocks and suddenly jump at the without warning.
Something which works exactly like a continue block, only that the code
would be executed immediately after evaluating the condition instead of
befor doing so would be a much nicer alternative, not the least because
it would mean a description of the intent of the code could be provided
to the compiler which would then utilize a suitable way of achieving
that instead of one the the numerous ways to 'interfere creatively' with
the control-flow of an ordinary loop which were presented in this
thread.
------------------------------
Date: Sat, 1 Mar 2014 00:36:22 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: use strict; use warnings;
Message-Id: <lera25$1o8$1@reader1.panix.com>
In article <akt4ua-rn31.ln1@anubis.morrow.me.uk>,
Ben Morrow <ben@morrow.me.uk> wrote:
>[Please start a new thread for a new topic.]
>
>Quoth Marek Novotny <mach2@hushmail.com>:
>> my @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H",
>> "9 H","10 H","J H","Q H","K H",
>> "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D",
>> "9 D","10 D","J D","Q D","K D",
>> "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C",
>> "9 C","10 C","J C","Q C","K C",
>> "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S",
>> "9 S","10 S","J S","Q S","K S");
>>
>> my $i = 0; my @randomdeck;
>> while ($i < 51){
>> $randomdeck[$i] = shift(@startingdeck); $i++;
>> $randomdeck[$i] = pop(@startingdeck); $i++;
>> }
>
>This loop continues until @startingdeck is empty. There happen to be 52
>entries at the moment, but in general it's not a good idea to rely on
>'magic numbers' like that:
>
> while (@startingdeck) {
> push @randomdeck, shift @startingdeck;
> push @randomdeck, pop @startingdeck;
> }
A more general programming note:
If, due to error, @startingdeck starts with an odd number of elements,
then the pop will return undef on the last iteration. That may be
sufficient -- maybe the undef will cause a visible error in later
code.
But I don't like to do overruns (I had to look up what pop does for an
empty array).
while (@startingdeck >= 2) {
push @randomdeck, shift @startingdeck;
push @randomdeck, pop @startingdeck;
}
avoids an overrun, but it has the disadvantage of silently ignoring
an odd element.
I like bullet-resistant code, so I might do an assertion check of some
sort beforehand. Or generate @startingdeck programmatically with a
two-level foreach.
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Sat, 1 Mar 2014 22:01:00 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: use strict; use warnings;
Message-Id: <st1aua-tu2.ln1@anubis.morrow.me.uk>
Quoth tmcd@panix.com:
> In article <akt4ua-rn31.ln1@anubis.morrow.me.uk>,
> Ben Morrow <ben@morrow.me.uk> wrote:
> >
> >This loop continues until @startingdeck is empty. There happen to be 52
> >entries at the moment, but in general it's not a good idea to rely on
> >'magic numbers' like that:
> >
> > while (@startingdeck) {
> > push @randomdeck, shift @startingdeck;
> > push @randomdeck, pop @startingdeck;
> > }
>
> A more general programming note:
>
> If, due to error, @startingdeck starts with an odd number of elements,
> then the pop will return undef on the last iteration. That may be
> sufficient -- maybe the undef will cause a visible error in later
> code.
That's a good point. I was actually expecting pop to return the empty
list in list context if the array was empty: an explicit undef is rather
surprising.
> But I don't like to do overruns (I had to look up what pop does for an
> empty array).
>
> while (@startingdeck >= 2) {
> push @randomdeck, shift @startingdeck;
> push @randomdeck, pop @startingdeck;
> }
>
> avoids an overrun, but it has the disadvantage of silently ignoring
> an odd element.
>
> I like bullet-resistant code, so I might do an assertion check of some
> sort beforehand. Or generate @startingdeck programmatically with a
> two-level foreach.
Yes.
Ben
------------------------------
Date: Sat, 1 Mar 2014 21:57:27 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: use strict; use warnings;
Message-Id: <7n1aua-tu2.ln1@anubis.morrow.me.uk>
Quoth tmcd@panix.com:
> In article <6qrvta-qa03.ln1@anubis.morrow.me.uk>,
> Ben Morrow <ben@morrow.me.uk> wrote:
> >No, there was never a year 0 because the Venemous Bede was working
> >with a number system which didn't have a zero.
>
> You mean "the Venomous Bede".
>
> --
> Tim "Sing a song of Saxons / In the wapentake of Rye /
> Four and twenty ealdormen / Too eald to die" McDaniel, tmcd@panix.com,
You're right, I do. The only explanation is that I was distracted by a
surfeit of Eggfroths.
Ben
------------------------------
Date: Sat, 01 Mar 2014 14:16:12 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: using a library
Message-Id: <87ppm6f1hv.fsf@sable.mobileactivedefense.com>
"Peter J. Holzer" <hjp-usenet3@hjp.at> writes:
[...]
> And not only the final OUTPUT marker must appear at the left margin, but
> the contents start at the left margin, too. So you either have to write
> code like this:
>
> if ($foo) {
> for (@bar) {
> if ($_ <= 0) {
> print <<OUTPUT;
> Oh, no! The current element of bar is $_,
> but foo is $foo.
> OUTPUT
> } elsif ($_ == 1) {
> print <<OUTPUT;
> Much better. The current element of bar is $_,
> and foo is $foo.
> OUTPUT
> } else {
> print <<OUTPUT;
> Great. The current element of bar is $_,
> and foo is still $foo.
> OUTPUT
> }
> }
> }
>
> which I consider totally unreadable.
It isn't much better with integrated strings:
if ($foo) {
for (@bar) {
if ($_ <= 0) {
print("Oh, no! The current element of bar is $_, but foo is $foo.\n");
} elsif ($_ == 1) {
print("Much better. The current element of bar is $_, and foo is $foo.\n");
} else {
print("Great. The current element of bar is $_, and foo is still $foo.\n");
}
}
}
ie, why does it say 'print' all the time even if it prints only one
message per iteration and why are the conditions chained in this way despite
there's no code after them?
if ($foo) {
for (@bar) {
if ($_ <= 0) {
$msg = "Oh, no! The current element of bar is $_, but foo is $foo.";
} elsif ($_ == 1) {
$msg = "Much better. The current element of bar is $_, and foo is $foo.";
} else {
$msg = "Great. The current element of bar is $_, and foo is still $foo.";
}
print($msg, "\n");
}
}
One could also ask oneself why this is comparing something to 1 three
times in a row ...
if ($foo) {
for (@bar) {
given ($_ <=> 1) {
when (-1) {
$msg = "Oh, no! The current element of bar is $_, but foo is $foo.";
}
when (0) {
$msg = "Much better. The current element of bar is $_, and foo is $foo.";
}
when (1) {
$msg = "Great. The current element of bar is $_, and foo is still $foo.";
}
}
print($msg, "\n");
}
}
... and why is there so much code, anyway?
my @msgs = ('Oh, no! The current element of bar is %s, but foo is %s.',
'Much better. The current element of bar is %s, and foo is %s.',
'Great. The current element of bar is %s, and foo is still %s.');
printf($msgs[($_ <=> 1) + 1]."\n", $_, $foo) for $foo ? @bar : ();
(this is not an entirely serious suggestion, more of a suggestion that
one should consider alternate approaches before rejecting them).
------------------------------
Date: Sat, 01 Mar 2014 14:27:37 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: using a library
Message-Id: <87r46mgfja.fsf@sable.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
[...]
> One could also ask oneself why this is comparing something to 1 three
> times in a row ...
... which it actually doesn't because Perl numbers are usually not
integers, ie, the original code would print the last message for real
numbers from [0, 1] ...
------------------------------
Date: Fri, 28 Feb 2014 23:43:19 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: using a library
Message-Id: <ler6um$bv2$1@reader1.panix.com>
In article <aqv1h91uifmerbs9cso3ativkb1mcq0q1j@4ax.com>,
Trifle Menot <triflemenot@protocol.invalid> wrote:
>On Fri, 28 Feb 2014 15:26:50 -0500, Charlton Wilbur
><cwilbur@chromatico.net> wrote:
>
>> TM> So you could pass a reference to the array. Like passing a
>> TM> pointer in C. The syntax for using it can be tricky though.
>>
>>Tricky? It would appear that the value of the advice is directly
>>proportional to the author's willingness to sign his name to it.
>
>I see my posts get you all riled up. Another usenet stalker.
>
><plonk>
You appear to have some misunderstandings about the meanings of
"tricky" (the syntax for an arrayref of constants is almost trivial)
and "stalker".
Please be so kind as to add me to your killfile, as I expect that
I would be in excellent company there.
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Sun, 02 Mar 2014 18:19:37 +0000
From: Trifle Menot <triflemenot@protocol.invalid>
Subject: Re: using a library
Message-Id: <sss6h9lpquqp1vrlnma77g0qohldlk84sm@4ax.com>
On Fri, 28 Feb 2014 23:43:19 +0000 (UTC), tmcd@panix.com (Tim McDaniel)
wrote:
>>On Fri, 28 Feb 2014 15:26:50 -0500, Charlton Wilbur
>><cwilbur@chromatico.net> wrote:
>>> TM> So you could pass a reference to the array. Like passing a
>>> TM> pointer in C. The syntax for using it can be tricky though.
>>>
>>>Tricky? It would appear that the value of the advice is directly
>>>proportional to the author's willingness to sign his name to it.
>You appear to have some misunderstandings about the meanings of
>"tricky"
You and your good buddy treated that casual subjective remark as though
it was a life threatening technical blunder. Its purpose went right over
your heads. Tells me a lot your about mental state and group dynamics.
It's not smart to join a gang of thugs. I could be a manager at work for
all you know. Someone who enjoys firing arrogant little twerps.
>Please be so kind as to add me to your killfile
It's not nice to kill people. But you qualify for the ass clown list.
------------------------------
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 4159
***************************************