Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

If I understand your current approach correctly, you have a bunch of meta tags and each tag has a set of data associated with it (value, char count, etc). When you said you're "creating a mess of hashes on the fly", it sounds like you're either using symbolic references (don't do that) or doing something like this:

%title = ( value => 'Welcome to my website!', char_ct => 22, word_ct => 4, repeat_ct => 1 ); %keywords = ( value => 'candy, food, pop, candy cane, soda, caffeine', char_ct => 31, word_ct => 5, repeat_ct => 2 ); etc...
I think what you're looking for here is a hash of hashes (which is actually a hash of hash references). You could combine the %title and %keywords hashes from above into a single hash:
%tags = ( title => { value => 'Welcome to my website!', char_ct => 22, word_ct => 4, repeat_ct => 1 }, keywords => { value => 'candy, food, pop, candy cane, soda, +caffeine', char_ct => 31, word_ct => 5, repeat_ct => 2 } );
Now you've got a single hash to store all your data in, and you don't need to worry about creating hundreds of individually declared hashes.

Getting data into this HoH (and accessing it) is simple. Instead of doing:

$title{value} = 'Welcome to my website!';
do this:
$tags{title}{value} = 'Welcome to my website!';
New inner (e.g., value) and outter (e.g., title) keys can be added at will.

Keep in mind that the value of $tags{title} is actually a reference to another hash. If you wanted to use the hashrefs instead of accessing the data directly (e.g., passing it to a subroutine), you would dereference it like this:

( $num_chars, $num_words ) = count_chars_words( $tags{title} ); sub count_chars_words { my ( $hashref ) = @_; my $value = ${ $hashref }{value}; # count stuff and return }
Since all of the inner hashes were set up the same, the subroutine doesn't need to know (or care) about which hashref it gets, all it knows is that there is a key named 'value' in the hash.

What if you want to add an array to the HOH? Let's add a new key to the inner hash, whose value is an arrayref:

$tags{keywords}{val_list} = [ ( split( ', ', $tags{keywords}{value} ) +) ]; print ${ $tags{keywords}{val_list} }[2]; # pop

References are very powerful and provide nearly unlimited flexibility in your data structure. They are well worth learning. The obligatory reference list: perlref, perlreftut, and perldsc.

HTH.


In reply to Re: Introduction to anonymous arrays and hashes by bobf
in thread Introduction to anonymous arrays and hashes by sulfericacid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-23 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found