[32297] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3564 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Dec 11 16:09:22 2011

Date: Sun, 11 Dec 2011 13: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, 11 Dec 2011     Volume: 11 Number: 3564

Today's topics:
    Re: iterating through a hash map <ken@swiss-soccer.net>
    Re: iterating through a hash map <willem@toad.stack.nl>
    Re: iterating through a hash map <ben@morrow.me.uk>
    Re: iterating through a hash map <ben@morrow.me.uk>
    Re: iterating through a hash map <ken@swiss-soccer.net>
    Re: Problem configuring OS/2 for installing CPAN packag <ben@morrow.me.uk>
    Re: Problem configuring OS/2 for installing CPAN packag <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 9 Dec 2011 14:51:01 -0500
From: Ken Butler <ken@swiss-soccer.net>
Subject: Re: iterating through a hash map
Message-Id: <alpine.DEB.2.00.1112091445220.24181@ken-laptop>

On Wed, 7 Dec 2011, Ben Morrow wrote:

>
> Quoth Ken Butler <ken@swiss-soccer.net>:

>> My code for the above would say something like
>>
>> for my $album (keys %{$tree->{singers}{album}})
>
> Did you try this? I would expect a 'Not a HASH reference' error, which
> would have given you some idea of what was wrong.

Usually, I manage to follow the maxim of "read twice, post once". Here I 
managed to miss the [0] (after {album}) entirely, which of course makes 
all the difference.

Had I received that error message, I would have stopped, asked myself "why 
on earth isn't it a hash reference, then?", scratched my head a few times, 
and then figured it out.

So:

for my $i (keys %{$x->{$y}}) {...}

will get at $x->{$y}{$i}

and

for my $i (@{$x->{$y}}) {...}

will get at $x->{$y}[$i]

if I have it right now.


Cheers,
Ken.


 .


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

Date: Sat, 10 Dec 2011 15:14:19 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: iterating through a hash map
Message-Id: <slrnje6tqb.kcl.willem@toad.stack.nl>

Ken Butler wrote:
) So:
)
) for my $i (keys %{$x->{$y}}) {...}
)
) will get at $x->{$y}{$i}

Correct.

) and
)
) for my $i (@{$x->{$y}}) {...}
)
) will get at $x->{$y}[$i]

Not quite.
That will make $i be each of the *values* of $x->{$y}[...]

The following are equivalent:

 for my $i (keys %{$x->{$y}})
and
 for my $i (0 .. $#{$x->{$y}})

As are the following:

 for my $v (values ${$x->{$y}})
and
 for my $v (@{$x->{$y}})


Can you see why ?


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: Sat, 10 Dec 2011 15:25:49 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: iterating through a hash map
Message-Id: <t8acr8-ejo1.ln1@anubis.morrow.me.uk>


Quoth Willem <willem@toad.stack.nl>:
> 
> The following are equivalent:
> 
>  for my $i (keys %{$x->{$y}})
> and
>  for my $i (0 .. $#{$x->{$y}})
> 
> As are the following:
> 
>  for my $v (values ${$x->{$y}})
> and
>  for my $v (@{$x->{$y}})

As of 5.12, you can also apply 'keys' and 'values' (and 'each') to an
array. (Obviously the 'values' case isn't terribly useful, though it
does force list context on the array.)

Ben



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

Date: Sat, 10 Dec 2011 15:22:15 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: iterating through a hash map
Message-Id: <72acr8-ejo1.ln1@anubis.morrow.me.uk>


Quoth Ken Butler <ken@swiss-soccer.net>:
> 
> So:
> 
> for my $i (keys %{$x->{$y}}) {...}
> 
> will get at $x->{$y}{$i}
> 
> and
> 
> for my $i (@{$x->{$y}}) {...}
> 
> will get at $x->{$y}[$i]
> 
> if I have it right now.

Yes, that's right.

More specifically, keys %{$x->{$y}} will return a list of keys, so if
you want the values you will need to look them up with $x->{$y}{$i}.
@{$x->{$y}} returns a list of values directly (rather than a list of
indices) so you don't need to look anything up.

Ben



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

Date: Sat, 10 Dec 2011 15:05:56 -0500
From: Ken Butler <ken@swiss-soccer.net>
Subject: Re: iterating through a hash map
Message-Id: <alpine.DEB.2.00.1112101501310.22082@ken-laptop>



On Sat, 10 Dec 2011, Willem wrote:

> Ken Butler wrote:
> ) So:
> )
> ) for my $i (keys %{$x->{$y}}) {...}
> )
> ) will get at $x->{$y}{$i}
>
> Correct.
>
> ) and
> )
> ) for my $i (@{$x->{$y}}) {...}
> )
> ) will get at $x->{$y}[$i]
>
> Not quite.
> That will make $i be each of the *values* of $x->{$y}[...]

