http://qs321.pair.com?node_id=930948


in reply to number of keys and values in a hash

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;

Replies are listed 'Best First'.
Re^2: number of keys and values in a hash
by veerubiji (Sexton) on Oct 12, 2011 at 10:04 UTC

    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).

        thanks