[33022] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4298 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 20 18:09:20 2014

Date: Mon, 20 Oct 2014 15:09:07 -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, 20 Oct 2014     Volume: 11 Number: 4298

Today's topics:
    Re: hash slice surprise (Tim McDaniel)
    Re: prime <jblack@nospam.com>
    Re: prime <rweikusat@mobileactivedefense.com>
    Re: prime <jblack@nospam.com>
    Re: prime <rweikusat@mobileactivedefense.com>
    Re: prime <gravitalsun@hotmail.foo>
    Re: prime <jblack@nospam.com>
    Re: prime <jblack@nospam.com>
    Re: prime <hjp-usenet3@hjp.at>
    Re: prime <hjp-usenet3@hjp.at>
    Re: prime <rweikusat@mobileactivedefense.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sun, 19 Oct 2014 22:50:58 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: hash slice surprise
Message-Id: <m21f8i$qm1$1@reader1.panix.com>

In article <d2e31695-3881-4061-9687-adf933645cbb@googlegroups.com>,
Marc Girod  <marc.girod@gmail.com> wrote:
>On Sunday, 19 October 2014 14:05:21 UTC+1, Peter J. Holzer  wrote:
>> Or you could read perldoc perldata:
>...
>> |      Slices in scalar context return the last item of the slice.
>
>Exactly what i missed.
>Writing is easy, reading is hard.

I sometimes have a hard time finding exactly which of the Perl man
pages has the datum I need, when it's not in the place I considered
"obvious".

-- 
Tim McDaniel, tmcd@panix.com


------------------------------

Date: Mon, 20 Oct 2014 14:50:32 -0500
From: John Black <jblack@nospam.com>
Subject: Re: prime
Message-Id: <MPG.2eaf2385b248e1a99897f7@news.eternal-september.org>

In article <m1phft$7ol$1@news.ntua.gr>, gravitalsun@hotmail.foo says...
> 
> > We do not have to search the space above sqrt(x) which is much
> 
> # the simple approach with sqrt is much faster.
> 
> 
> sub Find_the_prime_numbers_upto
> {
> return if $_[0]<2;
> my ($found,$N)=1; # say 2;
> 
> 	
> 	for (1 .. int ($_[0]-1)/2)
> 	{
> 	$N = 2*$_ + 1;
> 
> 		for (3 .. sqrt $N)
> 		{
> 		$N=0, last unless $N % $_
> 		}
> 
> 	$found++ if $N      # ;say $N if $N
> 	}
> $found
> }

George, can you please explain this thing with the comma?

> $N=0, last unless $N % $_

It looks like $N is being set to zero if ($N % $_) == 0, i.e. if the unless fails but I don't 
know why?  Thanks.

John Black


------------------------------

Date: Mon, 20 Oct 2014 21:01:31 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: prime
Message-Id: <87lhoa8q90.fsf@doppelsaurus.mobileactivedefense.com>

John Black <jblack@nospam.com> writes:
> In article <m1phft$7ol$1@news.ntua.gr>, gravitalsun@hotmail.foo says...
>> 
>> > We do not have to search the space above sqrt(x) which is much
>> 
>> # the simple approach with sqrt is much faster.
>> 
>> 
>> sub Find_the_prime_numbers_upto
>> {
>> return if $_[0]<2;
>> my ($found,$N)=1; # say 2;
>> 
>> 	
>> 	for (1 .. int ($_[0]-1)/2)
>> 	{
>> 	$N = 2*$_ + 1;
>> 
>> 		for (3 .. sqrt $N)
>> 		{
>> 		$N=0, last unless $N % $_
>> 		}
>> 
>> 	$found++ if $N      # ;say $N if $N
>> 	}
>> $found
>> }
>
> George, can you please explain this thing with the comma?
>
>> $N=0, last unless $N % $_
>
> It looks like $N is being set to zero if ($N % $_) == 0, i.e. if the unless fails but I don't 
> know why?

There's a statement here (an expression statement, to be precised) which
is

$N=0, last

The , evaluates its left-hand argument and discards any value returned
by that, followed be evaluating the right-hand argument and returning
whatever value that returned. The left-hand argument is

$N=0

hence, evaluating that sets $N to 0 and the right-hand argument is

last

