[33105] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4381 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Mar 1 09:09:18 2015

Date: Sun, 1 Mar 2015 06:09:04 -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, 1 Mar 2015     Volume: 11 Number: 4381

Today's topics:
    Re: del old files and empty dirs (Seymour J.)
    Re: function returning a list of the indices of its tru <rweikusat@mobileactivedefense.com>
    Re: function returning a list of the indices of its tru <gravitalsun@hotmail.foo>
    Re: function returning a list of the indices of its tru <gamo@telecable.es>
    Re: function returning a list of the indices of its tru <rweikusat@mobileactivedefense.com>
    Re: function returning a list of the indices of its tru <gravitalsun@hotmail.foo>
    Re: function returning a list of the indices of its tru <rweikusat@mobileactivedefense.com>
    Re: function returning a list of the indices of its tru <gamo@telecable.es>
    Re: function returning a list of the indices of its tru <gravitalsun@hotmail.foo>
    Re: function returning a list of the indices of its tru <rweikusat@mobileactivedefense.com>
    Re: How does this bizarre script work? <rweikusat@mobileactivedefense.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 27 Feb 2015 11:54:05 -0500
From: Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>
Subject: Re: del old files and empty dirs
Message-Id: <54f0a12d$21$fuzhry+tra$mr2ice@news.patriot.net>

In <87vbipeh1h.fsf@doppelsaurus.mobileactivedefense.com>, on
02/25/2015
   at 10:56 PM, Rainer Weikusat <rweikusat@mobileactivedefense.com>
said:

>Leaving questions about sensible style at the statement level 
>aside, I think subroutines shouldn't check their arguments at run 
>time:

ObAlanSherman

>Such checks should be done while compiling the code

That's not possible, not even in a strongly typed language, much less
a language with dynamic typing.

>and insofar the compiler can't do enough, remaining cases of 
>"wrong calls" should be sorted out by debugging.

Using the end user as a debug tool is wrong. The proper approach for
production software is belt-and-suspenders. But it's not my dog.

-- 
Shmuel (Seymour J.) Metz, SysProg and JOAT  <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action.  I reserve the
right to publicly post or ridicule any abusive E-mail.  Reply to
domain Patriot dot net user shmuel+news to contact me.  Do not
reply to spamtrap@library.lspace.org



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

Date: Fri, 27 Feb 2015 16:33:03 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <87r3tbiabk.fsf@doppelsaurus.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> On 26/2/2015 11:31 μμ, Rainer Weikusat wrote:
>> sub trues
>> {
>>      map { $_[$_] ? $_ : () } 0 .. $#_;
>> }
>>
>> I actually needed this for something and found it rather amusing.
>>
>
> sub trues
> {
> splice
> @_, $_, 1, $_[$_] ? $_ : '' for 0 .. $#_;
> @_
> }

This would need a post-processing step to work as intended, to get rid
the empty strings in the positions where the false-values resided
before.


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

Date: Fri, 27 Feb 2015 22:52:59 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <mcqlfa$14re$1@news.ntua.gr>

>
> This would need a post-processing step to work as intended, to get rid
> the empty strings in the positions where the false-values resided
> before.
>


here we go again

sub trues2
{
$_[$_] ? splice @_, $_, 1, $_: splice @_, $_, 1 for reverse 0 .. $#_;
@_
}


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

Date: Fri, 27 Feb 2015 22:26:37 +0100
From: gamo <gamo@telecable.es>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <mcqneb$c23$1@speranza.aioe.org>

El 27/02/15 a las 15:28, Rainer Weikusat escribió:
>> What's wrong with this?
> What is wrong with my English, too: I'm not a native speaker, hence, I'm
> prone to using phrases/ collocation appearing rather bizarre to native
> speakers without even realizing this and said 'native speakers' then either
> don't understand me or believe me to be some kind of half-wit.

Sorry, but I don't think neither misunderstood you or that you are 
half-wit. On the contrary, you are very clever and ingenious, so
I'm afraid for not taking your short cuts. Excuse me.



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

