Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: grepping the location of a value from a datastructure

by RichardK (Parson)
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2018-02-17 22:26 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (250 votes). Check out past polls.