[28221] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9585 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 10 00:05:53 2006

Date: Wed, 9 Aug 2006 21:05:06 -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           Wed, 9 Aug 2006     Volume: 10 Number: 9585

Today's topics:
    Re: How to send command line options into test scripts? <mgarrish@gmail.com>
        Is there arithmetic sequence represents? <zhushenli@gmail.com>
    Re: Is there arithmetic sequence represents? <jurgenex@hotmail.com>
    Re: Is there arithmetic sequence represents? <tony_curtis32@_NOSPAM_yahoo.com>
    Re: Is there arithmetic sequence represents? <zhushenli@gmail.com>
    Re: Is there arithmetic sequence represents? <uri@stemsystems.com>
    Re: OO Perl : Struggling with hash data members in my C <Brett.R.Davis@gmail.com>
    Re: OO Perl : Struggling with hash data members in my C <mumia.w.18.spam+nospam.usenet@earthlink.net>
    Re: OO Perl : Struggling with hash data members in my C (Randal L. Schwartz)
    Re: OO Perl : Struggling with hash data members in my C <mumia.w.18.spam+nospam.usenet@earthlink.net>
    Re: OO Perl : Struggling with hash data members in my C <Brett.R.Davis@gmail.com>
    Re: Read socket using both <> and sysread() <uri@stemsystems.com>
    Re: Read socket using both <> and sysread() <nospam-abuse@ilyaz.org>
        Slice of a hash of hashes usenet@DavidFilmer.com
    Re: Slice of a hash of hashes <uri@stemsystems.com>
    Re: Slice of a hash of hashes <mumia.w.18.spam+nospam.usenet@earthlink.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 9 Aug 2006 18:36:34 -0700
From: "Matt Garrish" <mgarrish@gmail.com>
Subject: Re: How to send command line options into test scripts?
Message-Id: <1155173794.765424.303450@q16g2000cwq.googlegroups.com>


yusuf wrote:

> > Why out of curiosity would you post one vague snippet of code and then
> > claim it's not a code question?
>
> I didn't post any code.

Then explain "I tried using the $Test::Harness::switches var".

Switches, if you read the documentation, is for setting the
command-line switches for perl.

>  They were command line argument examples. I
> can't post any code because I don't know how to code the thing I need.
> The thing I need is to:
>
> - From 'prove' which uses Test::Harness, when I call the different test
> files in runtests(). How do I send in command line arguments to those
> test files.

Well, if you've read the documentation you know it's not when you call
prove, because prove just runs your tests. Why is your test data not
self-contained in the test file? Do you know why you're using this
approach and not the debugger? I'm beginning to have my doubts.

Matt



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

Date: 9 Aug 2006 18:21:14 -0700
From: "Davy" <zhushenli@gmail.com>
Subject: Is there arithmetic sequence represents?
Message-Id: <1155172874.162345.106870@p79g2000cwp.googlegroups.com>

Hi all,

I found in Perl (2..8) is equal to (2,3,4,5,6,7,8).

But if I want a arithmetic sequence like (2,4,6,8,10,12), is there any
easy represents?

PS, in Matlab, is [2:2:12].

Thanks!
Davy



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

Date: Thu, 10 Aug 2006 01:36:04 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Is there arithmetic sequence represents?
Message-Id: <84wCg.18007$Qu4.3517@trnddc04>

Davy wrote:
> I found in Perl (2..8) is equal to (2,3,4,5,6,7,8).
>
> But if I want a arithmetic sequence like (2,4,6,8,10,12), is there any
> easy represents?

for my $i (1..6) {
    $i *= 2;
    .....
} 




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

Date: 09 Aug 2006 21:39:33 -0400
From: Tony Curtis <tony_curtis32@_NOSPAM_yahoo.com>
Subject: Re: Is there arithmetic sequence represents?
Message-Id: <y6zszmedxtbe.fsf@post.queensu.ca>

>> On 9 Aug 2006 18:21:14 -0700,
>> "Davy" <zhushenli@gmail.com> said:

> Hi all, I found in Perl (2..8) is equal to
> (2,3,4,5,6,7,8).

> But if I want a arithmetic sequence like
> (2,4,6,8,10,12), is there any easy represents?

> PS, in Matlab, is [2:2:12].

You could find a solution with a map...

    map {$_ * 2} (1 .. 6);

hth
t


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

