store information in hash and display number of keys.


 
Thread Tools Search this Thread
Top Forums Programming store information in hash and display number of keys.
# 1  
Old 10-11-2011
store information in hash and display number of keys.

I am trying to store this information (info and number) in hash. number is the key and info is value in a hash.i shown my code below.
Code:
    #!/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 to open file");
 
    while ( $reader->nextElement( 'DATA' ) ) {
     my $info = $reader->readOuterXml();
     $reader->nextElement( 'number' ); 
     my $number = $reader->readInnerXml(); 
     print( "num: $number\n" );
     print( " datainfo: $info\n" );

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.
Code:
    my %nums =( "$number", $info);
 
    while ((my $keys, my $values) = each (%nums)) { 
     print ("NUMBER:$keys." =>"INFORMATION: ".$values." \n");
    }
 
    my $key_count = keys %nums;
    print "$key_count";
    } 
    close($file);

when i am trying to excute it,it gives only one number but i have more numbers. may be my hash contains one number? how can iterate my hash to sotre more number of elements.

Last edited by veerubiji; 10-12-2011 at 05:01 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to store info from a txt file into a hash?

I'm trying to make a perl script using the "open" command to open and read a file, storing the information in said file into a hash structure. This is what is inside my file- Celena Standard F 01/24/94 Cancer Jeniffer Orlowski F 06/24/86 None Brent Koehler M 12/05/97 HIV Mao Schleich... (4 Replies)
Discussion started by: Eric1
4 Replies

2. Shell Programming and Scripting

How to display information of the script while its running?

how to display the messages on screen IN PERL such as " 1. Entering while loop" if the script enters a while loop. 2. Checking FILENAME. etc... 3. Print statement is also not helpful.... (1 Reply)
Discussion started by: Rashid Khan
1 Replies

3. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

4. Shell Programming and Scripting

How to store a number along with the sign

If I use the below cut -c37-48, the minus sign does not get appended to the number, as the sign is in the 36th Position. But, If I use cut -c36-48, the minus sign appears infront of the number, but for positive numbers, it throws an error. How can this be resolved.. (7 Replies)
Discussion started by: Haimanti
7 Replies

5. Shell Programming and Scripting

Sorting keys of a hash in perl

hi all, i have a small problem regarding sorting the keys in a hash. my %hash; for($i=0;$i<19;$i++) { $hash{$i}=$i; } foreach $c (sort keys %hash) { print "\n $hash{$c}"; } (1 Reply)
Discussion started by: niteesh_!7
1 Replies

6. Shell Programming and Scripting

Perl: Any quick way to use regex on hash keys?

Hi, Is there any quick way to use pull out keys that match a specific regex pattern? eg %hash ; $hash(123,456) = xxx; $hash(123,457) = xxx; $hash(123,458) = xxx; $hash(223,459) = xxx; I need a fast way to get all the keys that start with 123.. Meaning I should get ... (5 Replies)
Discussion started by: Leion
5 Replies

7. Linux

Need to store information relating to certain files

I need to save information relating to certain files that are projected to deleted. I am using find $defPath/archive/sub/subchild -type f -mtime +365 > LOGFILE cat LOGFILE| while read line do ls -l $line | cut -d" " -f 10-15 done But, since cut is cutting columns on space (single... (3 Replies)
Discussion started by: ramu_indian
3 Replies

8. Shell Programming and Scripting

How to set dynamically keys names in perl %hash

hello I have loop , in this loop im picking names , this names I want to be keys in %hash but I don't know how to set in every loop entertain different key in the %hash (1 Reply)
Discussion started by: umen
1 Replies

9. Shell Programming and Scripting

To display VTOC information

Hello everybody! 1. Can someone explain, how to obtain the information about every logical volume in the system? 2. And then list vtoc on every volume in the system? O.S.: UnixWare 7.1.* shell: ksh (1 Reply)
Discussion started by: gold4u
1 Replies
Login or Register to Ask a Question