[33059] in Perl-Users-Digest
Perl-Users Digest, Issue: 4335 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Dec 25 03:09:17 2014
Date: Thu, 25 Dec 2014 00:09:03 -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 Thu, 25 Dec 2014 Volume: 11 Number: 4335
Today's topics:
Re: Both substitute and filter <bauhaus@futureapps.invalid>
Re: Both substitute and filter <rweikusat@mobileactivedefense.com>
Re: Both substitute and filter <bauhaus@futureapps.invalid>
Re: fields separation <rweikusat@mobileactivedefense.com>
Re: fields separation <rweikusat@mobileactivedefense.com>
Re: Sort on regexp match <news@todbe.com>
Re: Sort on regexp match <derykus@gmail.com>
Re: Sort on regexp match <derykus@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 24 Dec 2014 10:43:19 +0100
From: Georg Bauhaus <bauhaus@futureapps.invalid>
Subject: Re: Both substitute and filter
Message-Id: <m7e1r1$k0v$1@dont-email.me>
On 22.12.14 20:31, Mart van de Wege wrote:
> Georg Bauhaus <bauhaus@futureapps.invalid> writes:
>
>>
>>> Conciseness is a virtue in helping understanding too.
>>
>> my @foo = ();
>>
>> is a concise way of reporting the initial value to the (presumed)
>> average programmer.
>>
>> my $foo = [];
>>
>> is a concise way of saying a very similar thing. Do you suggest
>> to write
>>
>> my $list_of_foo;
>>
>> instead?
>
> No, because those two case are not similar at all.
Indeed a very good example of Perl blinkers, which I think
may explain why some programmers still want these introductions
of variables to have the same "look". Not similar, you say?
The difference between the texts
1) "my @foo = ();"
2) "my $foo = [];"
is very small, that is, they are similar in several ways:
- The first difference is a between two "odd" symbols in the same place
(the sigils)
- The second difference is between a different pair of brackets,
also in the same place.
And both pairs of brackets will in effect be creating a list!
While there are technicalities suggesting a semantic difference,
and rightly so in the Perl sense, it seems rather exaggerated to me
to make that technicality the cornerstone of dismissing a whole
style of programming. Namely, the style of indicating initial values,
even empty or zero, by assigning after declaration. Worse, the redundancy
occurs in just a *single* case: the assignment of "zeros" of the type
(i.e. arrays, scalars here) is redundant. A single case does not seem
enough justification to say that the style is wrong.
Anyway,
Happy holidays.
------------------------------
Date: Wed, 24 Dec 2014 10:55:53 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Both substitute and filter
Message-Id: <873885gvuu.fsf@doppelsaurus.mobileactivedefense.com>
Georg Bauhaus <bauhaus@futureapps.invalid> writes:
> On 22.12.14 20:31, Mart van de Wege wrote:
>> Georg Bauhaus <bauhaus@futureapps.invalid> writes:
>>
>>>
>>>> Conciseness is a virtue in helping understanding too.
>>>
>>> my @foo = ();
>>>
>>> is a concise way of reporting the initial value to the (presumed)
>>> average programmer.
>>>
>>> my $foo = [];
>>>
>>> is a concise way of saying a very similar thing. Do you suggest
>>> to write
>>>
>>> my $list_of_foo;
>>>
>>> instead?
>>
>> No, because those two case are not similar at all.
>
> Indeed a very good example of Perl blinkers, which I think
> may explain why some programmers still want these introductions
> of variables to have the same "look". Not similar, you say?
> The difference between the texts
>
> 1) "my @foo = ();"
> 2) "my $foo = [];"
>
> is very small, that is, they are similar in several ways:
>
> - The first difference is a between two "odd" symbols in the same place
> (the sigils)
>
> - The second difference is between a different pair of brackets,
> also in the same place.
>
> And both pairs of brackets will in effect be creating a list!
>
> While there are technicalities suggesting a semantic difference,
> and rightly so in the Perl sense,
Georg Bauhaus and Georgs Hausbau are also quite similar. Surely, only an
ignorant would insist they mean something different!
------------------------------
Date: Wed, 24 Dec 2014 15:01:26 +0100
From: Georg Bauhaus <bauhaus@futureapps.invalid>
Subject: Re: Both substitute and filter
Message-Id: <m7egv0$93v$1@dont-email.me>
On 22.12.14 23:33, Rainer Weikusat wrote:
> Georg Bauhaus <bauhaus@futureapps.invalid> writes:
>
> [...]
>
>> my @foo = ();
>>
>> is a concise way of reporting the initial value to the (presumed)
>> average programmer.
>
> It isn't:
>
> my @foo = ();
>
> is a Perl expression statement and it doesn't have any 'natural' meaning
> outside of the semantics assigned to it by the language.
A 'natural' meaning, being technically as incorrect as common sense can be,
is one which the "(presumed) average programmer" will associate with the
above. Mostly harmless. What Perl will associate with the text simply
deserves to be known, e.g. from one of your technically oriented postings,
if not from carefully studying the reference documentation. Any insight
gained will be measured against the other goals of the program's author.
> Of course, someone with a list a bit of experience with other
> programming languages than Perl (whether or not this is true for 'an
> average Perl programmer' is a good question) will easily come up with a
> couple of probable conjectures about the motivation behind this, eg
> 'knee-jerk addition made by someone more used to languages where all
> local variables have to be initialized prior to first use' but that's
> only a conjecture and certainty can only be provided by 'real'
> documentation explaining the reasoning behind the meaningless
> assignment. Not only doesn't this document anything, since it defies
> reason, it better be documented itself.
Granted, ignoring the author's style can make superfluous assignments
(self-explanatory?) annoy the Perl savant's momentary focus of reasoning,
But the details of Perl's definitions appear to be about as subjective
and artificial as any conjectured or real (as reported) motivation driving
a style. It's language design.
Arguing in favor of a style is just more complex than drawing conclusions
from Perl's existing definition only, at least if style is not simply
"doing as the king does".
I suppose that, tonight, many will now want to do what those three kings
started, so I should leave it at that.
------------------------------
Date: Tue, 23 Dec 2014 21:11:07 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: fields separation
Message-Id: <87d27at6l0.fsf@doppelsaurus.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
> Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
>
>> Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
>>> George Mpouras <gravitalsun@hotmail.foo> writes:
>>>> Find fields using any separator e.g <*> if the input text is
>>
>> [...]
>>
>>> Something I believe to work within these limits
>>
>> First bug: '0' (string of length 1 containing a '0 character') counts as
>> false in Perl.
>
> [...]
>
> Second bug: In case the separator starts with \, it needs to be escaped
> in $re so that it doesn't escape the \ of the \n:
"More bugs" (one of them serious enough to warrant a fourth): The posted
code doesn't work with separators of length 1. I hereby declare that it
intentionally adds an empty field whenever a separator not preceeded by
anything but possibly another separator is encountered. Lastly, the
check if a there's a non-empty trailing field after the loop also needs
to look at the length of the string.
Corrected subroutine
--------------------
sub find_fields
{
my ($next_block, $sep) = @_;
my ($in, $field, $gather, $sp, $re);
my @fields;
$gather = 1;
$re = substr($sep, 0, 1);
$re = '\\\\' if $re eq '\\';
$re = '^([^'.$re.'\n]+)';
OUTER:
while (length($in) || $next_block->($in)) {
if ($gather) {
$in =~ /$re/ and do {
$field .= $1;
substr($in, 0, length($1), '');
next;
};
$in =~ /^(\n+)/ and do {
substr($in, 0, length($1), '');
next;
};
$sp = 0;
$gather = 0;
}
while (substr($in, 0, 1) eq substr($sep, $sp, 1)) {
substr($in, 0, 1, '');
if (++$sp == length($sep)) {
push(@fields, $field);
$field = '';
$gather = 1;
next OUTER;
}
next OUTER unless length($in);
}
$in =~ /^(\n+)/ and do {
substr($in, 0, length($1), '');
next;
};
$field .= substr($sep, 0, $sp);
$gather = 1;
}
$field .= substr($sep, 0, $sp) unless $gather;
push(@fields, $field) if length($field);
return @fields;
}
------------------------------
Date: Tue, 23 Dec 2014 21:16:45 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: fields separation
Message-Id: <878uhyt6bm.fsf@doppelsaurus.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
[...]
> this may still be terribly wasteful if perl can't delete characters at
> the beginning of a string efficiently which it probably can't):
"Should have checked this earlier". Both 5.10.1 and 5.14.2 can:
[rw@doppelsaurus]/tmp#perl -MDevel::Peek -e '$s = "Dum Dideldidi"; Dump($s); substr($s, 0, 4, ""); Dump($s)'
SV = PV(0x604c20) at 0x623940
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x61cd60 "Dum Dideldidi"\0
CUR = 13
LEN = 16
SV = PV(0x604c20) at 0x623940
REFCNT = 1
FLAGS = (POK,OOK,pPOK)
OFFSET = 4
PV = 0x61cd64 ( "Dum\4" . ) "Dideldidi"\0
CUR = 9
LEN = 12
------------------------------
Date: Tue, 23 Dec 2014 15:42:19 -0800
From: "$Bill" <news@todbe.com>
Subject: Re: Sort on regexp match
Message-Id: <5499FDDB.7020003@todbe.com>
On 12/22/2014 20:39, Tim McDaniel wrote:
> Yeah, another "what's the smoothest way to do this in Perl?" question.
>
> I have a bunch of elements in an array along the lines of
> {IDENTIFIER}{SUFFIX} was last touched on {DATE}
> E.g.
> FROBx3 was last touched on 2014-04-11
> FROBx1 was last touched on 2014-03-17
> NARFj5 was last touched on 2014-06-01
> Fixed-width at the moment, as it happens, but certainly
>
> I wanted to sort by the suffix, and then by the line.
If the FROB field is fixed width this would be a simple way:
my @result = sort { substr ($a, 4, 2) cmp substr ($b, 4, 2) || $a cmp $b } @data;
------------------------------
Date: Wed, 24 Dec 2014 06:37:37 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Sort on regexp match
Message-Id: <778df34e-6b6a-450c-9c35-e394f36f2a40@googlegroups.com>
On Monday, December 22, 2014 8:39:09 PM UTC-8, Tim McDaniel wrote:
> Yeah, another "what's the smoothest way to do this in Perl?" question.
>
> I have a bunch of elements in an array along the lines of
> {IDENTIFIER}{SUFFIX} was last touched on {DATE}
> E.g.
> FROBx3 was last touched on 2014-04-11
> FROBx1 was last touched on 2014-03-17
> NARFj5 was last touched on 2014-06-01
> Fixed-width at the moment, as it happens, but certainly
>
> I wanted to sort by the suffix, and then by the line.
> I tried something along the lines of
> sort {
> ($::a ~= /(..) /) cmp ($::b ~= /(..) /) ||
> $::a cmp $::b
> }
> ...
$re = qr/(..)/;
sort { ($a=~/$re/,$1)[0] cmp ($b=~/$re/,$1)[0]
or $a cmp $b }
--
Charles DeRykus
------------------------------
Date: Wed, 24 Dec 2014 07:03:25 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Sort on regexp match
Message-Id: <f2eeffde-eb87-4527-a7f5-92551958f615@googlegroups.com>
On Wednesday, December 24, 2014 6:37:41 AM UTC-8, C.DeRykus wrote:
> On Monday, December 22, 2014 8:39:09 PM UTC-8, Tim McDaniel wrote:
> > Yeah, another "what's the smoothest way to do this in Perl?" question.
> >
> > I have a bunch of elements in an array along the lines of
> > {IDENTIFIER}{SUFFIX} was last touched on {DATE}
> > E.g.
> > FROBx3 was last touched on 2014-04-11
> > FROBx1 was last touched on 2014-03-17
> > NARFj5 was last touched on 2014-06-01
> > Fixed-width at the moment, as it happens, but certainly
> >
> > I wanted to sort by the suffix, and then by the line.
> > I tried something along the lines of
> > sort {
> > ($::a ~= /(..) /) cmp ($::b ~= /(..) /) ||
> > $::a cmp $::b
> > }
> > ...
>
> $re = qr/(..)/;
> sort { ($a=~/$re/,$1)[0] cmp ($b=~/$re/,$1)[0]
> or $a cmp $b }
>
Rather:
sort { ($a=~/$re/)[0] cmp ($b=~/$re/)[0]
or $a cmp $b }
--
Charles DeRykus
------------------------------
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 4335
***************************************