[31940] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3203 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Nov 9 21:09:27 2010

Date: Tue, 9 Nov 2010 18:09:09 -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           Tue, 9 Nov 2010     Volume: 11 Number: 3203

Today's topics:
    Re: FAQ 4.71 How can I check if a key exists in a multi <marc.girod@gmail.com>
    Re: FAQ 4.71 How can I check if a key exists in a multi <peter@makholm.net>
    Re: FAQ 4.71 How can I check if a key exists in a multi sln@netherlands.com
    Re: Foreach <no@email.here.invalid>
    Re: Foreach <willem@turtle.stack.nl>
    Re: Foreach <dirk@plfn.invalid>
    Re: Foreach <no@email.here.invalid>
    Re: Foreach <uri@StemSystems.com>
    Re: Foreach <uri@StemSystems.com>
    Re: Foreach <willem@turtle.stack.nl>
    Re: Foreach <m@rtij.nl.invlalid>
    Re: humor <m@rtij.nl.invlalid>
        Replacing urls with anchor tags <laredotornado@zipmail.com>
        testing whether a number is an integer <cartercc@gmail.com>
    Re: testing whether a number is an integer <uri@StemSystems.com>
    Re: testing whether a number is an integer <kst-u@mib.org>
    Re: Using Perl to find what address bar says <sherm.pendley@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 9 Nov 2010 04:24:59 -0800 (PST)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: FAQ 4.71 How can I check if a key exists in a multilevel hash?
Message-Id: <a77ff35e-86ca-4830-9e10-90c27a512fc3@f20g2000yqi.googlegroups.com>

On Nov 9, 11:00=A0am, PerlFAQ Server <br...@theperlreview.com> wrote:

> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0foreach my $key ( @keys ) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return unless eval { exist=
s $hash->{$key} };
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$hash =3D $hash->{$key};
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}

Naive question: what is the role of the eval block above?

Thanks,
Marc


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

Date: Tue, 09 Nov 2010 14:34:04 +0100
From: Peter Makholm <peter@makholm.net>
Subject: Re: FAQ 4.71 How can I check if a key exists in a multilevel hash?
Message-Id: <8739rar583.fsf@vps1.hacking.dk>

Marc Girod <marc.girod@gmail.com> writes:

> On Nov 9, 11:00 am, PerlFAQ Server <br...@theperlreview.com> wrote:
>
>>                foreach my $key ( @keys ) {
>>                        return unless eval { exists $hash->{$key} };
>>                        $hash = $hash->{$key};
>>                        }
>
> Naive question: what is the role of the eval block above?

If $hash isn't a hash reference or undef 'exists $hash->{key}' would
die. The eval block makes the function safer to use if one of the
intermediate keys might point at a scalar value or a arrayref (or any
other kind of reference than a hashref).

//Makholm 


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

Date: Tue, 09 Nov 2010 06:18:09 -0800
From: sln@netherlands.com
Subject: Re: FAQ 4.71 How can I check if a key exists in a multilevel hash?
Message-Id: <4slid6pu0jpbl2v2s6c9pionf3cr35l0n0@4ax.com>

On Tue, 9 Nov 2010 04:24:59 -0800 (PST), Marc Girod <marc.girod@gmail.com> wrote:

>On Nov 9, 11:00 am, PerlFAQ Server <br...@theperlreview.com> wrote:
>
>>                foreach my $key ( @keys ) {
>>                        return unless eval { exists $hash->{$key} };
>>                        $hash = $hash->{$key};
>>                        }
>
>Naive question: what is the role of the eval block above?
>
>Thanks,
>Marc

Presumeably its to block printing a message like this:

Can't use string ("I am not a hash") as a HASH ref while "strict refs" in use at
 ...

-sln


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

Date: Tue, 9 Nov 2010 17:03:59 +0000 (UTC)
From: Mladen Gogala <no@email.here.invalid>
Subject: Re: Foreach
Message-Id: <pan.2010.11.09.17.03.59@email.here.invalid>

On Wed, 03 Nov 2010 10:34:33 -0700, Keith Thompson wrote:


> foreach my $arg (@ARGV) {
>     print "$arg\n";
> }

Of course, there is a "map" equivalent, too:

map { print "$_\n"; } @ARGV;

I find that to be the most elegant way of looping through arrays. Of 
course, if you need the global $_ variable, for instance for reading a 
file, this trick is not applicable. 





-- 
http://mgogala.byethost5.com


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

Date: Tue, 9 Nov 2010 17:22:04 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: Foreach
Message-Id: <slrnidj0ps.16i.willem@turtle.stack.nl>

Mladen Gogala wrote:
) On Wed, 03 Nov 2010 10:34:33 -0700, Keith Thompson wrote:
)
)
)> foreach my $arg (@ARGV) {
)>     print "$arg\n";
)> }
)
) Of course, there is a "map" equivalent, too:
)
) map { print "$_\n"; } @ARGV;

First: Looping through an array and doing stuff is for 'for' loops.
Map is for when you want the resulting values.

Example:
print map { "$_\n" } @ARGV;

Second: You can do the short version of for too:

Example:
print "$_\n" for @ARGV;

) I find that to be the most elegant way of looping through arrays.

If you want other people to read your code, then

for my $arg (@ARGV) { print "$arg\n"; }

is more readable (because it's like other languages) than

print "$_\n" for @ARGV;

) Of 
) course, if you need the global $_ variable, for instance for reading a 
) file, this trick is not applicable. 

Wrong.  The $_ is aliased to the elements and after the loop you get the
original value back.

So, in closing, I totally disagree with your whole post.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


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

Date: Tue, 9 Nov 2010 17:43:32 +0000 (UTC)
From: Deadly Dirk <dirk@plfn.invalid>
Subject: Re: Foreach
Message-Id: <pan.2010.11.09.17.43.32@plfn.invalid>

On Tue, 09 Nov 2010 17:22:04 +0000, Willem wrote:


> Wrong.  The $_ is aliased to the elements and after the loop you get the
> original value back.

I know that. The problem is that you don't have it within the loop.

> 
> So, in closing, I totally disagree with your whole post.

My post was, basically, describing my taste. It doesn't make much sense 
to discuss somebody's taste. So, in closing, you can agree with me or you 
can have a bad taste.

PS:
Before you explode, the last sentence was a joke.




-- 
The missionaries go forth to Christianize the savages - 
as if the savages weren't dangerous enough already.


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

Date: Tue, 9 Nov 2010 17:44:13 +0000 (UTC)
From: Mladen Gogala <no@email.here.invalid>
Subject: Re: Foreach
Message-Id: <pan.2010.11.09.17.44.11@email.here.invalid>

On Tue, 09 Nov 2010 17:43:32 +0000, Deadly Dirk wrote:

> On Tue, 09 Nov 2010 17:22:04 +0000, Willem wrote:
> 
> 
>> Wrong.  The $_ is aliased to the elements and after the loop you get
>> the original value back.
> 
> I know that. The problem is that you don't have it within the loop.
> 
> 
>> So, in closing, I totally disagree with your whole post.
> 
> My post was, basically, describing my taste. It doesn't make much sense
> to discuss somebody's taste. So, in closing, you can agree with me or
> you can have a bad taste.
> 
> PS:
> Before you explode, the last sentence was a joke.

Posted from the wrong ID. I apologize.



-- 
http://mgogala.byethost5.com


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

Date: Tue, 09 Nov 2010 13:24:09 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Foreach
Message-Id: <87zktiicdy.fsf@quad.sysarch.com>

>>>>> "MG" == Mladen Gogala <no@email.here.invalid> writes:

  MG> On Wed, 03 Nov 2010 10:34:33 -0700, Keith Thompson wrote:
  >> foreach my $arg (@ARGV) {
  >> print "$arg\n";
  >> }

  MG> Of course, there is a "map" equivalent, too:

  MG> map { print "$_\n"; } @ARGV;

  MG> I find that to be the most elegant way of looping through arrays. Of 
  MG> course, if you need the global $_ variable, for instance for reading a 
  MG> file, this trick is not applicable. 

