perl-extract data from hash values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl-extract data from hash values
# 1  
Old 01-15-2010
perl-extract data from hash values

Hello,

I have parsed an xml file using perl to get the hash values and the output looks like this

Code:
$VAR1 = {
 
'RT' => {
            'List' => {
                        'String' => [
                                      'abcde',
                                        'prrst',                                                                                       
                                      'mnopqr',
                                     ]                                                                                                                                                                                           ]
                       }
         },
'dPS' => {
            'List' => {
                        'String' => [
                                      'abcde',
                                      'prrst',                                                          
          'mnopqr',
                                    ]
                      }
         },
'RT1' => {
              'List' => {
                          'Map' => [
                                     {
                                     'MapEntry' => {
                                       'firstname' => {
                                                      'value' => 'firstname'
                                                      },
                                       'accountId' => {
                                                      'value' => 'abcdef'
                                                      },
                                                                                                                                                                                               },
                                       'middleinitial' => {
                                                      'value' => 'J'
                                                      },
                                       'lastname' => {
                                                      'value' => 'lastname'
                                                      },
                                                                                                        
                                                                                                 
                                      {
          'MapEntry' => {
                                       'firstname' => {
                                                      'value' => 'firstname1'
                                                      },
                                       'accountId' => {
                                                      'value' => 'abcdefg'
                                                      },
                                                                                                                                                                                               },
                                       'middleinitial' => {
                                                      'value' => 'J'
                                                      },
                                       'lastname' => {
                                                      'value' => 'lastname1'
                                                      },
                                                              
}


I am looking to get the output from 'RT' tag and just the accountid value from the 'RT1' tag.

I was thinking of using a slice but am not sure on how to use it for this hash. Any thoughts on how to approach this would be really helpful.

Thanks,

Last edited by pludi; 01-15-2010 at 05:54 PM.. Reason: code tags, please...
# 2  
Old 01-17-2010
If the hash has a uniform pattern why not directly read the hash instead of using slice

Code:
foreach (@{$hash{'RT1'}{'List'}{'Map'}}){
        print "$_->{'MapEntry'}{'accountId'}{'value'} \n";

}

abcdef
abcdefg

where $hash is the name of the hash . replace $hash with the name of the hash which you are using in the code.

HTH,
PL
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl giving unexpected md5 hash values

I am trying to speed up creating a line by line hash file from a huge file using Perl. Here is my current (working but too slow) Bash code: (while read line; do hash=$(echo -n $line | md5sum); echo ${hash:0:32}; done)And here is my Perl code: perl -MDigest::MD5 -le 'foreach $line ( <STDIN> )... (3 Replies)
Discussion started by: Michael Stora
3 Replies

2. Shell Programming and Scripting

Perl : Assigning multile hash values to a single array

I know that @food = %fruit; Works. But how do I assign %fruit and %veggies to @food ? (2 Replies)
Discussion started by: popeye
2 Replies

3. Shell Programming and Scripting

Perl :: reading values from Data Dumper reference in Perl

Hi all, I have written a perl code and stored the data into Data structure using Data::Dumper module. But not sure how to retreive the data from the Data::Dumper. Eg. Based on the key value( Here CRYPTO-6-IKMP_MODE_FAILURE I should be able to access the internal hash elements(keys) ... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. Shell Programming and Scripting

Finding unique values in a hash (Perl)

Hi, I have a hash with unique keys associated with some data. my %FINALcontigs = ( 'mira_rep_c765:119reads**', 'ctctactggaagactgac', 'mira_rep_c7454:54reads**', 'atggatactgcgctgttgctaactactgga', 'mira_rep_c6803:12reads**', 'atcgactggatgcagggttgtggtttcta', ... (2 Replies)
Discussion started by: jdilts
2 Replies

5. Shell Programming and Scripting

Perl: Printing null hash values as a " "?

I'm filling in a table of values for grades. I decided to go with reading into a hash from the files but I'm coming up with an error when printing a value that does not exist. I need to know if I can on-the-fly print a space (" ") or blank in place of the grade. Here's what the output should... (2 Replies)
Discussion started by: D2K
2 Replies

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

7. Shell Programming and Scripting

Sorting values of hash in ascending order using Perl

I want to sort values of a hash in ascending order. my %records; for my $value (sort values %records){print $value,"\n";} When I use the above code I get values in this order: 1,10,11,2,3,4,5,6,7,8,9. But, I need values in my output in this order: 1,2,3,4,5,6,7,8,9,10,11. Can Someone... (1 Reply)
Discussion started by: koneru_18
1 Replies

8. Shell Programming and Scripting

Remove default data hash sorting in perl script?

Hi, I have a datahash with 'n' number of values in perl script. I am writing a xml file from the datahash. I am getting output with sorting(Field sorting). My question is that i don't want any default sorting.whatever i am inserting into datahash it should give same xml file. Any help? ... (0 Replies)
Discussion started by: solo123
0 Replies

9. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

10. Shell Programming and Scripting

PERL: reading 2 column data into Hash file

I am trying to read in a 2 column data file into Perl Hash array index. Here is my code. #!/usr/bin/perl -w use strict; use warnings; my $file = "file_a"; my @line = (); my $index = 0; my %ind_file = (); open(FILE, $file) or die($!); while(<FILE>) { chomp($_); if ($_ eq '') { ... (1 Reply)
Discussion started by: subhap
1 Replies
Login or Register to Ask a Question