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

__DATA__ (key, value) format test1= 1 2 3 test2= 4 5 6 _output_ 3 3

how to get the size of the value of each key? I have data in %hash

Replies are listed 'Best First'.
Re: Size of the hash value
by karlgoethebier (Abbot) on Feb 22, 2018 at 09:27 UTC

    Did you mean something like this?

    #!/usr/bin/env perl use strict; use warnings; use feature qw(say); use Data::Dump; my %hash = ( test1 => [ 1, 2, 3 ], test2 => [ 4, 5, 6 ] ); dd map { scalar @{ $hash{$_} } } keys %hash; __END__ karls-mac-mini:playground karl$ ./hash.pl (3, 3)

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Size of the hash value
by Tux (Canon) on Feb 22, 2018 at 11:04 UTC

    Size != Length.

    $ perl -MDevel::Size=total_size -wE'my%h=(test1=>[1..3],test2=>[4..6]) +;say"$_ -> ",total_size$h{$_}for keys%h' test1 -> 160 test2 -> 160 $ perl -wE'my%h=(test1=>[1..3],test2=>[4..6]);say"$_ -> ",scalar@{$h{$ +_}}for keys%h' test2 -> 3 test1 -> 3

    Enjoy, Have FUN! H.Merijn
Re: Size of the hash value
by LanX (Saint) on Feb 22, 2018 at 08:33 UTC
    What exactly is the value of test1 and what is the "size" you are expecting?

    The text you show isn't Perl code and doesn't make sense.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

Re: Size of the hash value
by hippo (Archbishop) on Feb 22, 2018 at 09:34 UTC

      Except this will increase the array size (append undef to each array). So it will give a different result on each call, always out of date.

        Indeed so. If harishnv requires nullipotence then another similar approach could be employed. Such as

        say $#$_ + 1 for values %hash;
Re: Size of the hash value
by harishnv (Sexton) on Feb 22, 2018 at 08:11 UTC

    anyone who can help me?

      Read split

      #!perl use strict; my %hash = ( test1 => "1 2 3", test2 => "4 5 6", ); for my $key (keys %hash){ my $lines = split "\n",$hash{$key}; print "$key $lines\n"; }
      poj
      Sure. What you posted is raw data. There is no hash so there are no hash values. Smartalecky answer i know but you posted data not a data structure
      If you don't ask a good question, you won't get a good answer.