Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Length of array in hash of arrays

by Grimy (Pilgrim)
on May 12, 2013 at 08:43 UTC ( [id://1033178]=note: print w/replies, xml ) Need Help??


in reply to Length of array in hash of arrays

You're aware that none of these solutions actually print the length of the array, right? Well, they do so in this example, but by pure serendipity. The actual “standard” way to get the length of the array would be $#{$g{k}} or 0+@{$g{k}}

Replies are listed 'Best First'.
Re^2: Length of array in hash of arrays
by ambrus (Abbot) on May 12, 2013 at 08:50 UTC

    Yes, I'm aware of that. That's why this is post is in Obfuscated Code rather than Meditations. If you want to get the real solution, the simpler

    use 5.012; no warnings; my @t = map { rand } 1..60; my %g = (k => \@t) +; for my $n (0..1e3) { (my $s = $n) =~ y/0-9/ {}@#$%=^~+/; my $c= qq( +${s}g{k}}); if (60 == eval $c) { warn $c; } }
    prints it together with another fake solution.

      The simplest way must contain the function 'length':

      length(!()x@{(\%x)->{k}})
Re^2: Length of array in hash of arrays
by gurpreetsingh13 (Scribe) on May 13, 2013 at 05:29 UTC
    Like this:
    $ perl -e 'my %g = (k => [8, 2, 10, 2, 1, 3],l => [10, 7, 9, 0, 1]); f +oreach $keyval(sort keys %g){print 0+@{$g{$keyval}};}'

      That prints only the length of the first subarray, the one corresponding to the key "k". Let's do better and use the each keyword to print the length of each subarray.

      my %g = (k => [8, 2, 10, 2, 1, 3],l => [10, 7, 9, 0, 1]); %gkey = sort keys %g; print 0+@{$g{each %gkey}}, "\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 00:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found