[32877] in Perl-Users-Digest
Perl-Users Digest, Issue: 4155 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 26 14:09:43 2014
Date: Wed, 26 Feb 2014 11:09:07 -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 Wed, 26 Feb 2014 Volume: 11 Number: 4155
Today's topics:
Re: cookie <xhoster@gmail.com>
Re: cookie <hjp-usenet3@hjp.at>
Re: cookie <hjp-usenet3@hjp.at>
Re: hex print <rvtol+usenet@xs4all.nl>
Re: hex print <triflemenot@protocol.invalid>
last iteration of a for loop (hymie!)
Re: last iteration of a for loop <news@lawshouse.org>
Re: last iteration of a for loop <janek_schleicher@yahoo.de>
Re: last iteration of a for loop <janek_schleicher@yahoo.de>
Re: last iteration of a for loop <jurgenex@hotmail.com>
Re: last iteration of a for loop <rweikusat@mobileactivedefense.com>
Re: last iteration of a for loop <rweikusat@mobileactivedefense.com>
Re: last iteration of a for loop <rweikusat@mobileactivedefense.com>
Re: last iteration of a for loop <janek_schleicher@yahoo.de>
Re: last iteration of a for loop <rvtol+usenet@xs4all.nl>
Re: last iteration of a for loop <rweikusat@mobileactivedefense.com>
Re: use strict; use warnings; <news@todbe.com>
Re: use strict; use warnings; <ben@morrow.me.uk>
Re: use strict; use warnings; <mach2@hushmail.com>
Re: use strict; use warnings; <rweikusat@mobileactivedefense.com>
Re: use strict; use warnings; <ben@morrow.me.uk>
Re: use strict; use warnings; <rweikusat@mobileactivedefense.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 25 Feb 2014 18:33:07 -0800
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: cookie
Message-Id: <lejmna$iof$3@dont-email.me>
On 02/25/14 01:37, George Mpouras wrote:
> Στις 25/2/2014 05:29, ο/η Xho Jingleheimerschmidt έγραψε:
>> On 02/24/14 15:02, George Mpouras wrote:
>>>
>>>
>>> well here is the flow.
>>>
>>> 1) at the very start (before the header) I check if the user have the
>>> cookie and if contain a valid data. In this case the user is redirected
>>> immediately to the "final" page
>>>
>>> 2) In case there is no cookie, or if the cookie data are not valid, The
>>> user must fill a form.
>>> If the form data validated successfully, then a cookie must be sent at
>>> user's browser , and redirected to the final page (here is the
>>> difficulty is to send the cookie after the submission and validation)
>>
>> Item 2 is really items 2 and 3, as telling the user to fill out a form,
>> and processing the form when/if they do so, are two different things.
>>
>> Here is a direct translation of your requirements. You did not specify
>> what should happen if the user submitted the form but it was invalid, I
>> just threw an fatal error, but you could just redisplay the form.
>>
>> It is not so nice to have the redirect in two different places, but as I
>> said it is direct translation of your requirements.
>>
>> In particular, if you don't print the header too early, you will not
>> have a problem with the header having been printed too early. Simply
>> delay printing the header until until you know you don't need to
>> redirect.
>>
>> #!/usr/bin/perl
>> use CGI;
>> use CGI::Carp qw(fatalsToBrowser);
>> use strict;
>>
>> my $cgi=new CGI;
>>
>> if ($cgi->cookie('valid') eq 'valid') {
>> print $cgi->redirect("/final");
>> exit;
>> };
>>
>> if (not $cgi->param()) {
>> print $cgi->header(), $cgi->start_html(), $cgi->start_form();
>> print $cgi->textfield('foo');
>> print $cgi->submit();
>> exit;
>> };
>>
>> if ($cgi->param('foo') eq 'valid') {
>> print $cgi->redirect(
>> -uri => "/final",
>> -cookie=>$cgi->cookie('valid','valid')
>> );
>> exit;
>> };
>>
>> die "Form was submitted, but was not valid";
>
>
> Xho,
>
> I hope you understand that you can not redirect if you print the header
That is why I did not print the header when I needed to redirect instead.
> I hope you understand that you can not send a cookie after the header is
> printed.
That is why I did not print the header when I still needed to figure out
the cookie.
> your code do not come even close to work.
I did test it, it works. You did not.
I'm now sorry I've tried to assist a proven troll, liar, and half-wit.
Bye-bye George.
Xho
------------------------------
Date: Wed, 26 Feb 2014 13:40:28 +0100
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: cookie
Message-Id: <slrnlgro5s.e2e.hjp-usenet3@hrunkner.hjp.at>
On 2014-02-25 21:42, George Mpouras <gravitalsun@hotmail.foo> wrote:
>
>> You obviously didn't try it. It works.
>>
>>> It is very tricky and can not be done without very carefully design and
>>> testing.
>>
>> Actually it's rather simple if you understand the request-response
>> nature of HTTP and that the CGI script is invoked for each request.
>>
>> Try to understand what the script does for these requests:
>>
>> ------------------------------------------------------------------------
>> GET /xho.cgi HTTP/1.0
>>
>> ------------------------------------------------------------------------
>> POST /xho.cgi HTTP/1.0
>> Content-Type: application/x-www-form-urlencoded
>>
>> valid=invalid
>>
>> ------------------------------------------------------------------------
>> POST /xho.cgi HTTP/1.0
>> Content-Type: application/x-www-form-urlencoded
>>
>> valid=valid
>>
>> ------------------------------------------------------------------------
>> GET /xho.cgi HTTP/1.0
>> Cookie: valid=valid
>>
>> ------------------------------------------------------------------------
>
>
> Well I try it. Not only this but several other similar or not
> variations. I also can see the requests , but I can not see any cookie
> at Firefox cookies.sqlite
It sets a session cookie. Session cookies shouldn't be stored, so it is
not surprising that you don't see it in cookies.sqlite. You should see
it in Preferences -> Privacy -> Cookies, however, and - more importantly
- you should see the effect. If you try to load the script again, you
are immediately redirected.
> I do not know, maybe I am doing something wrong, but I end up to
> conclusion that you can not send any cookie (at lease through CGI) after
> the header is printed
It is true that you can not send any cookie after the header (it needs
to be in the header, but this this is exactly why Xho sends the cookie
in the header and not after it. He has shown you how to do it. Please
make at least a minimal effort to understand the script before repeating
the problem solved by the very script you are looking at.
> For not messing this post , I will post a stripped down variation of my
> latest try
I don't think including relevant information counts as "messing".
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/ | zusammenpat. -- Ralph Babel
------------------------------
Date: Wed, 26 Feb 2014 13:47:54 +0100
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: cookie
Message-Id: <slrnlgrojq.e2e.hjp-usenet3@hrunkner.hjp.at>
On 2014-02-25 21:43, George Mpouras <gravitalsun@hotmail.foo> wrote:
> #!/usr/bin/perl
> use strict;
> use warnings;
> use CGI qw/:standard -compile -nosticky/; $CGI::DISABLE_UPLOADS=1;
> use CGI::Carp qw/fatalsToBrowser/;
> my
> $cgi = new CGI;
> $cgi->autoEscape(1);
>
>
> # First lets check if there is already a cookie at user browser
> my %cookie_data = $cgi->cookie('LastActions');
>
> if ((exists $cookie_data{Book}) && (exists $cookie_data{Time}))
> {
> # some code goes here
> print CGI::redirect("http://www.cpan.org");
> exit
> }
>
> print
> $cgi->header(
> -type => 'text/html',
> -charset => 'UTF-8',
> -encoding => 'UTF-8',
> '-Content-Language' => 'el',
> -lang => 'el-Greek',
> -expires => '+10h',
> -nph => 0,
> -status => '200 ok'),
You print the header here. Unconditionally (well, almost).
And then you print the HTML body and finally you try to set the cookie
here:
> if ( defined $cgi->param('SUBMIT') )
> {
> print $cgi->redirect( -uri => "http://www.cpan.org", -cookie =>
> cookie(-name=>'LastActions', -path=>'/', -secure=>0, -expires=>'+2h',
> -value=>{ Book => $cgi->param('Book'), Shelf => $cgi->param('Shelf') } ) );
> exit;
> }
So that's your mistake. Although you have repeatedly written yourself
that you cannot set the cookie after printing the header and although
Xho showed you how to delay printing the header until you know what
should go into it, you still do it in a way which you know cannot work.
Why?
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/ | zusammenpat. -- Ralph Babel
------------------------------
Date: Wed, 26 Feb 2014 11:50:21 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
To: Trifle Menot <triflemenot@protocol.invalid>
Subject: Re: hex print
Message-Id: <530DC6ED.80304@xs4all.nl>
On 2014-02-25 23:33, Trifle Menot wrote:
> On Tue, 25 Feb 2014 16:29:00 -0500, Charlton Wilbur
> <cwilbur@chromatico.net> wrote:
>>>>>>> "T" == Trifle Menot <triflemenot@protocol.invalid> writes:
>>
>> T> To print a string
>> >> my $data = 'abc123';
>>
>> T> in hex, separated by single spaces
>>
>> >> 61 62 63 31 32 33
>>
>> T> can it be done without looping?
>>
>> No. Something will need to iterate; the best you can accomplish is
>> making it implicit rather than explicit.
>
> I know what assembly language is, so I understand what you mean. But
> those words may confuse people who only want to learn enough Perl to
> solve their immediate problem.
>
> It's not always wise to say everything you know.
It can also not be done without electricity.
TIMTOWDI: there is (always) more than one (decent) way to do it.
A better general question is:
What practical{, joker} ways are there to ...
Driest is probably:
perl -wE'
my $data = "abc123";
say join " ", unpack "(H2)*", $data;
'
61 62 63 31 32 33
--
Ruud
------------------------------
Date: Wed, 26 Feb 2014 17:43:16 +0000
From: Trifle Menot <triflemenot@protocol.invalid>
Subject: Re: hex print
Message-Id: <l59sg9p08bhjpgmajcji2vdjr7cs7tsuj3@4ax.com>
On Wed, 26 Feb 2014 11:50:21 +0100, "Dr.Ruud" <rvtol+usenet@xs4all.nl>
wrote:
>> On Tue, 25 Feb 2014 16:29:00 -0500, Charlton Wilbur
>>> No. Something will need to iterate; the best you can accomplish is
>>> making it implicit rather than explicit.
>It can also not be done without electricity.
>TIMTOWDI: there is (always) more than one (decent) way to do it.
In this case maybe not.
>Driest is probably:
>
>perl -wE'
> my $data = "abc123";
> say join " ", unpack "(H2)*", $data;
>'
>61 62 63 31 32 33
I consider print vs. say immaterial. The essence of the solution is:
a) unpack a string to a list of hex values
b) rejoin it with spaces
Both steps in one line of code. I don't see how that could be improved.
------------------------------
Date: 26 Feb 2014 14:30:26 GMT
From: hymie@lactose.homelinux.net (hymie!)
Subject: last iteration of a for loop
Message-Id: <530dfa82$0$29196$862e30e2@ngroups.net>
Greeings.
So I've got a for loop. It's doing some printing, and at the end of
what it prints, it prints a separator line like
----------
Is there an easy way that I can tell my loop "don't print the separator
after the last iteration"?
I know I can say
print "----------" unless $i==9;
but I was hoping there might be something more robust or generic, or that
would work in a more complex loop such as
foreach $day (sort { daynum($a) <=> daynum($b) } keys %sched)
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.
--hymie! http://lactose.homelinux.net/~hymie hymie@lactose.homelinux.net
-------------------------------------------------------------------------------
------------------------------
Date: Wed, 26 Feb 2014 15:34:41 +0000
From: Henry Law <news@lawshouse.org>
Subject: Re: last iteration of a for loop
Message-Id: <JqCdnUpa-d0MlJPOnZ2dnUVZ8nGdnZ2d@giganews.com>
On 26/02/14 14:30, hymie! wrote:
> Is there an easy way that I can tell my loop "don't print the separator
> after the last iteration"?
This is an interesting question; it has occurred to me before, but I
just strongarm it through in one of the ways you mention.
But, thinking about it, what you're wanting is a way that the code
inside a loop can intrinsically tell when it's processing the last
iteration. (By "intrinsically" I mean having reference only to the code
within the loop).
I can't see how you'd ever do that.
You either have to refer to some counter (extrinsic to the loop code)
which says when you're on the last lap, or to a flag (extrinsic again)
which tells you you're on the first one, or to some behaviour of a
statement inside the loop which is affected by something extrinsic ( as
in the kind of loop you write to read lines from a file, i.e. while ( my
$line = <$SOMEFILE> ) ...).
Now I wait for one of the experts to show me I'm wrong. I'll be
grateful 'cos I feel uncomfortable about those flags and counters too.
--
Henry Law Manchester, England
------------------------------
Date: Wed, 26 Feb 2014 16:50:48 +0100
From: Janek Schleicher <janek_schleicher@yahoo.de>
Subject: Re: last iteration of a for loop
Message-Id: <bn6gr0Fq5avU1@mid.individual.net>
Am 26.02.2014 15:30, schrieb hymie!:
> So I've got a for loop. It's doing some printing, and at the end of
> what it prints, it prints a separator line like
>
> ----------
>
> Is there an easy way that I can tell my loop "don't print the separator
> after the last iteration"?
>
> I know I can say
> print "----------" unless $i==9;
> but I was hoping there might be something more robust or generic, or that
> would work in a more complex loop such as
> foreach $day (sort { daynum($a) <=> daynum($b) } keys %sched)
>
> 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.
a way for a
foreach my $item (@not_changing_list) {
...
} loop would be too rewrite it as a: [untested]
for (my ($item, @l) = @not_changing_list; $item = shift @l; @l > 0) {
...
print "--------\n" if @l;
# @l is empty, so false when processing last item
}
It has obv the disadvantage that the original list has first to be
copied, but looks anyway solid unless we are talking about massive data
sizes or side effects (e.g. when working with Tie::File).
Greetings,
Janek
------------------------------
Date: Wed, 26 Feb 2014 17:01:12 +0100
From: Janek Schleicher <janek_schleicher@yahoo.de>
Subject: Re: last iteration of a for loop
Message-Id: <bn6hehFq9m2U1@mid.individual.net>
Am 26.02.2014 16:50, schrieb Janek Schleicher:
> for (my ($item, @l) = @not_changing_list; $item = shift @l; @l > 0) {
should be of course
for (my ($item, @l) = @not_changing_list; @l > 0; $item = shift @l) {
(use C-stylish for loops so rarely)
> ...
> print "--------\n" if @l;
> # @l is empty, so false when processing last item
> }
A complete different way that works more generic would be to use one of
the Iterator modules from CPAN, e.g.
use Array::Iterator;
for (my $i = Array::Iterator->new(@array); $i->has_next(); $i->next()) {
my $current = $i->current();
# .. do something with current
print "------" if $i->has_next();
}
in that way can also work with non constant lists.
Greetings,
Janek
------------------------------
Date: Wed, 26 Feb 2014 08:00:59 -0800
From: Jrgen Exner <jurgenex@hotmail.com>
Subject: Re: last iteration of a for loop
Message-Id: <u73sg99d477hlqkj2ra7nfqhdvjvoqi6ik@4ax.com>
hymie@lactose.homelinux.net (hymie!) wrote:
>Greeings.
>
>So I've got a for loop. It's doing some printing, and at the end of
>what it prints, it prints a separator line like
>
>----------
>
>Is there an easy way that I can tell my loop "don't print the separator
>after the last iteration"?
This is a very typical scenario and it baffles a surprisingly large
number of people. There are 2 standard ways of handling it.
If it is just output you want to combine then maybe you can use the
join() function:
foreach ($elem in @list){
push @results, process_item($elem);
}
print (join($separator, @results));
Or you can process the first element separately (easier than the last
because often you don't know when you have reached the last element):
process_item($list[0]);
foreach ($elem in @list[1..@list]){
print $separator;
process_item($elem);
}
Or with HOFs you can use a standard reduce.
jue
------------------------------
Date: Wed, 26 Feb 2014 16:10:01 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: last iteration of a for loop
Message-Id: <87fvn5j1nq.fsf@sable.mobileactivedefense.com>
hymie@lactose.homelinux.net (hymie!) writes:
>
> So I've got a for loop. It's doing some printing, and at the end of
> what it prints, it prints a separator line like
>
> ----------
>
> Is there an easy way that I can tell my loop "don't print the separator
> after the last iteration"?
>
> I know I can say
> print "----------" unless $i==9;
> but I was hoping there might be something more robust or generic, or that
> would work in a more complex loop such as
> foreach $day (sort { daynum($a) <=> daynum($b) } keys %sched)
>
> 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.
AFAIK, you've hit a fundamental, conceptual limitation in the way
"structured programming loops" are defined: The notion of "some piece of
code which gets executed prior to each re-execution of the loop body"
doesn't exist. It is possible to work around this for do - while loops
with goto a la
goto doit;
do {
# print separator
doit:
# print other stuff
} while keep_on_running();
but that's not a good idea in Perl because Perl gotos are very expensive
and it doesn't work for loops which check the condition prior to
executing the loop-body for the first time.
The usual workarounds would be "print separator at the top and 1st
element before the loop" or "print separator at the bottom and first
element after the loop".
------------------------------
Date: Wed, 26 Feb 2014 16:13:44 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: last iteration of a for loop
Message-Id: <87bnxtj1hj.fsf@sable.mobileactivedefense.com>
Janek Schleicher <janek_schleicher@yahoo.de> writes:
> Am 26.02.2014 16:50, schrieb Janek Schleicher:
>> for (my ($item, @l) = @not_changing_list; $item = shift @l; @l > 0) {
>
> should be of course
> for (my ($item, @l) = @not_changing_list; @l > 0; $item = shift @l) {
>
> (use C-stylish for loops so rarely)
>
>> ...
>> print "--------\n" if @l;
>> # @l is empty, so false when processing last item
>> }
>
>
> A complete different way that works more generic would be to use one
> of the Iterator modules from CPAN, e.g.
>
> use Array::Iterator;
>
> for (my $i = Array::Iterator->new(@array); $i->has_next(); $i->next()) {
> my $current = $i->current();
> # .. do something with current
> print "------" if $i->has_next();
> }
>
> in that way can also work with non constant lists.
That equivalent to the print .... if $i == 9 the author wanted to get
rid of (in both cases and besides being an atrocious attempt if
imitating Java in Perl for the sake of 'eyesore' ...).
------------------------------
Date: Wed, 26 Feb 2014 16:21:39 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: last iteration of a for loop
Message-Id: <8738j5j14c.fsf@sable.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
[...]
> The notion of "some piece of
> code which gets executed prior to each re-execution of the loop body"
> doesn't exist.
[...]
> The usual workarounds would
[...]
>or "print separator at the bottom and first element after the loop".
s/first/last
------------------------------
Date: Wed, 26 Feb 2014 17:55:37 +0100
From: Janek Schleicher <janek_schleicher@yahoo.de>
Subject: Re: last iteration of a for loop
Message-Id: <bn6kkiFqvmrU1@mid.individual.net>
Am 26.02.2014 17:13, schrieb Rainer Weikusat:
>> in that way can also work with non constant lists.
>
> That equivalent to the print .... if $i == 9 the author wanted to get
> rid of (in both cases and besides being an atrocious attempt if
> imitating Java in Perl for the sake of 'eyesore' ...).
You mean beside, not having to track the not needing loop nr and beside
having magical nrs in your code, beside it works easy when changing the
list in any way that is not too drastic, or to say it with OP's words:
"
I know I can say
print "----------" unless $i==9;
but I was hoping there might be something more robust or generic, or that
would work in a more complex loop such as
foreach $day (sort { daynum($a) <=> daynum($b) } keys %sched)
"
Both my proposed solutions would work easy (to implement, read, maintan,
adopt), robust, generic with that and even if you got a more complex
question (like an XML feed or a complex asynchronous calculation), just
take another CPAN module for Iterators, usually all you have to give is
a sub of how to get the next elem and here indeed wheter the next elem
even exists. But you certainly can get rid of having to hard code it in
the loop.
I don't know what you expected,
I mean at some point we have to say to Perl or control flow, that a part
of the loop only has to be executed if it is not the last round. I think
a if @l or a if @i->has_next is close to be shorter than any
possible other syntactic sugar or any hack.
I mean, we can even make something like:
my ($first, @inbetween, $last) = @l;
HANDLE_FIRST: {
...
}
HANDLE_INBETWEEN: for ... {
...
}
HANDLE_LAST: {
...
}
what is pretty much what you proposed.
Pretty much this is what OP asked for (and maybe a compiler might
optimize us too),
all I give him is a kind of 2 shortcuts for that, that still ain't to be
hacks, still are maintainable and readable and where we don't probably
copy 3x relevant code.
Of course, that neither can handle all kind of loops (just if it could,
Perl wouldn't be Turing complete), nor it is a quicker/shorter than to
hardcode list abbrubt condition into the loop.
Make a better suggestion, (beside goto xor make 1st and last round
seperate special cases)
Greetings,
Janek
------------------------------
Date: Wed, 26 Feb 2014 18:03:58 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
To: "hymie!" <hymie@lactose.homelinux.net>
Subject: Re: last iteration of a for loop
Message-Id: <530E1E7E.30600@xs4all.nl>
On 2014-02-26 15:30, hymie! 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.
Yes, rather go for that.
perl -wE'
sub show {
my $sep = ("- " x 6)."-";
my $show_sep;
for my $i ( 1 .. 4 ) {
$show_sep and say $sep or $show_sep = 1;
say $i;
}
}
show;
show;
'
--
Ruud
------------------------------
Date: Wed, 26 Feb 2014 18:22:03 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: last iteration of a for loop
Message-Id: <87txblhgz8.fsf@sable.mobileactivedefense.com>
Janek Schleicher <janek_schleicher@yahoo.de> writes:
> Am 26.02.2014 17:13, schrieb Rainer Weikusat:
>>> in that way can also work with non constant lists.
>>
>> That equivalent to the print .... if $i == 9 the author wanted to get
>> rid of (in both cases and besides being an atrocious attempt if
>> imitating Java in Perl for the sake of 'eyesore' ...).
>
> You mean beside, not having to track the not needing loop nr and
> beside having magical nrs in your code,
I 'mean' that it is special-casing the last iteration of the loop in
exactly the same way as the original counting loop, just adapted to a
different set of 'iteration mechanics' you happen to like better than
ordinary Perl-foreach loops. For the 'traverse a list case', this can also
be written as
my @temp = produce_list_of_elements();
for (0 .. $#temp) {
# do something with $temp[$_]
print_sep() unless $_ == $#temp;
}
No need to count anything explicitly, no 'magic constants' and no for
(;;;) loop expressing something semantically equivalent in a more
complicated way. But since that's still what the OP didn't want to do
(and I'm convinced he is that knowledgable himself), I didn't post this.
[...]
> I mean, we can even make something like:
> my ($first, @inbetween, $last) = @l;
> HANDLE_FIRST: {
> ...
> }
> HANDLE_INBETWEEN: for ... {
> ...
> }
> HANDLE_LAST: {
> ...
> }
> what is pretty much what you proposed.
I didn't propose anything, I just pointed out that a ready-made solution
doesn't exist and mentioned two other workarounds.
------------------------------
Date: Tue, 25 Feb 2014 16:08:05 -0800
From: "$Bill" <news@todbe.com>
Subject: Re: use strict; use warnings;
Message-Id: <lejb95$t36$1@dont-email.me>
On 2/25/2014 05:02, Henry Law wrote:
> On 25/02/14 12:00, $Bill wrote:
>> Except the zeroth and first element are the same thing
>
> What fun. Does that mean we have, for the first time in history, a need for the cardinal "oneth"? The first element is the zeroth; the second is the oneth.
Yeah, but twoth sounds funny and I have no idea how to say twelftheth. :)
------------------------------
Date: Wed, 26 Feb 2014 01:15:18 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: use strict; use warnings;
Message-Id: <6qrvta-qa03.ln1@anubis.morrow.me.uk>
Quoth Rainer Weikusat <rweikusat@mobileactivedefense.com>:
> Ben Morrow <ben@morrow.me.uk> writes:
> > Quoth Jrgen Exner <jurgenex@hotmail.com>:
> >> Ben Morrow <ben@morrow.me.uk> wrote:
> >> >It's not uncommon, when talking about languages like Perl which index
> >> >from 0, to start the ordinals at 'zeroth'.
> >>
> >> Only by those people, who also celebrated the start of the third
> >> millenium at the end of 1999.
> >
> > No, that's the opposite problem: the Gregorian calendar is 1-indexed.
>
> The Gregorian calender isn't indexed at all because time is not a
> random-access sequence.
Of course it is. If I open my diary at the page for 'June 2014' I am
accessing a representation of a particular period of time using an
index.
> But it is a nice example to illustrate the
> differnce: 1AD is the year were Christ turned 1. That's the first year
> after his birth. The first millenium ended with with 1000th year after
> his birth, hence, the first year of the 2nd millenium was
> 1001. Ultimatively, the sequence of 'year numbers' comes into being as
> years are being counted: The number for each new years is 'number of
> years we have seen so far + 1'. There was never a year with 'number 0'
> because 0 was the state 'no years seen so far'.
No, there was never a year 0 because the Venemous Bede was working with
a number system which didn't have a zero. Any sensible modern calendar
does include a year or time 0 (usually 1 BC in year-based systems; other
systems like TAI count seconds rather than years and have an
arbitrarily-chosen 'time 0').
> OTOH, 'array indexing' is a surjective function F:X -> Y which maps
> 'some set of numbers' to 'some set of elements'.
Indeed. Exactly as year numbering is a function from the nonzero
integers to a set of periods of time.
Ben
------------------------------
Date: Tue, 25 Feb 2014 22:29:36 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: use strict; use warnings;
Message-Id: <4uOdnUQ_YPAt8JDOnZ2dnUVZ_t-dnZ2d@supernews.com>
On Tue, 25 Feb 2014 19:57:30 +0000, Ben Morrow wrote:
> Quoth Henry Law <news@lawshouse.org>:
>> On 24/02/14 17:32, Marek Novotny wrote:
>> > I actually think they are using the wrong word in their description.
>>
>> I actually think you are smarter than your instructors, even though
>> they know more than you do at present.
>
> I agree. In fact, if you're someone who can learn from books, you may
> well be better off forgetting about the Perl courses you have been
> offered and just sticking to Learning Perl, which is written by people
> who *do* know how to teach Perl. (Of course, there may be some
> administrative advantage to being able to say you've done an 'official'
> Perl course.)
>
> Ben
Well... books and a whole lot of help came from here. I take one course
at a time and see how it goes.
You guys are seriously awesome!
--
Marek Novotny
A member of the Linux Foundation
http://www.linuxfoundation.org
------------------------------
Date: Wed, 26 Feb 2014 14:43:37 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: use strict; use warnings;
Message-Id: <87mwhehr3a.fsf@sable.mobileactivedefense.com>
Ben Morrow <ben@morrow.me.uk> writes:
> Quoth Rainer Weikusat <rweikusat@mobileactivedefense.com>:
>> Ben Morrow <ben@morrow.me.uk> writes:
>> > Quoth Jrgen Exner <jurgenex@hotmail.com>:
>> >> Ben Morrow <ben@morrow.me.uk> wrote:
>> >> >It's not uncommon, when talking about languages like Perl which index
>> >> >from 0, to start the ordinals at 'zeroth'.
>> >>
>> >> Only by those people, who also celebrated the start of the third
>> >> millenium at the end of 1999.
>> >
>> > No, that's the opposite problem: the Gregorian calendar is 1-indexed.
>>
>> The Gregorian calender isn't indexed at all because time is not a
>> random-access sequence.
>
> Of course it is. If I open my diary at the page for 'June 2014' I am
> accessing a representation of a particular period of time using an
> index.
You are accessing 'your diary' according to some arbitrary indexing
scheme someone invented. Ergo, your diary is indexed. That doesn't mean
anything else is.
>> But it is a nice example to illustrate the
>> differnce: 1AD is the year were Christ turned 1. That's the first year
>> after his birth. The first millenium ended with with 1000th year after
>> his birth, hence, the first year of the 2nd millenium was
>> 1001. Ultimatively, the sequence of 'year numbers' comes into being as
>> years are being counted: The number for each new years is 'number of
>> years we have seen so far + 1'. There was never a year with 'number 0'
>> because 0 was the state 'no years seen so far'.
>
> No, there was never a year 0 because the Venemous Bede was working with
> a number system which didn't have a zero.
The likely reason why this numbering system didn't have zero was that
nobody saw any use for it. Considering this, it seems unlikely that the
Nondescript Whatnot had used zero to mean one had he only known about
it. But both your statement and my statement are "conjectural history",
ie speculations about what could have happened in the past instead of
what did happen.
> Any sensible modern calendar does include a year or time 0 (usually 1
> BC in year-based systems; other systems like TAI count seconds rather
> than years and have an arbitrarily-chosen 'time 0').
That's your misinterpretation of it. As explained above, 'time 0' really
means 'no time passed so far'.
Loosely related: There isn't really a 'minute 0' on the clock, that's
minute sixty of the past hour, after which the 'minute counter' wraps
around to zero because the arithmetic is performed in the mod 60
remainder ring.
I hereby proclaim a much more sensible system of counting that 1st, 2nd, 3rd,
..., one I call Kelvinism. It goes like this -273,15th, -272.15th,
-271.15th ... Not only does this get rid of this confusing array of
context-depenendent suffixes but it is firmly grounded in laws of
nature!.
------------------------------
Date: Wed, 26 Feb 2014 16:35:36 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: use strict; use warnings;
Message-Id: <onh1ua-o7d.ln1@anubis.morrow.me.uk>
Quoth Rainer Weikusat <rweikusat@mobileactivedefense.com>:
>
> > Any sensible modern calendar does include a year or time 0 (usually 1
> > BC in year-based systems; other systems like TAI count seconds rather
> > than years and have an arbitrarily-chosen 'time 0').
>
> That's your misinterpretation of it. As explained above, 'time 0' really
> means 'no time passed so far'.
No. ISO 8601 years number AD 1 as +1, 1 BC as +0, and 2 BC as -1. If it
was counting years before or after some point 1 BC would be -1.
> Loosely related: There isn't really a 'minute 0' on the clock, that's
> minute sixty of the past hour, after which the 'minute counter' wraps
> around to zero because the arithmetic is performed in the mod 60
> remainder ring.
There is no 60 in the mod 60 remainder ring. 60 % 60 = 0.
> I hereby proclaim a much more sensible system of counting that 1st, 2nd, 3rd,
> ..., one I call Kelvinism. It goes like this -273,15th, -272.15th,
> -271.15th ... Not only does this get rid of this confusing array of
> context-depenendent suffixes but it is firmly grounded in laws of
> nature!.
No, it's grounded in the rather arbitrary choice of the freezing point
of water as the basis of the Celsius scale.
Ben
------------------------------
Date: Wed, 26 Feb 2014 17:17:43 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: use strict; use warnings;
Message-Id: <87y50xhjyg.fsf@sable.mobileactivedefense.com>
Ben Morrow <ben@morrow.me.uk> writes:
> Quoth Rainer Weikusat <rweikusat@mobileactivedefense.com>:
>>
>> > Any sensible modern calendar does include a year or time 0 (usually 1
>> > BC in year-based systems; other systems like TAI count seconds rather
>> > than years and have an arbitrarily-chosen 'time 0').
>>
>> That's your misinterpretation of it. As explained above, 'time 0' really
>> means 'no time passed so far'.
>
> No. ISO 8601 years number AD 1 as +1, 1 BC as +0, and 2 BC as -1. If it
> was counting years before or after some point 1 BC would be -1.
For systems which measure passing time, 'time 0' means "we start to
measure now", ie, the POSIX timestamp 1 represents first second which
passed since the epoch. You're correct about ISO8601 'year numbers' but
this is a convention which has very likely been designed for 'ease of
processing' on computers.
>> Loosely related: There isn't really a 'minute 0' on the clock, that's
>> minute sixty of the past hour, after which the 'minute counter' wraps
>> around to zero because the arithmetic is performed in the mod 60
>> remainder ring.
>
> There is no 60 in the mod 60 remainder ring. 60 % 60 = 0.
... which is why the minute counter of a digital clock wraps to 0 after the
60th minute of an hour has passed ...
>> I hereby proclaim a much more sensible system of counting that 1st, 2nd, 3rd,
>> ..., one I call Kelvinism. It goes like this -273,15th, -272.15th,
>> -271.15th ... Not only does this get rid of this confusing array of
>> context-depenendent suffixes but it is firmly grounded in laws of
>> nature!.
>
> No, it's grounded in the rather arbitrary choice of the freezing point
> of water as the basis of the Celsius scale.
There's no Kelvinism but Kelvinism and the dirty heretics who speak of
'Celsian Kelvinism' in order to justify sticking to their
devil-inspired, pagan superstitions will be arianised in due time!
(I was actually envisioning the nice possibility of a theological
schism which is just as pointless, if not more, than the idea itself
here, as further illustration of the absurdity of the concept).
------------------------------
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 4155
***************************************