Date: 9 Aug 2006 19:30:24 -0700
From: "Davy" <zhushenli@gmail.com>
Subject: Re: Is there arithmetic sequence represents?
Message-Id: <1155177024.737336.79730@75g2000cwc.googlegroups.com>


Tony Curtis wrote:
> >> On 9 Aug 2006 18:21:14 -0700,
> >> "Davy" <zhushenli@gmail.com> said:
>
> > Hi all, I found in Perl (2..8) is equal to
> > (2,3,4,5,6,7,8).
>
> > But if I want a arithmetic sequence like
> > (2,4,6,8,10,12), is there any easy represents?
>
> > PS, in Matlab, is [2:2:12].
>
> You could find a solution with a map...
>
>     map {$_ * 2} (1 .. 6);
>
[snip]
Hi,

But it seems not intuitive like [2:2:12]. Anyhow, thanks!

Davy  

> hth
> t



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

Date: Wed, 09 Aug 2006 23:05:04 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Is there arithmetic sequence represents?
Message-Id: <x77j1h5lzz.fsf@mail.sysarch.com>

>>>>> "D" == Davy  <zhushenli@gmail.com> writes:

  D> Tony Curtis wrote:
  >> >> On 9 Aug 2006 18:21:14 -0700,
  >> >> "Davy" <zhushenli@gmail.com> said:
  >> 
  >> > Hi all, I found in Perl (2..8) is equal to
  >> > (2,3,4,5,6,7,8).
  >> 
  >> > But if I want a arithmetic sequence like
  >> > (2,4,6,8,10,12), is there any easy represents?
  >> 
  >> > PS, in Matlab, is [2:2:12].
  >> 
  >> You could find a solution with a map...
  >> 
  >> map {$_ * 2} (1 .. 6);
  >> 
  D> [snip]
  D> Hi,

  D> But it seems not intuitive like [2:2:12]. Anyhow, thanks!

so use matlab or perl6 which has such a beast. perl5 doesn't have the
syntax for it.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: 9 Aug 2006 18:08:44 -0700
From: "Brett.R.Davis@gmail.com" <Brett.R.Davis@gmail.com>
Subject: Re: OO Perl : Struggling with hash data members in my Class
Message-Id: <1155172124.757084.285440@75g2000cwc.googlegroups.com>

Thank you for your response.
I think my simple color abstraction may have confused the issue.
Let me try to rephrase the problem - given a hash :

my %bar = { 'x'=>0xbeef, 'y'=>0xfeed };

and assuming a class Foo with the  constructor below,
how do I put %bar into MY_HASH.

And then how do I get the contents of MY_HASH?

I can't seem to get this to work.

=-=-=-=--=-=-=-=-=-=-=-=-=-

Package My::Class::Foo;

use strict;
use warnings;
use Carp;

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self  = {};
    $self->{MY_HASH}  = {};
    bless($self, $class);
    return $self; 
} 

=-=-=-=-=-=-=-=--=-==-=-=-=-=-



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

Date: Thu, 10 Aug 2006 02:31:17 GMT
From: "Mumia W." <mumia.w.18.spam+nospam.usenet@earthlink.net>
Subject: Re: OO Perl : Struggling with hash data members in my Class
Message-Id: <VTwCg.1856$Sn3.120@newsread3.news.pas.earthlink.net>

On 08/09/2006 05:36 PM, Brett.R.Davis@gmail.com wrote:
> I am writing my first Perl Class and have hit a brick wall.
> 
> What I need to do is have a class method copy a hash into the data 
> member %MY_HASH.
> 
> I have coded a constructor and a method my_hash which is *supposed* to 
> either:
> 
> 1. return the HASH reference or
> 2. write a copy the contents of a hash into the MY_COLORS data member.
> 
> Then I wanted a second data member that uppon being written to with a 
> symbolic name, would use the MY_COLORS hash to provide the numberical 
> code for that COLOR as shown:
> 
> my $composite_colors={'green'=>1, 'blue'=>2, 'red'=>3 };
> my $color_set=Chash->new();

I got your program to work, but I had to assign $color_set to 
$chash before the line below.

> $chash->my_colors($composite_colors);
> $chash->color_code('blue');
> print $chash{'color_code'};
> 
> The output should be:
> 
> 2
> 
> However, I am never able to get the object $chash to take in the 
> $composite_colors reference, and copy it into the data member
> MY_COLORS.  

The code you posted pretty much should work so long as $chash 
is set to something meaningful. You have an annoying warning 
that I'll show you how to get rid of.

