Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

number of keys and values in a hash

by veerubiji (Sexton)
on Oct 11, 2011 at 14:18 UTC ( #930826=perlquestion: print w/replies, xml ) Need Help??

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

hi, i am trying to find out numbers keys and number of values in hash. and print the number. i written my code like this but it not given number of keys.what is the error in my code.

how can i store these num and datainfo in a hash. and how can i count number of keys in hash. I did like this but not working. In the above code i have only one element i am calling so it displying one element how can i iterate for all numbers in my hash.

Replies are listed 'Best First'.
Re: number of keys and values in a hash
by choroba (Archbishop) on Oct 11, 2011 at 14:30 UTC
    Your code has two opening curly brackets, but three closing ones. Please, post the real code, otherwise we can not help you.
    Are you sure you are not calling keys inside the each loop in the real code? That would explain the infinite loop.

      hi choroba, Upto printing number and datainfo is ok, it displying toatl numbers and thier datainfo but after that i need to sotre these numbers into hash so i created hash,but in my hash only one element is there how can use all elements in my hash.

Re: number of keys and values in a hash
by pvaldes (Chaplain) on Oct 11, 2011 at 14:28 UTC

    Several ways to do the same, i.e.

    my $number = keys %myhash;   print $number;

    Updated (to avoid duplication in the other post)

Re: number of keys and values in a hash
by biohisham (Priest) on Oct 11, 2011 at 16:47 UTC
    As was indicated to you, using the 'keys' keyword in scalar context can tell you about how many keys are there in the hash, the number of values will be similar of course to the number of keys, but to have a count of values in a way that is more sensible you will need to filter out the undefined values and return only those that are defined, in this context, using grep in conjunction with defined is a good way to go
    use strict; use warnings; my %hash = ( 'name'=>'hisham', 'keyword'=>undef, 'place'=>'the Monastery' ); my $key_count = keys %hash; my $value_count = grep {defined} values %hash; print $key_count,"\n"; print $value_count,"\n";


    David R. Gergen said "We know that second terms have historically been marred by hubris and by scandal." and I am a two y.o. monk today :D, June,12th, 2011...
Re: number of keys and values in a hash
by jethro (Monsignor) on Oct 12, 2011 at 09:30 UTC

    I think many here are confused because you split your script into two halves.

    Instead of

    my %nums =( "$number", $info);

    which initializes (i.e. removes previous information in) a hash, you should use

    $nums{$number}= $info;

      i tried like that as you said but i have one doubt

      #!/usr/bin/perl use warnings; use strict; use XML::LibXML::Reader; my $file;open $file, 'formal.xml'); my $reader = XML::LibXML::Reader->new( IO => $file ) or die ("unable t +o open file"); while ( $reader->nextElement( 'DATA' ) ) { my $info = $reader->readOuterXml(); $reader->nextElement( 'number' ); my $number = $reader->readInnerXml(); $nums{$number}=$info; print( "num: $number\n" ); print( " datainfo: $info\n" }

      it excuting but can i print hash directly not using

      print( "num: $number\n" ); print( " datainfo: $info\n"
      i tried like this
      print %nums;
      but it excuting infinity times. and also how to specify input path outside the module.

        Sigh, I think that your real problem is the lack of a solid basis. I don't want to be rude but it seems that you don't know exactly what are you doing. If you need help to iterate, print a hash or even open a file, the best advice that I can give you is: "Stop now, stop to write code for a while and read some manuals".

        how can i count number of keys in hash can you tel me how to create hash table in my code. how can i iterate all keys in my hash I need to to fill my hash with all keys

        If you open the manual for the function keys (perldoc -f keys) you will have the answer to your first question in the fourth line. If you take a look to the faqs (perldoc perlfaq) you will see that the perlfaq4 cover "How do I process an entire hash?" and "How can I know how many entries are in a hash?"

        Well... just read the manual, we can't do this for you...

        Naturally when you put a print statement inside a loop it prints as often as the loop is executed. And when you have a print statement inside a loop inside another loop, this print statement will be executed much much more often. If you don't want to print so many times, put the print statement AFTER the loop(s).

Re: number of keys and values in a hash
by JavaFan (Canon) on Oct 12, 2011 at 06:32 UTC
    You are calling "keys" inside the while loop where you are doing "each". Calling "keys" resets the "each" loop. Which explains the infinite loop.

    As pointed out, there's no need to iterate over the hash using each. Considering you are calling keys - I've no idea why you are using each.

    Furthermore, since you build the hash as a one key, one value pair, why the need to find out the number of keys? The answer to that is already known, isn't it?

      hi javafan, I need to to fill my hash with all keys, still now i have only one element in hash. so i need to fill all elements in hash then disply the number of keys.

        Uhm, why don't you count them while filling in? ;-)
Re: number of keys and values in a hash
by pvaldes (Chaplain) on Oct 11, 2011 at 14:36 UTC
    Updated: sorry the former was duplicated, deleted now:

    normally the number of keys = number of values, if a value is missing is filled automatically, if a value is undef is not printed, but you still can call it. If you need to discard the null values you can always add a test or if-block

      hi Its not working, it giving only one number is there but i have 148 numbers.can you tel me how to create hash table in my code.
        well, of course you will need to create and fill a hash first with the desired part of your data. The code tells you exactly what you have: my %nums =( "$number", $info) is a hash with one element only, a single pair key-value. You need to iterate between all keys and all values

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2021-12-06 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (33 votes). Check out past polls.

    Notices?