[29432] in Perl-Users-Digest
Perl-Users Digest, Issue: 676 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jul 24 00:14:16 2007
Date: Mon, 23 Jul 2007 21:14:09 -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, 23 Jul 2007 Volume: 11 Number: 676
Today's topics:
Re: FAQ 4.45 How do I find the first array element for <m@rtij.nl.invlalid>
Re: FAQ 4.45 How do I find the first array element for anno4000@radom.zrz.tu-berlin.de
Re: Math <blb8@po.cwru.edu>
Re: regex question <dummy@example.com>
Re: regex question <stoupa@practisoft.cz>
Re: regex question <tadmc@seesig.invalid>
Re: regex question <noreply@gunnar.cc>
Re: removing . from a string <tadmc@seesig.invalid>
Re: The Modernization of Emacs: exists already <m@rtij.nl.invlalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 23 Jul 2007 22:42:12 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: FAQ 4.45 How do I find the first array element for which a condition is true?
Message-Id: <pan.2007.07.23.20.42.43@rtij.nl.invlalid>
On Sun, 22 Jul 2007 00:30:23 +0000, anno4000 wrote:
> brian d foy <brian.d.foy@gmail.com> wrote in comp.lang.perl.misc:
>> In article <1184941331.498545.290230@57g2000hsv.googlegroups.com>,
>> MrL22 <mr.leishman@gmail.com> wrote:
>>
>> > # Do this using GREP
>> >
>> > my @people = ('Jacob Smith', 'Michael Brown', 'Joshua Smith',
>> > 'Matthew Cope');
>> > @smiths = grep(/smith/i, @people);
>>
>> The trick is to do it without doing extra work. Imagine a list with
>> thousands of elements. Do you really want to check every element, even
>> after you've found the first element?
>>
>> That's why List::Util has first(). :)
>
> Unfortunately List::Util::first is slightly broken. I returns an
> undefined value when there is no first element, either because the
> condition never matched or because the list to search is empty in the
> first place. It should return empty in this case. As it is, if the
I think undef is perfectly acceptable in both cases. Besides, what kind
of scalar is "empty"?
> condition can match an undefined value there is no way to tell if it did
> that or if it never matched.
That's more of a restriction due to the pretty syntax. How would you
distinguish between these cases?
The only ways I can think of are to use exceptions or a very different
syntax. Both are not nearly as pretty as the current syntax.
Just my E0,02
M4
------------------------------
Date: 23 Jul 2007 22:22:23 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: FAQ 4.45 How do I find the first array element for which a condition is true?
Message-Id: <5gko0vF3fddf3U1@mid.dfncis.de>
Martijn Lievaart <m@rtij.nl.invlalid> wrote in comp.lang.perl.misc:
> On Sun, 22 Jul 2007 00:30:23 +0000, anno4000 wrote:
>
> > brian d foy <brian.d.foy@gmail.com> wrote in comp.lang.perl.misc:
> >> In article <1184941331.498545.290230@57g2000hsv.googlegroups.com>,
> >> MrL22 <mr.leishman@gmail.com> wrote:
> >>
> >> > # Do this using GREP
> >> >
> >> > my @people = ('Jacob Smith', 'Michael Brown', 'Joshua Smith',
> >> > 'Matthew Cope');
> >> > @smiths = grep(/smith/i, @people);
> >>
> >> The trick is to do it without doing extra work. Imagine a list with
> >> thousands of elements. Do you really want to check every element, even
> >> after you've found the first element?
> >>
> >> That's why List::Util has first(). :)
> >
> > Unfortunately List::Util::first is slightly broken. I returns an
> > undefined value when there is no first element, either because the
> > condition never matched or because the list to search is empty in the
> > first place. It should return empty in this case. As it is, if the
>
> I think undef is perfectly acceptable in both cases. Besides, what kind
> of scalar is "empty"?
No scalar is empty. That's why the difference can only be detected in
list context.
> > condition can match an undefined value there is no way to tell if it did
> > that or if it never matched.
>
> That's more of a restriction due to the pretty syntax. How would you
> distinguish between these cases?
>
> The only ways I can think of are to use exceptions or a very different
> syntax. Both are not nearly as pretty as the current syntax.
No, an empty return (return with no argument) is easily distinguishable
from returning an undefined value. You need list context:
sub ret_undef { return undef }
sub ret_empty { return }
( my ( $x) = ret_undef) or warn "ret_undef returned nothing\n";
( my ( $y) = ret_empty) or warn "ret_empty returned nothing\n";
Both $x and $y end up undefined, but only the second call prints
a warning.
The current code for List::Util::first is (the Perl variant):
sub first (&@) {
my $code = shift;
foreach (@_) {
return $_ if &{$code}();
}
undef; # this is unfortunate
}
It would be more useful if "undef;" was replaced by "return;". The
effect in scalar context would be unchanged, but if you need to know,
you can.
Anno
------------------------------
Date: Mon, 23 Jul 2007 21:39:45 +0000 (UTC)
From: Brian Blackmore <blb8@po.cwru.edu>
Subject: Re: Math
Message-Id: <f8376v$m7$1@gnus01.u.washington.edu>
anno4000@radom.zrz.tu-berlin.de wrote:
> Robert Hicks <sigzero@gmail.com> wrote in comp.lang.perl.misc:
> [...]
> > Basically it has to do with the positions of ships. We get emails from
> [...]
> > We currently have c libs with the math and a Perl/XS module that calls
> > those routines. We are having a horrible time moving that code from PA-
> > RISC to Itanium (the code is as old as IRIS and PRIME). So we may just
> > move it all into the Perl realm to make it easier going forward.
> That sounds like a plan. You can do a lot of trigonometry per received
> mail message, even in Perl, speed won't be a problem. There's
> "Geo::Distance - Calculate Distances and Closest Locations" on CPAN.
> GIS::Distance seems to be the aspiring successor.
I'll also add that I don't expect you to have any difficulty with
precision either, whether or not you're converting numbers to
strings and vice-versa. For instance, at fifteen digit precision,
one finds a change of one-one millionth of a second for epoch time,
leading to a 10^-9th change (degrees) in GHA Aries or Sol Azi.
Then again, taking the length of one degree of longitude at the
equator, fifteen digits of precision gives a direct error of 1nm, or
in other words, 10^-13 percent error. Indeed, rounding errors
propagate, etc., but that won't be as notable a problem than the
simple fact that you can't predict path one billion cycles ahead
anyway. Besides that, positioning information should probably not
be considered accurate to more than a meter anyway simply because of
common safety concerns, etc.
If you need to know the position of your ships to more precision
than one nanometer...
--
Brian Blackmore
blb8 at po dot cwru dot edu
------------------------------
Date: Mon, 23 Jul 2007 21:49:31 GMT
From: "John W. Krahn" <dummy@example.com>
Subject: Re: regex question
Message-Id: <Ln9pi.66435$xk5.23253@edtnps82>
Gunnar Hjalmarsson wrote:
> new2perl@gmail.com wrote:
>> Trying to match a variable such as: $name
>>
>> or
>>
>> a hashvar such as: $name{var1}
>>
>>
>> Tried s/\$(\w+)/g but that only matches $name.
>>
>> Also tried s/\$(\w+{?\w*}? - not working.
>
> What are you trying to achieve? Are you trying to resolve variables in a
> template? Perl variables are not well suited as template variables.
>
> Anyway, to just answer your question, this would match both your examples:
>
> \$\S+
Except where it won't:
$ perl -le'%x = "A" .. "F"; print $x { C }'
D
perl usually ignores whitespace.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: Tue, 24 Jul 2007 02:51:46 +0200
From: "Petr Vileta" <stoupa@practisoft.cz>
Subject: Re: regex question
Message-Id: <f83igq$e2o$2@ns.felk.cvut.cz>
new2perl@gmail.com wrote:
> Trying to match a variable such as: $name
>
> or
>
> a hashvar such as: $name{var1}
>
>
> Tried s/\$(\w+)/g but that only matches $name.
>
> Also tried s/\$(\w+{?\w*}? - not working.
>
> Bob.
If you want it for replacing variables in html templates so you can use
something like
s/(\$[a-z][a-z\{\}]*)/some_value_to_replace/sig;
This mean that the variable must begin with '$' followeb by one alphabet
character and can or cannot be followed by other alphabet characters or '{'
or '}' characters. This is very simple example and of course it replace this
too :-)
<span>$name}var1{</span>
Should be
<span>$name{var1}</span>
--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
------------------------------
Date: Tue, 24 Jul 2007 01:22:48 GMT
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: regex question
Message-Id: <slrnfaakim.iil.tadmc@tadmc30.sbcglobal.net>
new2perl@gmail.com <new2perl@gmail.com> wrote:
> Trying to match a variable such as: $name
Why are you trying to match a variable such as: $name ?
What about
${name}
$main::name
??
> or
>
> a hashvar such as: $name{var1}
^^^^
A hash key can be any Perl expression.
You need to parse nearly the entire Perl language to do that reliably.
A Perl expression can be nested to an arbitrary depth, so it is
mathematically impossible to parse them with a regular expression.
That road leads to madness.
Give up.
> Tried s/\$(\w+)/g but that only matches $name.
>
> Also tried s/\$(\w+{?\w*}? - not working.
This is almost certainly an XY problem.
You should use a templating system if that is what your Y is.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Tue, 24 Jul 2007 04:56:42 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: regex question
Message-Id: <5gl89lF3h6t20U1@mid.individual.net>
John W. Krahn wrote:
> Gunnar Hjalmarsson wrote:
>> new2perl@gmail.com wrote:
>>> Trying to match a variable such as: $name
>>>
>>> or
>>>
>>> a hashvar such as: $name{var1}
>>>
>>>
>>> Tried s/\$(\w+)/g but that only matches $name.
>>>
>>> Also tried s/\$(\w+{?\w*}? - not working.
>>
>> What are you trying to achieve? Are you trying to resolve variables in
>> a template? Perl variables are not well suited as template variables.
>>
>> Anyway, to just answer your question, this would match both your
>> examples:
>>
>> \$\S+
>
> Except where it won't:
>
> $ perl -le'%x = "A" .. "F"; print $x { C }'
> D
>
> perl usually ignores whitespace.
I don't understand your objection. The OP's examples are:
$name
$name{var1}
If you use Perl variables as template variables, which I don't think you
should, you can obviously apply a more restrictive format than what the
Perl interpreter would understand.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Tue, 24 Jul 2007 01:22:46 GMT
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: removing . from a string
Message-Id: <slrnfaah2h.iil.tadmc@tadmc30.sbcglobal.net>
Clenna Lumina <savagebeaste@yahoo.com> wrote:
> Tad McClellan wrote:
>> pkassies@gmail.com <pkassies@gmail.com> wrote:
>>
>> > I need to strip "90.00" back to "90".
>>
>> -------------------------
>> #!/usr/bin/perl
>> use warnings;
>> use strict;
>>
>> my $item = "90.0";
>> $item += 0;
>> print "$item\n";
>> -------------------------
>
> 1) This code doesn't work as expected if the fraction part is
> anything other than .0 (i.e., using "90.4" will end up
> printing 90.4 )
The OP did not ask for code that works if the fraction part is
anything other than .0
The value of an answer is often in direct proportion to the
care taken in framing the question.
Ask a poor question, get a poor answer.
> 2) I thought it was generally frowned upon to quote a variable
> this way, rather than, say, C< print $itme, "\n"; > or so?
No, quoting a *lone* variable is warned about in the FAQ.
I was not quoting a lone variable, and I in fact did want to make
use of one of the two things that double quoting gives you
(backslash escapes and/or interpolation).
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Mon, 23 Jul 2007 22:45:21 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: The Modernization of Emacs: exists already
Message-Id: <pan.2007.07.23.20.45.53@rtij.nl.invlalid>
On Wed, 18 Jul 2007 06:17:00 -0700, Xah Lee wrote:
> About a month ago, i posted a message about modernization of emacs. I
> enlisted several items that i think emacs should adapt.
And you are posting this to compl.lang.perl because.....??????
F'up set.
M4
------------------------------
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:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#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 676
**************************************