> I get comments like "odd number of elements in anonymous
> hash".  If I data dump the contents of the data member MY_COLORS it 
> shows that the $composite_colors hash is being copied in as a key to 
> the has instead of essentially becoming the MY_COLORS hash.
> 
> Any assistance would be greatly appreciated!!!!
> 
> Brett
> 
> 
> Here is the code.
> 
> package Chash;
> 
> sub new {
>     my $proto = shift;
>     my $class = ref($proto) || $proto;
>     my $self  = {};
>     $self->{MY_COLORS}  = {};
>     $self->{COLOR_CODE}  = undef;
>     bless($self, $class);
>     return $self;
> }
> 
> sub my_colors {
>     my $self = shift;
>     if (@_) { $self->{MY_COLORS} = $_[0]  }
>     return  { $self->{MY_COLORS} };

The line above produces an annoying warning; the line uses 
$self->{MY_COLORS}, which contains only *one* value, a scalar 
reference to a hash, as the data for an anonymous hash 
(denoted by { ... }); hashes with an uneven number of elements 
are usually messed up.

I don't know why you're creating an anonymous hash then 
returning it; you already have a much better hash to return in 
$self->{MY_COLORS}, so why not just return it? Perhaps you 
were thinking of the {...} as a block. Anyway, change it:

   return $self->{MY_COLORS};


> }
> 
> 
> sub color_code {
>     my $self = shift;
>     if (@_) {
>         if (ref $self->{MY_COLORS} eq "HASH") {
>             $self->{COLOR_CODE} = $self->{MY_COLORS}->{$_[0]};
>         }
>         else {
>             $self->{COLOR_CODE} = shift;
>         }
>     }
>     return $self->{COLOR_CODE};
> }
> 

A. Sinan Unur showed you how to use some perl modules to do 
this differently, and I think I'll do that too:

package My::Chash2;
use Class::Struct;
struct 'My::Chash2' => [ my_colors => '%' ];

sub color_code {
     my $self = shift;
     $self->my_colors(shift());
}

package main;

my $composite_colors={'green'=>1, 'blue'=>2, 'red'=>3 };
my $chash = My::Chash2->new();
$chash->my_colors($composite_colors);
print $chash->color_code('blue'), "\n";



__HTH__

:-)


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

Date: 09 Aug 2006 19:39:42 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: OO Perl : Struggling with hash data members in my Class
Message-Id: <868xlx8gb5.fsf@blue.stonehenge.com>

>>>>> "Brett" == Brett R Davis <Brett.R.Davis@gmail.com> writes:

