Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: grepping the location of a value from a datastructure

by RichardK (Vicar)
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
Replies are listed 'Best First'.
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'}};


      my $hash = { "value" => [ { 'chapter' => 2, 'paragraph' => 10 }, { 'chapter' => 2, 'paragraph' => 11 }, ... ], "value2" => [ { 'chapter' => 3, 'paragraph' => 1 } ] };
        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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1048378]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2015-12-01 04:50 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (794 votes), past polls