and that is considered to be very poor perl code. it is using map in a
void context. in older perls it would actually build up a list and throw
it away wasting ram and cpu. newer perls don't do that but it it still
poor style. map is for making a NEW LIST from an existing list. so it
should have output that is used. ignoring the output will confuse the
reader. and perl HAS a similar thing that is what you want: the foreach
modifier.

	print "$_\n" foreach @ARGV ;

don't use map in a void context!

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 09 Nov 2010 13:25:42 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Foreach
Message-Id: <87vd46icbd.fsf@quad.sysarch.com>

>>>>> "DD" == Deadly Dirk <dirk@plfn.invalid> writes:

  DD> On Tue, 09 Nov 2010 17:22:04 +0000, Willem wrote:
  >> Wrong.  The $_ is aliased to the elements and after the loop you get the
  >> original value back.

  DD> I know that. The problem is that you don't have it within the loop.

  >> 
  >> So, in closing, I totally disagree with your whole post.

  DD> My post was, basically, describing my taste. It doesn't make much sense 
  DD> to discuss somebody's taste. So, in closing, you can agree with me or you 
  DD> can have a bad taste.

you can not only disagree with coding style, some styles are objectively
(yes, not subjectively) better than others. you need to learn this.

  DD> PS:
  DD> Before you explode, the last sentence was a joke.

and my comment wasn't a joke. i have read too much poor perl to be funny
about this.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 9 Nov 2010 19:15:42 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: Foreach
Message-Id: <slrnidj7eu.2mb.willem@turtle.stack.nl>

Deadly Dirk wrote:
) On Tue, 09 Nov 2010 17:22:04 +0000, Willem wrote:
)> Wrong.  The $_ is aliased to the elements and after the loop you get the
)> original value back.
)
) I know that. The problem is that you don't have it within the loop.

You were talking about using map for a loop.  If you use map for long
loops where you need access to the global $_ then you really are a joke.

)> So, in closing, I totally disagree with your whole post.
)
) My post was, basically, describing my taste. It doesn't make much sense 
) to discuss somebody's taste.

My post, on the other hand, was giving objective arguments as to why your
"taste" is bad, therefore it makes perfect sense.  Your dismissal is
therefore off the mark and pointless.

) So, in closing, you can agree with me or you can have a bad taste.
)
) PS:
) Before you explode, the last sentence was a joke.

As was the rest of your post.

This sentence is no joke:
Your taste is not bad, your taste is objectively *wrong*.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


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

Date: Wed, 10 Nov 2010 00:33:54 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Foreach
Message-Id: <2c2pq7-6ii.ln1@news.rtij.nl>

On Tue, 09 Nov 2010 13:24:09 -0500, Uri Guttman wrote:

> 	print "$_\n" foreach @ARGV ;

Or (arguably worse)

{ $,="\n"; print @ARGV; }

> don't use map in a void context!

Aye!

M4


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

Date: Wed, 10 Nov 2010 00:20:39 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: humor
Message-Id: <7j1pq7-6ii.ln1@news.rtij.nl>

On Mon, 08 Nov 2010 20:14:43 -0700, John Smith wrote:

> I don't know where I ran into this.  Hopefully it wasn't here so that
> this elicits the chuckle as opposed to the eye-roll.
> 
> Thought this was funny:
> 
> http://imgs.xkcd.com/comics/lisp.jpg

That is the second xkcd that made me really lol. (first one was "bobby 
tables").

Appreciated.

M4


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

Date: Tue, 9 Nov 2010 10:10:15 -0800 (PST)
From: laredotornado <laredotornado@zipmail.com>
Subject: Replacing urls with anchor tags
Message-Id: <f418c705-c3c0-451e-a6e6-447c60c2b7ae@x4g2000pre.googlegroups.com>

Hi,

I'm using Perl 5.8.9 on Mac 10.6.3.  Does anyone have a handy regular
expression for replacing URLs with an anchor tag reference?  I
stumbled across a couple of suggestions online ...

http://www.webmasterworld.com/forum13/4227.htm

but the second one falsely replaced

sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:
726)