evaluating that terminates execution of the current block (the
for (3 .. sqrt $N) { } loop in this case.

This statement has a statement modifier attached which is

unless $N % $_

meaning, the $N=0, last is only executed if $N % $_ returns a value
which counts as 'false'. % returns 0 (which counts as false) in case the
left argument was evenly divisible by the right (no remainder), hence,
the complete statements means

"if $N is evenly divisible by $_, set $N to 0 and exit the loop"


------------------------------

Date: Mon, 20 Oct 2014 15:47:32 -0500
From: John Black <jblack@nospam.com>
Subject: Re: prime
Message-Id: <MPG.2eaf30dc268b8c569897f8@news.eternal-september.org>

In article <87lhoa8q90.fsf@doppelsaurus.mobileactivedefense.com>, 
rweikusat@mobileactivedefense.com says...
> > George, can you please explain this thing with the comma?
> >
> >> $N=0, last unless $N % $_
> >
> > It looks like $N is being set to zero if ($N % $_) == 0, i.e. if the unless fails but I don't 
> > know why?
> 
> There's a statement here (an expression statement, to be precised) which
> is
> 
> $N=0, last
> 
> The , evaluates its left-hand argument and discards any value returned
> by that, followed be evaluating the right-hand argument and returning
> whatever value that returned. The left-hand argument is

You are saying it does the left side first "followed by evaluating the right" side.  But if 
it did that, it would set $N to 0 before noticing that the right side has a modifier so it 
can't really work that way.  If the "unless" fails, then $N is not being set to 0 so the left 
side must be executed *after* the right side, and somehow NOT be executed if the statement on 
the right fails?

John Black




------------------------------

Date: Mon, 20 Oct 2014 22:16:39 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: prime
Message-Id: <87d29m8mrs.fsf@doppelsaurus.mobileactivedefense.com>

John Black <jblack@nospam.com> writes:
> In article <87lhoa8q90.fsf@doppelsaurus.mobileactivedefense.com>, 
> rweikusat@mobileactivedefense.com says...
>> > George, can you please explain this thing with the comma?
>> >
>> >> $N=0, last unless $N % $_
>> >
>> > It looks like $N is being set to zero if ($N % $_) == 0, i.e. if the unless fails but I don't 
>> > know why?
>> 
>> There's a statement here (an expression statement, to be precised) which
>> is
>> 
>> $N=0, last
>> 
>> The , evaluates its left-hand argument and discards any value returned
>> by that, followed be evaluating the right-hand argument and returning
>> whatever value that returned. The left-hand argument is
>
> You are saying it does the left side first "followed by evaluating the right" side.  But if 
> it did that, it would set $N to 0 before noticing that the right side has a modifier so it 
> can't really work that way.

You're misunderstanding this.

$N=0, last

is the statement and the modifier applies to that.


------------------------------

Date: Tue, 21 Oct 2014 00:20:46 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: prime
Message-Id: <m23ubl$2nlh$1@news.ntua.gr>

On 20/10/2014 10:50 μμ, John Black wrote:
> In article <m1phft$7ol$1@news.ntua.gr>, gravitalsun@hotmail.foo says...
>>
>>> We do not have to search the space above sqrt(x) which is much
>>
>> # the simple approach with sqrt is much faster.
>>
>>
>> sub Find_the_prime_numbers_upto
>> {
>> return if $_[0]<2;
>> my ($found,$N)=1; # say 2;
>>
>> 	
>> 	for (1 .. int ($_[0]-1)/2)
>> 	{
>> 	$N = 2*$_ + 1;
>>
>> 		for (3 .. sqrt $N)
>> 		{
>> 		$N=0, last unless $N % $_
>> 		}
>>
>> 	$found++ if $N      # ;say $N if $N
>> 	}
>> $found
>> }
>
> George, can you please explain this thing with the comma?
>
>> $N=0, last unless $N % $_
>
> It looks like $N is being set to zero if ($N % $_) == 0, i.e. if the unless fails but I don't
> know why?  Thanks.
>
> John Black
>




ok you are right its an ugly (but very fast) hack.
The normal (slower) equivelant is


	if ( 0 == $N % $_ )
	{
	$N=0;
	last
	}




------------------------------

Date: Mon, 20 Oct 2014 16:31:54 -0500
From: John Black <jblack@nospam.com>
Subject: Re: prime
Message-Id: <MPG.2eaf3b42aaabf4639897f9@news.eternal-september.org>

In article <87d29m8mrs.fsf@doppelsaurus.mobileactivedefense.com>, 
rweikusat@mobileactivedefense.com says...
> John Black <jblack@nospam.com> writes:
> > In article <87lhoa8q90.fsf@doppelsaurus.mobileactivedefense.com>, 
> > rweikusat@mobileactivedefense.com says...
> >> > George, can you please explain this thing with the comma?
> >> >
> >> >> $N=0, last unless $N % $_
> >> >
> >> > It looks like $N is being set to zero if ($N % $_) == 0, i.e. if the unless fails but I don't 
> >> > know why?
> >> 
> >> There's a statement here (an expression statement, to be precised) which
> >> is
> >> 
> >> $N=0, last
> >> 
> >> The , evaluates its left-hand argument and discards any value returned
> >> by that, followed be evaluating the right-hand argument and returning
> >> whatever value that returned. The left-hand argument is
> >
> > You are saying it does the left side first "followed by evaluating the right" side.  But if 
> > it did that, it would set $N to 0 before noticing that the right side has a modifier so it 
> > can't really work that way.
> 
> You're misunderstanding this.
> 
> $N=0, last
> 
> is the statement and the modifier applies to that.

Ok, its making more sense, thanks.  Is it correct to say then that two statements separated 
by a comma, behave as if they are one statement?  At least in the sense that a modifier like 
"unless" or "if" will apply to both of them?  If so, I don't like it from a readability 
standpoint at least without some clarifying parentheses because its non-obvious that the 
"unless" will bind to both the statements and not just to the "last" statement.

John Black


------------------------------

Date: Mon, 20 Oct 2014 16:36:35 -0500
From: John Black <jblack@nospam.com>
Subject: Re: prime
Message-Id: <MPG.2eaf3c60686af6749897fa@news.eternal-september.org>

In article <m23ubl$2nlh$1@news.ntua.gr>, gravitalsun@hotmail.foo says...
> 
> On 20/10/2014 10:50 µµ, John Black wrote:
> > In article <m1phft$7ol$1@news.ntua.gr>, gravitalsun@hotmail.foo says...
> >>
> >>> We do not have to search the space above sqrt(x) which is much
> >>
> >> # the simple approach with sqrt is much faster.
> >>
> >>
> >> sub Find_the_prime_numbers_upto
> >> {
> >> return if $_[0]<2;
> >> my ($found,$N)=1; # say 2;
> >>
> >> 	
> >> 	for (1 .. int ($_[0]-1)/2)
> >> 	{
> >> 	$N = 2*$_ + 1;
> >>
> >> 		for (3 .. sqrt $N)
> >> 		{
> >> 		$N=0, last unless $N % $_
> >> 		}
> >>
> >> 	$found++ if $N      # ;say $N if $N
> >> 	}
> >> $found
> >> }
> >
> > George, can you please explain this thing with the comma?
> >
> >> $N=0, last unless $N % $_
> >
> > It looks like $N is being set to zero if ($N % $_) == 0, i.e. if the unless fails but I don't
> > know why?  Thanks.
> >
> > John Black
> >
> 
> 
> 
> 
> ok you are right its an ugly (but very fast) hack.
> The normal (slower) equivelant is
> 
> 
> 	if ( 0 == $N % $_ )
> 	{
> 	$N=0;
> 	last
> 	}

George, thanks.  You are right that this code is noticably slower.  But I experimented and 
found that

   if (!($N % $_)) {
      $N = 0; last;
   }

performs just as well as the comma version.  Somehow the comparision to exactly 0 slows it 
way down though...

John Black


------------------------------

Date: Mon, 20 Oct 2014 23:44:58 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: prime
Message-Id: <slrnm4b0is.tai.hjp-usenet3@hrunkner.hjp.at>

On 2014-10-20 19:50, John Black <jblack@nospam.com> wrote:
> In article <m1phft$7ol$1@news.ntua.gr>, gravitalsun@hotmail.foo says...
>> sub Find_the_prime_numbers_upto
>> {
>> return if $_[0]<2;
>> my ($found,$N)=1; # say 2;
>> 
>> 	
>> 	for (1 .. int ($_[0]-1)/2)
>> 	{
>> 	$N = 2*$_ + 1;
>> 
>> 		for (3 .. sqrt $N)
>> 		{
>> 		$N=0, last unless $N % $_
>> 		}
>> 
>> 	$found++ if $N      # ;say $N if $N
>> 	}
>> $found
>> }

George, can you please, please, please indent your code in a sane
manner?


> George, can you please explain this thing with the comma?
>
>> $N=0, last unless $N % $_
>
> It looks like $N is being set to zero if ($N % $_) == 0, i.e. if the unless fails

Yes.

> but I don't know why?  Thanks.

because $N is the number currently being examined and if it is divisable
by $_ (i.e. any other integer) it is not a prime. So $N is set to zero
(known not to be prime and tested for after the loop) and the loop is
exited. If the the loop reaches the end without $N being set to 0, then
$N is a prime.

        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: Mon, 20 Oct 2014 23:48:15 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: prime
Message-Id: <slrnm4b0p0.tai.hjp-usenet3@hrunkner.hjp.at>

On 2014-10-20 20:47, John Black <jblack@nospam.com> wrote:
> In article <87lhoa8q90.fsf@doppelsaurus.mobileactivedefense.com>, 
> rweikusat@mobileactivedefense.com says...
>> > George, can you please explain this thing with the comma?
>> >
>> >> $N=0, last unless $N % $_
>> >
>> > It looks like $N is being set to zero if ($N % $_) == 0, i.e. if the unless fails but I don't 
>> > know why?
>> 
>> There's a statement here (an expression statement, to be precised) which
>> is
>> 
>> $N=0, last
>> 
>> The , evaluates its left-hand argument and discards any value returned
>> by that, followed be evaluating the right-hand argument and returning
>> whatever value that returned. The left-hand argument is
>
> You are saying it does the left side first "followed by evaluating the right" side.

No. 

    statement unless condition;

is equivalent to 

    unless (condition) {
        statement
    }

(except for scope).

So

    $N=0, last unless $N % $_;

is equivalent to

    unless ($N % $_) {
        $N=0, last;
    }

Just written differently, just like you can switch the order in many
natural languages.

        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: Mon, 20 Oct 2014 22:52:08 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: prime
Message-Id: <871tq28l4n.fsf@doppelsaurus.mobileactivedefense.com>

John Black <jblack@nospam.com> writes:
> In article <87d29m8mrs.fsf@doppelsaurus.mobileactivedefense.com>, 
> rweikusat@mobileactivedefense.com says...
>> John Black <jblack@nospam.com> writes:
>> > In article <87lhoa8q90.fsf@doppelsaurus.mobileactivedefense.com>, 
>> > rweikusat@mobileactivedefense.com says...
>> >> > George, can you please explain this thing with the comma?
>> >> >
>> >> >> $N=0, last unless $N % $_
>> >> >
>> >> > It looks like $N is being set to zero if ($N % $_) == 0, i.e. if the unless fails but I don't 
>> >> > know why?
>> >> 
>> >> There's a statement here (an expression statement, to be precised) which
>> >> is
>> >> 
>> >> $N=0, last
>> >> 
>> >> The , evaluates its left-hand argument and discards any value returned
>> >> by that, followed be evaluating the right-hand argument and returning
>> >> whatever value that returned. The left-hand argument is
>> >
>> > You are saying it does the left side first "followed by evaluating the right" side.  But if 
>> > it did that, it would set $N to 0 before noticing that the right side has a modifier so it 
>> > can't really work that way.
>> 
>> You're misunderstanding this.
>> 
>> $N=0, last
>> 
>> is the statement and the modifier applies to that.
>
> Ok, its making more sense, thanks.  Is it correct to say then that two statements separated 
> by a comma, behave as if they are one statement?  At least in the sense that a modifier like 
> "unless" or "if" will apply to both of them?

You're confusing statements and expressions here: The 'comma operator'
is a binary operator taking two expressions as arguments and the whole
construct is 'a simple statement', an expression evaluated because of
its side effects. And the statement modifier applies to the statement
and not its component expressions. This is really no different from

$a += $b if $c < 34;

$a += $b is a simple statement (based on another binary operator) and it
is conditionally executed because of the attached statement
modifier. It's not something like

$a += ($c < 34 ? $b : die("Falling over the edge of the world! Aaaargh!"));




------------------------------

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 4298
***************************************


home help back first fref pref prev next nref lref last post