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.