Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by wind (Priest)
on Jul 08, 2011 at 21:38 UTC ( [id://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"; } }

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://913427]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found