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

Re: number of keys and values in a hash

by jethro (Monsignor)
on Oct 12, 2011 at 09:30 UTC ( #930948=note: print w/replies, xml ) Need Help??

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


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-01-29 14:05 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (74 votes). Check out past polls.