Brett> sub new {
Brett>     my $proto = shift;
Brett>     my $class = ref($proto) || $proto;

Please don't do this. See
<http://www.stonehenge.com/merlyn/UnixReview/col52.html> for a long discussion
about constructors, including the last few paragraphs which describe why not
to do that.

print "Just another Perl hacker,"; # the original

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
Posted via a free Usenet account from http://www.teranews.com



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

Date: Thu, 10 Aug 2006 03:27:03 GMT
From: "Mumia W." <mumia.w.18.spam+nospam.usenet@earthlink.net>
Subject: Re: OO Perl : Struggling with hash data members in my Class
Message-Id: <bIxCg.6702$0e5.2363@newsread4.news.pas.earthlink.net>

On 08/09/2006 08:08 PM, Brett.R.Davis@gmail.com wrote:
> Thank you for your response.
> I think my simple color abstraction may have confused the issue.
> Let me try to rephrase the problem - given a hash :
> 
> my %bar = { 'x'=>0xbeef, 'y'=>0xfeed };
> 

You have problems with Perl syntax. Curly braces are used to 
create blocks and anonymous hashes. Use parenthesis to create 
arrays and non-anonymous hashes, e.g.

my %bar = ( 'x'=>0xbeef, 'y'=>0xfeed );


> and assuming a class Foo with the  constructor below,
> how do I put %bar into MY_HASH.
> 
> And then how do I get the contents of MY_HASH?
> 
> I can't seem to get this to work.
> 
> =-=-=-=--=-=-=-=-=-=-=-=-=-
> 
> Package My::Class::Foo;
> 
> use strict;
> use warnings;
> use Carp;
> 
> sub new {
>     my $proto = shift;
>     my $class = ref($proto) || $proto;
>     my $self  = {};
>     $self->{MY_HASH}  = {};
      %{$self->{MY_HASH}} = @_ if (@_);

>     bless($self, $class);
>     return $self; 
> } 
> 
> =-=-=-=-=-=-=-=--=-==-=-=-=-=-
> 

package main;

my $chash = My::Class::Foo->new(%bar)

However, I wouldn't bother writing a new() function for 
something this simple, because it's so much easier to cheat:

use Class::Struct;
struct My::Class::Foo => [ my_colors => '%' ];

my %bar = ( 'x'=>0xbeef, 'y'=>0xfeed );
my $chash = My::Class::Foo->new( my_colors => \%bar);

=-=-=-=-=-=-=-=--=-==-=-=-=-=-
UNTESTED CODE


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

Date: 9 Aug 2006 20:49:16 -0700
From: "Brett.R.Davis@gmail.com" <Brett.R.Davis@gmail.com>
Subject: Re: OO Perl : Struggling with hash data members in my Class
Message-Id: <1155181756.301002.146700@75g2000cwc.googlegroups.com>

You said that you were able to get my original code to work.

Can you tell me specifically what it was - or was it the items you
specified in the message?

The class I presented was just for academic reasons.  The actual class
I need to implement will be quite a bit more complicated.

Does the struct class you showed me allow for member functions?

Thanks

Mumia W. wrote:
> On 08/09/2006 05:36 PM, Brett.R.Davis@gmail.com wrote:
> > I am writing my first Perl Class and have hit a brick wall.
> >
> > What I need to do is have a class method copy a hash into the data
> > member %MY_HASH.
> >
> > I have coded a constructor and a method my_hash which is *supposed* to
> > either:
> >
> > 1. return the HASH reference or
> > 2. write a copy the contents of a hash into the MY_COLORS data member.
> >
> > Then I wanted a second data member that uppon being written to with a
> > symbolic name, would use the MY_COLORS hash to provide the numberical
> > code for that COLOR as shown:
> >
> > my $composite_colors={'green'=>1, 'blue'=>2, 'red'=>3 };
> > my $color_set=Chash->new();
>
> I got your program to work, but I had to assign $color_set to
> $chash before the line below.
>
> > $chash->my_colors($composite_colors);
> > $chash->color_code('blue');
> > print $chash{'color_code'};
> >
> > The output should be:
> >
> > 2
> >
> > However, I am never able to get the object $chash to take in the
> > $composite_colors reference, and copy it into the data member
> > MY_COLORS.
>
> The code you posted pretty much should work so long as $chash
> is set to something meaningful. You have an annoying warning
> that I'll show you how to get rid of.
>
> > I get comments like "odd number of elements in anonymous
> > hash".  If I data dump the contents of the data member MY_COLORS it
> > shows that the $composite_colors hash is being copied in as a key to
> > the has instead of essentially becoming the MY_COLORS hash.
> >
> > Any assistance would be greatly appreciated!!!!
> >
> > Brett
> >
> >
> > Here is the code.
> >
> > package Chash;
> >
> > sub new {
> >     my $proto = shift;
> >     my $class = ref($proto) || $proto;
> >     my $self  = {};
> >     $self->{MY_COLORS}  = {};
> >     $self->{COLOR_CODE}  = undef;
> >     bless($self, $class);
> >     return $self;
> > }
> >
> > sub my_colors {
> >     my $self = shift;
> >     if (@_) { $self->{MY_COLORS} = $_[0]  }
> >     return  { $self->{MY_COLORS} };
>
> The line above produces an annoying warning; the line uses
> $self->{MY_COLORS}, which contains only *one* value, a scalar
> reference to a hash, as the data for an anonymous hash
> (denoted by { ... }); hashes with an uneven number of elements
> are usually messed up.
>
> I don't know why you're creating an anonymous hash then
> returning it; you already have a much better hash to return in
> $self->{MY_COLORS}, so why not just return it? Perhaps you
> were thinking of the {...} as a block. Anyway, change it:
>
>    return $self->{MY_COLORS};
>
>
> > }
> >
> >
> > sub color_code {
> >     my $self = shift;
> >     if (@_) {
> >         if (ref $self->{MY_COLORS} eq "HASH") {
> >             $self->{COLOR_CODE} = $self->{MY_COLORS}->{$_[0]};
> >         }
> >         else {
> >             $self->{COLOR_CODE} = shift;
> >         }
> >     }
> >     return $self->{COLOR_CODE};
> > }
> >
>
> A. Sinan Unur showed you how to use some perl modules to do
> this differently, and I think I'll do that too:
>
> package My::Chash2;
> use Class::Struct;
> struct 'My::Chash2' => [ my_colors => '%' ];
>
> sub color_code {
>      my $self = shift;
>      $self->my_colors(shift());
> }
>
> package main;
>
> my $composite_colors={'green'=>1, 'blue'=>2, 'red'=>3 };
> my $chash = My::Chash2->new();
> $chash->my_colors($composite_colors);
> print $chash->color_code('blue'), "\n";
> 
> 
> 
> __HTH__
> 
> :-)



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

