Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

using List::MoreUtils::zip with anon arrays ?

by leocharre (Priest)
on Aug 28, 2006 at 19:57 UTC ( [id://570036]=perlquestion: print w/replies, xml ) Need Help??

leocharre has asked for the wisdom of the Perl Monks concerning the following question:

I wanted to populate an anon array with stat from a file, with labels.... so..

#!/usr/bin/perl -w use strict; use List::MoreUtils qw(:all); use Smart::Comments; my @labels =qw(dev ino mode nlink uid gid rdev size atime mtime ctime +blksize blocks); my @stat = stat "/tmp"; my $stat = { zip @labels, @stat}; ### $stat

That works great. Prints out :

### $stat: {
###          atime => 1156789541,
###          blksize => 4096,
###          blocks => 32,
###          ctime => 1156791103,
###          dev => 64768,
###          gid => 0,
###          ino => 2684353,
###          mode => 17407,
###          mtime => 1156791103,
###          nlink => 15,
###          rdev => 0,
###          size => 12288,
###          uid => 0
###        }

But why declare the arrays in the fist place? ...

#!/usr/bin/perl -w use strict; use List::MoreUtils qw(:all); use Smart::Comments; my $stat = { zip qw(dev ino mode nlink uid gid rdev size atime mtime ctime blksize bl +ocks), ( stat '/tmp' ) }; # No! Don't like that! ### $stat

gives:

Type of arg 1 to List::MoreUtils::mesh must be array (not list) at tmp +.pl line 16, near "} }"

I can't figure out the correct way/syntax to have zip/mesh interpret these as arrays :( - Array? Not list? does this mean we must predeclare as an array?

Replies are listed 'Best First'.
Re: using List::MoreUtils::zip with anon arrays ?
by Tanktalus (Canon) on Aug 28, 2006 at 20:05 UTC

    Try:

    my $stat = { zip @{[qw(dev ino mode nlink uid gid rdev size atime mtime ctime blksi +ze blocks)]}, @{[ stat '/tmp' ]} };
    Not that it's particularly nice to look at, but it does seem to satisfy the interpreter, at a likely cost to the runtime speed...

      Yeah, sure does make it happier. Thank you!

      I'm wondering now if this is too 'clever' to be using in production code. Maybe zip should take lists as well as actual arrays?

      There must be some internal reason for why the developers did it this way... ? Over my head.

Re: using List::MoreUtils::zip with anon arrays ?
by stvn (Monsignor) on Aug 28, 2006 at 21:39 UTC

    Why use zip at all, when a slice assignment will work just fine.

    #!/usr/bin/perl -w use strict; use List::MoreUtils qw(:all); use Test::More no_plan => 1; my @labels =qw(dev ino mode nlink uid gid rdev size atime mtime ctime +blksize blocks); my @stat = stat "/tmp"; my $stat = { zip @labels, @stat }; my %stat; @stat{@labels} = @stat; is_deeply($stat, \%stat, '... look twins!');
    And you can just as easily do it inline if you want.
    use strict; use Data::Dumper; my %stat; @stat{ qw(dev ino mode nlink uid gid rdev size atime mtime ctime blksize +blocks) } = (stat "/tmp"); print Dumper \%stat; __OUTPUT__ $VAR1 = { 'blksize' => 4096, 'ctime' => 1156800398, 'rdev' => 0, 'blocks' => 0, 'uid' => 0, 'dev' => 234881026, 'mtime' => 1156800398, 'mode' => 17407, 'size' => '238', 'nlink' => 7, 'atime' => 1156749300, 'ino' => 5795297, 'gid' => 0 };
    However, I think nothingmuch has the right idea, use a module.

    -stvn
Re: using List::MoreUtils::zip with anon arrays ? (&)
by tye (Sage) on Aug 28, 2006 at 20:17 UTC

    Another alternative is to override the (overused) prototypes:

    &zip( [qw( ... )], [ stat '/tmp' ] )

    [Note the &, and don't leave off the ( ).]

    - tye        

Re: using List::MoreUtils::zip with anon arrays ?
by philcrow (Priest) on Aug 28, 2006 at 20:11 UTC
    Indeed, you must use actual arrays (though as a previous poster responded, they need not be named). The reason is the prototype. The eventual method signature looks like this:
    sub mesh (\@\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\ +@\@\@) {
    Meaning that everything you pass in MUST be an array. A list or array reference will not do. Given the nature of the module, there is probably a speed efficiency reason.

    Phil

Re: using List::MoreUtils::zip with anon arrays ?
by nothingmuch (Priest) on Aug 28, 2006 at 21:16 UTC
    Not that it answers your question directly, but have you seen File::stat?

    -nuffin
    zz zZ Z Z #!perl

      Yes, thank you.
      The thread's really not about getting the stat data to a hash, but about usage of zip in the extended utilities. This was just an example.
      I must say though, stvn's example of slice assignment is another pretty interesting way to solve the problem.

        Yes, slices are awesome.

        Another little known feature of hash slices is that they are localizable:

        use Data::Dumper; my %hash = ( foo => 1, bar => 2 ); warn Dumper(\%hash); { local @hash{qw/bar moose/} = qw/happy joy/; warn Dumper(\%hash); } warn Dumper(\%hash); __END__ $VAR1 = { 'bar' => 2, 'foo' => 1 }; $VAR1 = { 'bar' => 'happy', 'moose' => 'joy', 'foo' => 1 }; $VAR1 = { 'bar' => 2, 'foo' => 1 };
        -nuffin
        zz zZ Z Z #!perl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://570036]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found