Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

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

Usually, when working with such data structures, you're only interested at one node at a time. And when that's the case I find it pretty useful to keep that node in its own variable, so you don't have to bother with the whole structure all the time.

For example, suppose I keep track of the way visistors use my web app from one day to another. To do this, I keep user statistics in an array, where each element represents a day. The last element in the list is yesterday, element -2 is the day before yesterday, et cetera.

Each element is itself a hashref, with at least the key 'username' given. Another key include 'visited_pages', which value is an AoH with information about the pages this particular user visited such as page title and an array of other users who also visited that page on that day.

Suppose I'm tasked to see what pages a certain user, named John Doe, had visited a week ago, and who else has visited the same pages on that day.

my $webapp_usage_statistics = pull_this_data_from_somewhere(); my $johndoe = grep {$_->{username eq "John Doe"}} @{$webapp_usage_stat +istics->[-7]} for my $page (@{$johndoe->{pages}}) { print "$johndoe->{He_or_She} visited $page->{title}\n"; my $others = join(", ", map {$_->{username}} @{$page->{others}}); print " as did $others.\n\n"; }

You see what I did there. Already in line 2 I extract some data from $webapp_usage_statistics and then never bother wit $webapp_usage_statistics again, because I know I'm only interested in that one node of seven days ago where John Doe is the user name.

And I go on like that: the moment I begin looping over John's visited pages of a week ago, I've made a variable that keeps a reference to one page at a time, so I can pull the information from it that I want, without constantly saying $johndoe->{pages}->[$idx]->{title}. Nope, just $page->{title}. And when it comes to showing the other users that visited that page that day I do the same trick. I make map loop over the dereferenced array of users so that I can just say $_->{username} from within the BLOCK. That really beats $webapp_usage_statistics->[-7]->[$some_index]->{pages}->[$some_other_index]->{others}->{username}, which is really what it boils down to.

That code's just there to illustrate a point. It might contain typos, bugs, or oddities. Also, I don't think that writing a webapp usage tracker in this fashion is really a good idea because the data structure is... awkward at best.


In reply to Re: Access Hashes of Hashes etc. by muba
in thread Access Hashes of Hashes etc. by packetstormer

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 studying the Monastery: (7)
As of 2024-04-23 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found