with

sun.net.<a href="http://
www.protocol.http.HttpURLConnection.conne">www.protocol.http.HttpURLConnection.conne</a>ct(HttpURLConnection.java:726)


Thanks, - Dave


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

Date: Tue, 9 Nov 2010 10:42:52 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: testing whether a number is an integer
Message-Id: <fc51bb40-b44c-4842-8cd2-cd3300474a8e@i32g2000pri.googlegroups.com>

This is probably a Dumb Question, but I'll ask it anyway.

I have a number, which I use to validate an array, like this:
$number = scalar($@array) / 3;
If $number is an integer, the array is perfect and can be processed.
If it isn't, it's malformed and must be written to an error log.

Some languages have a predicate like is(integer()) which tests the
obvious. Does Perl have a built-in is_integer() equivalent, or will
have have to install something like Test::Numeric?

Thanks, CC.


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

Date: Tue, 09 Nov 2010 13:56:45 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: testing whether a number is an integer
Message-Id: <87tyjqgwb6.fsf@quad.sysarch.com>

>>>>> "c" == ccc31807  <cartercc@gmail.com> writes:

  c> This is probably a Dumb Question, but I'll ask it anyway.
  c> I have a number, which I use to validate an array, like this:
  c> $number = scalar($@array) / 3;

that isn't legal code. $@array makes no sense. i will assume you meant
just @array

  c> If $number is an integer, the array is perfect and can be processed.
  c> If it isn't, it's malformed and must be written to an error log.

  c> Some languages have a predicate like is(integer()) which tests the
  c> obvious. Does Perl have a built-in is_integer() equivalent, or will
  c> have have to install something like Test::Numeric?

simple math will do it for you. the int func will truncate something to
an integer. so it $foo/3 is an int, its int() value will be the same.

my $int = @array/3 # no need for scalar() as / provides scalar context

if ( $int == int( $int ) ) { ...

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 09 Nov 2010 12:41:44 -0800
From: Keith Thompson <kst-u@mib.org>
Subject: Re: testing whether a number is an integer
Message-Id: <lnd3qe8c1j.fsf@nuthaus.mib.org>

"Uri Guttman" <uri@StemSystems.com> writes:
>>>>>> "c" == ccc31807  <cartercc@gmail.com> writes:
>
>   c> This is probably a Dumb Question, but I'll ask it anyway.
>   c> I have a number, which I use to validate an array, like this:
>   c> $number = scalar($@array) / 3;
>
> that isn't legal code. $@array makes no sense. i will assume you meant
> just @array
>
>   c> If $number is an integer, the array is perfect and can be processed.
>   c> If it isn't, it's malformed and must be written to an error log.
>
>   c> Some languages have a predicate like is(integer()) which tests the
>   c> obvious. Does Perl have a built-in is_integer() equivalent, or will
>   c> have have to install something like Test::Numeric?
>
> simple math will do it for you. the int func will truncate something to
> an integer. so it $foo/3 is an int, its int() value will be the same.
>
> my $int = @array/3 # no need for scalar() as / provides scalar context
>
> if ( $int == int( $int ) ) { ...

I'd say the condition you're really testing is whether the number of
elements in @array is a multiple of 3.  So I might write something
like this:

    die "Malformed array\n" if scalar @array % 3 != 0;
    $number = scalar @array / 3;

I know you want better error handling that die(); this is just
an example.

And yes, I know the "scalar" operator is not strictly necessary.
IMHO it makes the code more readable.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


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

Date: Tue, 09 Nov 2010 08:45:21 -0500
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: Using Perl to find what address bar says
Message-Id: <m2vd468vbi.fsf@sherm.shermpendley.com>

jwcarlton <jwcarlton@gmail.com> writes:

>> One
>> has to wonder, since the user obviously wishes to remain anonymous,
>> why not simply let him do so?
>
> Can you think of a non-malicious reason why someone would want to hide
> their IP address?

Many, but first and foremost - because I can. It's private information,
and it's your responsibility to justify your need to have it, not mine
to justify my desire for privacy.

sherm--

-- 
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer


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

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


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