Date: Wed, 09 Aug 2006 22:47:00 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Read socket using both <> and sysread()
Message-Id: <x7irl15mu3.fsf@mail.sysarch.com>

>>>>> "x" == xhoster  <xhoster@gmail.com> writes:

  x> Uri Guttman <uri@stemsystems.com> wrote:

  x> Why not just use read() instead of sysread()?
  >> 
  >> that works but since the api is the same, all you get is more buffering
  >> with read with no benefits. sockets already have buffering so there is
  >> no need for more in the user space.

  x> I'm not sure I understand this.  Is this special to sockets?  What form of
  x> IO *doesn't* already have some buffering at the system level?

but why have system buffering in the socket AND also user process
buffering via stdio (or perlio)? that just means extra copying of all
the data.

  x> Anyway, additional user-space buffering does seem like it makes things
  x> faster for sockets (as well as for regular files) with small reads, but I
  x> don't know if the difference is meaningful for real-world cases.

  >> time perl -le 'use IO::Socket;
  x>       my $x=IO::Socket::INET->new("localhost:9871") or die $@;
  x>       $y+=read $x,$buf,10 foreach 1..10_000_000;
  x>       print $y'
  x> 100000000
  x> 3.694u 0.138s 0:03.84 99.4%     0+0k 0+0io 0pf+0w

  >> time perl -le 'use IO::Socket;
  x>       my $x=IO::Socket::INET->new("localhost:9871") or die $@;
  x>       $y+=sysread $x,$buf,10 foreach 1..10_000_000;
  x>       print $y'
  x> 100000000
  x> 4.097u 5.682s 0:09.80 99.6%     0+0k 0+0io 0pf+0w

10 bytes is not a typical size read on sockets for the kind of
applications i do or see. sure local buffering will help there as it
bypasses the overhead of sysread calls. but with a larger read size
that shouldn't matter as much.

  >> it is just not my style so i would never do that.

  x> So then maybe you are the wrong person to ask, but why does Perl's read
  x> exist?

not sure in a way. it really is the opposite of print in most ways (like
sysread/syswrite are opposites) so it is there to provide a sized read
on the stdio (or perlio to appease ilya. as i said, from this point of
view they are the same - just user space buffered i/o).

  x> Was it intended to be just like sysread only doesn't intefere with <>?

  x> Or was it intended to be just like sysread only faster because it uses
  x> an extra layer of buffering?

all are reasonable answers but as i said, i wouldn't expect as much (and
you got very little! :) speedup with larger read sizes.

  x> Is the fact that read will block until the requested size (or eof)
  x> is read (as opposed to sysread, which blocks until at least 1 byte
  x> is read, but after that will return a partial buffer if need be
  x> rather than blocking) a real feature or a malfeature or a bug?

read will restart if it was interrupted by a signal and sysread will
return a partial read or some EINTR error instead.

sysread will block on its read size unless you set the socket to
non-blocking mode. this is defined in the unix read call. you should
never use perl's read on an non-blocking socket since typical buffered
i/o (like stdio or perlio) expect a blocking socket. another reason i
use sysread, is that stem uses non-blocking sockets.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Thu, 10 Aug 2006 03:02:01 +0000 (UTC)
From:  Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Read socket using both <> and sysread()
Message-Id: <ebe7j9$tnh$1@agate.berkeley.edu>

[A complimentary Cc of this posting was sent to
Uri Guttman 
<uri@stemsystems.com>], who wrote in article <x7wt9h7hor.fsf@mail.sysarch.com>:
>   IZ> b) Perl core knows perfectly well when an unbuffered operation follows
>   IZ>    a buffered one (same as for read/write mixes).
> 
>   IZ> So it is a bug in PerlIO that the necessary seek()s needed on such a
>   IZ> boundary are not performed.

> hmm, you should know better.

I do.

> this is doing reads on a SOCKET. seek doesn't apply there

