Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: grepping the location of a value from a datastructure

by RichardK (Priest)
on Aug 07, 2013 at 16:37 UTC ( #1048378=note: print w/ replies, xml ) Need Help??


in reply to grepping the location of a value from a datastructure

I wonder if this is an XY Problem?

Can you explain what you are going to do with this *location* once you've got it?


Comment on Re: grepping the location of a value from a datastructure
Re^2: grepping the location of a value from a datastructure
by rastoboy (Monk) on Aug 07, 2013 at 17:31 UTC
    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".

      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.

      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
Node Status?
node history
Node Type: note [id://1048378]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-12-21 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (101 votes), past polls