Date: Fri, 27 Feb 2015 21:26:42 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <878ufjhwq5.fsf@doppelsaurus.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
>>
>> This would need a post-processing step to work as intended, to get rid
>> the empty strings in the positions where the false-values resided
>> before.
>>
>
> here we go again
>
> sub trues2
> {
> $_[$_] ? splice @_, $_, 1, $_: splice @_, $_, 1 for reverse 0 .. $#_;
> @_
> }

I played with this for some time but broke it off after realizing that
this would need to be done backwords and without thinking of applying
reverse to the index list. It's not really necessary to invoke splice
twice, the expression I was using in the map { } works in an argument
list, too,

sub trues
{
    splice(@_, $_, 1, $_[$_] ? $_ : ()) for reverse(0 .. $#_);
    @_;
}

coming to think of it, my idea of earlier can also be made to work

sub trues
{
    splice(@_, -$_, 1, $_[-$_] ? -$_ : ()) for -$#_ .. 0;
    @_;
}

but that's (all) a curiosity I wouldn't want to use in actual code.


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

Date: Sat, 28 Feb 2015 00:24:33 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <mcqqr0$1k29$1@news.ntua.gr>


> coming to think of it, my idea of earlier can also be made to work
>
> sub trues
> {
>      splice(@_, -$_, 1, $_[-$_] ? -$_ : ()) for -$#_ .. 0;
>      @_;
> }
>

thats good !



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

Date: Fri, 27 Feb 2015 22:43:30 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <874mq7ht65.fsf@doppelsaurus.mobileactivedefense.com>

gamo <gamo@telecable.es> writes:
> El 27/02/15 a las 15:28, Rainer Weikusat escribió:
>>> What's wrong with this?
>> What is wrong with my English, too: I'm not a native speaker, hence, I'm
>> prone to using phrases/ collocation appearing rather bizarre to native
>> speakers without even realizing this and said 'native speakers' then either
>> don't understand me or believe me to be some kind of half-wit.
>
> Sorry, but I don't think neither misunderstood you or that you are
> half-wit. On the contrary, you are very clever and ingenious, so
> I'm afraid for not taking your short cuts. Excuse me.

I assure you I make as many stupid mistakes as the next guy. Trying to
word this in a different way: The code you posted was

sub trues {
	my @list = @_;
	my @result;
	for my $i (0..$#list){
		if ($list[$i]){
			push @result, $i;
		}else{
			# nothing
		}
	}
	return @results;
}

That's a very orderly and conventional approach. You start with getting
all the input and stuffing it into a box with a name and get another box
with a name for the output. Then, you go through the index numbers of
the items in the input box, using another properly named box for the
current one, look at the corresponding item and stuff the appropriate
thing into the output box. Afterwards, you return whatever is in the
output box. The first thing to note here would be that the names on the
boxes don't exist because boxes have to be names made up of letters but
because they help with identifying the boxes. @list is a totally generic
name, OTOH, @_ also names a list, namely, the current argument
list. Similar to this, $i is also a generic name, meaning 'the current
index'. But $_ is really just as good in a Perl foreach loop. This
yields (all code untested)

sub trues
{
	my @results;

        for (0 .. $#_) {
        	if ($_[$_]) {
                	push(@results, $_);
		} else {
                	# what's the purpose of this?
		}
	}

        return @results;
}

The next observation would be that the else-branch just sits there for
sitting there, so, let's do away with it

sub trues
{
	my @results;

        for (0 .. $#_) {
        	if ($_[$_]) {
                	push(@results, $_);
		}
	}

        return @results;
}

Harking back to the first point, the {} also don't exist because they're
curly, the denote a block. But what's the use of a block with only a
single statement in it?

sub trues
{
	my @results;

        for (0 .. $#_) {
        	$_[$_] and push(@results, $_);
        }

        return @results;
}

Still too curly,

sub trues
{
	my @results;
        $_[$_] and push(@results, $_) for 0 .. $#_;
        return @results;
}

After having gotten all the redundant names and punctuation out of the
way, let's have a look at what this actually does: It translates an
input list (0 .. $#_) to an output list (@results) based on the values
of another list (@_). Perl has two expressions for doing this, a general
one (map) which can apply any kind of translation function to the
elements on the input list, and a more specific one, (grep), which
puts a value from the input list onto the output list if some predicate
is true. Which is exactly what the code above is doing in a more
explicit way:

sub trues
{
	grep { $_[$_] } 0 .. $#_;
}

(I've never used grep for anything so far, hence, I thought of map and
didn't think of grep).




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

Date: Sat, 28 Feb 2015 07:28:41 +0100
From: gamo <gamo@telecable.es>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <mcrn6q$45g$1@speranza.aioe.org>

El 27/02/15 a las 23:43, Rainer Weikusat escribió:
> sub trues
> {
> 	grep { $_[$_] } 0 .. $#_;
> }


Thanks for the explanation.

              Rate classic rainers
classic 2158107/s      --    -45%
rainers 3949984/s     83%      --


-- 
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance


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

Date: Sat, 28 Feb 2015 15:02:59 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <mcsea4$q6k$1@news.ntua.gr>

>
> sub trues
> {
>      splice(@_, -$_, 1, $_[-$_] ? -$_ : ()) for -$#_ .. 0;
>      @_;
> }


my $i=0;
my @A;
join("\0", @_)  =~/[^\0+](??{ push @A,$i if $& ; $i++ })/g;
@A;



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

Date: Sat, 28 Feb 2015 19:53:22 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <874mq5q0ct.fsf@doppelsaurus.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
>>
>> sub trues
>> {
>>      splice(@_, -$_, 1, $_[-$_] ? -$_ : ()) for -$#_ .. 0;
>>      @_;
>> }
>
> my $i=0;
> my @A;
> join("\0", @_)  =~/[^\0+](??{ push @A,$i if $& ; $i++ })/g;
> @A;

I liked the splice-expression (I was actually looking forward to
understanding it on the day after the evening you wrote it) but this is
just the usual

"wife-not-at-home-lets-turn-the-washing-machine-into-lawnmower-with-integrated-air-condition"

stuff ...




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

Date: Fri, 27 Feb 2015 16:55:23 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How does this bizarre script work?
Message-Id: <87ioeni9ac.fsf@doppelsaurus.mobileactivedefense.com>

Eli the Bearded <*@eli.users.panix.com> writes:
> In comp.lang.perl.misc, Robbie Hatley  <see.my.sig@for.my.address> wrote:
>> On 2/24/2015 1:50 PM, Rainer Weikusat wrote:
>> 
>> > *{"\0\0\0"} = \"\0";
>> > print(ord(substr(${"\0\0\0"}, 0, 1)), "\n");
>> 
>> Triple-Null "identifier", eh?  Fine.  But why stop there?
>> Symbol table hashes apparently can use ANYTHING as a key,
>> even an entire valid Perl program such as:
>> say "Cheese!";
>
> The syntax you need to wrap that stuff with gives it away.
>
>    $ cat /tmp/yes
>    #!/usr/local/bin/perl5.20.0
>    use warnings;
>    use strict;
>
>    $_ = "Just another perl hacker,\n";
>
>    sperlPerlg;
>
>    print;
>    __END__
>    $ /tmp/yes
>    Just another Perl hacker,
>    $ grep s.perl.Perl.g /tmp/yes
>    sperlPerlg;
>    $ 
>
> That's right, you can use unprintable characters as delimitors in your
> code.

I realize this is sort-of silly but I couldn't resist doing it
nevertheless:

[rw@doppelsaurus]/tmp#cat b.pl 
@j = ("Just another Perl hacker\n");
{}
{}
print eval;
[rw@doppelsaurus]/tmp#perl b.pl
Just another Perl hacker


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

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


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