Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: grepping the location of a value from a datastructure

by rastoboy (Monk)
on Aug 07, 2013 at 17:31 UTC ( [id://1048388]=note: print w/replies, xml ) Need Help??


in reply to Re: grepping the location of a value from a datastructure
in thread grepping the location of a value from a datastructure

That's a fair point. I'm doing a text search through a book. I've parsed the book into chapters and paragraph numbers, so that when I find the location I can say "text found at paragraph 10 in chapter 2".
  • Comment on Re^2: grepping the location of a value from a datastructure

Replies are listed 'Best First'.
Re^3: grepping the location of a value from a datastructure
by derby (Abbot) on Aug 07, 2013 at 17:51 UTC

    Then you should probably invert your data structure. Instead of

    my $hash = { base => { key => 'value', key2 => 'value2'}};

    consider

    my $hash = { "value" => [ { 'chapter' => 2, 'paragraph' => 10 }, { 'chapter' => 2, 'paragraph' => 11 }, ... ], "value2" => [ { 'chapter' => 3, 'paragraph' => 1 } ] };
    -derby
      You are a f*cking genius. LMAO.

      Of course, that would work perfectly!

      Thanks! I'd still be interested if anyone has other clever ideas though, in principle.

Re^3: grepping the location of a value from a datastructure
by Jim (Curate) on Aug 07, 2013 at 19:03 UTC

    I agree with derby. Your data structure should explicitly model the whole book, including the text, the chapter numbers, and the paragraph numbers. It's better not to infer chapter numbers and paragraph numbers implicitly from their positions within a Perl nested data structure. A well-designed data structure will be extensible. If you decide later you want to model another attribute of the book—say, sentence numbers—you can do so without having to re-implement the data structure or create a new one. The data structure will then also map neatly to an external, reusable representation such as XML.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1048388]
help
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 17:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found