seek() applies to any buffered operation.

> so perl would have to trap any sysread calls

It should not "trap" anything.  It is Perl who initiates them, not
some third party.

> and check if there is anything in the buffer and return that

Right.

> and possibly even do more kernel reads to satisfy the read request size.

No need to.  Partial read is OK.

> sounds like something only you would like.

Right; so it is because of me these questions appear again and again...

> i will stick to pure sysread and do my
> own buffering. been doing that for 25 years so it is second nature.

Sure; feel free to do your own buffering - on top of Perl's one,
(possibly) on top of CRTL one, on top of OS' one.

Yours,
Ilya




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

Date: 9 Aug 2006 19:23:37 -0700
From: usenet@DavidFilmer.com
Subject: Slice of a hash of hashes
Message-Id: <1155176617.768270.259610@i42g2000cwa.googlegroups.com>

Kindly consider the following code which illustrates my question:

#!/usr/bin/perl
   use strict;   use warnings;
   use Data::Dumper;

   my %hash;

   $hash{1}{'foo'} = 'one';
   $hash{2}{'foo'} = 'two';
   $hash{3}{'foo'} = 'three';

   my @foos;
   push @foos, $hash{$_}{'foo'} for keys %hash;

   print Dumper \@foos;

__END__

As you can see, I have created an array containing the values of the
various "foo" keys within the hash.  But that seems a bit kludgy.  It
seems I ought to be able to do something similar to this (with
slicing):

   my @foos = @hash{keys %hash}{'foo'};

But I can't seem to figure out how to implement this approach.

I would appreciate some not-so-kludgy pointers.  Thanks!

-- 
David Filmer (http://DavidFilmer.com)



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

Date: Wed, 09 Aug 2006 23:04:22 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Slice of a hash of hashes
Message-Id: <x7bqqt5m15.fsf@mail.sysarch.com>

>>>>> "u" == usenet  <usenet@DavidFilmer.com> writes:

  u>    $hash{1}{'foo'} = 'one';
  u>    $hash{2}{'foo'} = 'two';
  u>    $hash{3}{'foo'} = 'three';

  u>    push @foos, $hash{$_}{'foo'} for keys %hash;

  u> As you can see, I have created an array containing the values of the
  u> various "foo" keys within the hash.  But that seems a bit kludgy.  It
  u> seems I ought to be able to do something similar to this (with
  u> slicing):

  u>    my @foos = @hash{keys %hash}{'foo'};

  u> But I can't seem to figure out how to implement this approach.

perl 5 can't slice any intermediate levels (hash or array) in a
structure. it can only slice a single ref value which will be at the
lowest level of your structure that you access. intermediate keys/index
values have to be single so they can lookup the next level down. perl6
has such midlevel slice capabilities.

but you can do better than your push code with map:

	my @foos = map $_->{'foo'}, values %hash ;

if you want more levels than you can nest the maps or loop more. if you
need to do that often then wrap that code in subs to make it easier to
use.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Thu, 10 Aug 2006 03:27:05 GMT
From: "Mumia W." <mumia.w.18.spam+nospam.usenet@earthlink.net>
Subject: Re: Slice of a hash of hashes
Message-Id: <dIxCg.6703$0e5.3284@newsread4.news.pas.earthlink.net>

On 08/09/2006 09:23 PM, usenet@DavidFilmer.com wrote:
> Kindly consider the following code which illustrates my question:
> 
> #!/usr/bin/perl
>    use strict;   use warnings;
>    use Data::Dumper;
> 
>    my %hash;
> 
>    $hash{1}{'foo'} = 'one';
>    $hash{2}{'foo'} = 'two';
>    $hash{3}{'foo'} = 'three';
> 
>    my @foos;
>    push @foos, $hash{$_}{'foo'} for keys %hash;
> 
>    print Dumper \@foos;
> 
> __END__
> 
> As you can see, I have created an array containing the values of the
> various "foo" keys within the hash.  But that seems a bit kludgy.  It
> seems I ought to be able to do something similar to this (with
> slicing):
> 
>    my @foos = @hash{keys %hash}{'foo'};
> 
> But I can't seem to figure out how to implement this approach.
> 
> I would appreciate some not-so-kludgy pointers.  Thanks!
> 

I don't consider your original code to be kludgy, but here:

my @foos = map $_->{foo}, values %hash;

UNTESTED CODE




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

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 V10 Issue 9585
***************************************


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