Ah, in the same way that

for my $i (@a) {...}

gives you the *values* of @a,

and

for my $i (0..$#a) {...}

gives you the *indices* of @a, so that $a[$i] is how you get at the value.

> The following are equivalent:
>
> for my $i (keys %{$x->{$y}})
> and
> for my $i (0 .. $#{$x->{$y}})
>
> As are the following:
>
> for my $v (values ${$x->{$y}})
> and
> for my $v (@{$x->{$y}})
>
>
> Can you see why ?

Yep, I reckon I understand it now. In your two pairs of examples, $i takes 
the values of the keys of the hash and the indices of the array, and $v 
takes the values of the hash (rather transparently) and the values of the 
array.

Cheers,
Ken.



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

Date: Sat, 10 Dec 2011 00:44:28 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Problem configuring OS/2 for installing CPAN packages
Message-Id: <ckmar8-b791.ln1@anubis.morrow.me.uk>


Quoth Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>:
> In <o40ar8-v431.ln1@anubis.morrow.me.uk>, on 12/09/2011
>    at 06:20 PM, Ben Morrow <ben@morrow.me.uk> said:
> 
> >Are you sure you haven't edited the Makefile.PL with an editor 
> >that has changed tabs into spaces?
> 
> I don't recall editing it, and the date is the same as the date on the
> directory. I can try replacing it from the archive and see whether it
> is different.

Hmm. Well, I definitely get tabs on my system with 5.10.0 and EUMM 6.42,
and I can't see any reason to get different results on OS/2. Can you
verify

    - sub postamble in Net-DNS' Makefile.PL does in fact have tabs in
      the right places?

    - sub makemakerdflt_target in your copy of ExtUtils::MM_Any
      similarly has tabs in the right places? You can find the location
      of the module with

        perldoc -l ExtUtils::MM_Any

      though that may give you a Unix path you need to translate by
      hand.

> >Does the generated Makefile contain both these lines, somewhere,
> >    NOECHO = @
> >    NOOP = $(SHELL) -c true
> 
> Yes.
> 
> >and is SHELL set to something sensible
> 
> SHELL = /bin/sh.exe
> 
> That doesn't exist per se, but kLIBC Pathsrewriters has /bin as U:\bin

(And, presumably, there is a U:\bin\sh.exe?)

Does 

    U:\bin\sh.exe -c true

successfully do nothing? If you create an empty directory with a file
called Makefile containing only

    NOECHO = @
    SHELL = /bin/sh.exe
    NOOP = $(SHELL) -c true

    all:
    	$(NOECHO) $(NOOP)

(with a tab in the appropriate place) and run 'make' in that directory,
does it successfully do nothing?

Ben



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

Date: Sun, 11 Dec 2011 04:42:34 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Problem configuring OS/2 for installing CPAN packages
Message-Id: <quodr8-4u2.ln1@anubis.morrow.me.uk>


Quoth Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>:
> In <ckmar8-b791.ln1@anubis.morrow.me.uk>, on 12/10/2011
>    at 12:44 AM, Ben Morrow <ben@morrow.me.uk> said:
> 
> >Hmm. 
> 
> It turns out that I did edit MAKEFILE.PM, for reasons that I can't
> remember. I added the equivalent of a shebang, and my customary editor
> loses trailing spaces. Once I extracted the original from the tarball
> I got further. I now get warnings about unresolved dependencies[1] for
> Digest::HMAC_MD5 1 and Net::IP 1.2. If I continue with the make and
> make test and get some compilation errors, although a surprising
> number of the tests completed normally.

Good. Am I correct that everything is now working properly?

> >perldoc -l ExtUtils::MM_Any
> 
>  [H:\vendors\CPAN\NetDNS]Q:\PROGRAMS\PERL\bin\perl
> Q:\PROGRAMS\PERL\lib\5.10.0\Pod\perldoc -l ExtUtils::MM_Any
>  Can't find string terminator '"' anywhere before EOF at
> Q:\PROGRAMS\PERL\lib\5.10.0\Pod\perldoc line 1.

Um, I believe Q:\programs\perl\lib\5.10.0\Pod\Perldoc is a directory?
You want Q:\programs\perl\bin\perldoc. 

I don't know if it's directly executable: I suspect not, since the
shebang-equivalent looks wrong for OS/2 to me.

Ben



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

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


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