Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Storing an indeterminate number of hash keys in a variable

by wind (Priest)
on Jul 08, 2011 at 21:38 UTC ( #913427=note: print w/ replies, xml ) Need Help??


in reply to Storing an indeterminate number of hash keys in a variable

It'd be easier if you just stored the list of keys in an array ref instead of a string

However, you could just parse the keys and traverse the data structure to find the proper value like so:

use strict; use warnings; my @array = ( '{title}', '{Rule}{Severity}', '{Rule}{weight}', '{Rule}{id}', '{Rule}{version}', '{Rule}{title}', '{Rule}{detailedDescription}{VulnDiscussion}', '{Rule}{detailedDescription}{Responsibility}', '{Rule}{detailedDescription}{IAControls}', '{Rule}{check}{check-content}', '{Rule}{fixtext}{content}', ); for my $firstKey (sort keys %hashData) { for my $arrayKey (@array) { my $val = $hashData{$firstKey}; # Traverse to desired value $val = $val{$1} while $arrayKey =~ /\{(.*?)\}/g; print $val, "\n"; } }


Comment on Re: Storing an indeterminate number of hash keys in a variable
Download Code
Re^2: Storing an indeterminate number of hash keys in a variable
by banesong (Acolyte) on Jul 09, 2011 at 20:06 UTC
    Thanks; that looks like it will work. Just curious, what do you mean store it as an array ref, or more importantly, why would it be better?

    I realize that a bit more background might be useful; I am post processing a bunch of gigantic, complex XML files. The reason for the strings at the moment is to allow users to alter the the tagged output without my intervention. It is hard enough trying to explain the naming convention of XML and how to annotate it, let alone try and get them to understand Perl data structures/syntax. T

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-08-01 02:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls