Q-1) Is this approach fine or is there any better way to do it?

It's fine so long as your dataset is regular and well-formed. If it starts to become less so there are other options: use a different format (XML, JSON, YAML, etc.), use a module (say Text::xSV or Text::CSV_XS), pre-process it, etc.

Q-2) Will it be better to embed the data in input.txt in the code itself rather than keeping it in a separate file?

Almost invariably no. Do you use a version control system? Hopefully the answer is "yes" in which case you would be committing deltas every time you change the dataset if it were embedded in the source. Equally, anyone else using your code would have to fire up an editor and know enough perl to be able to change the data without messing up the script syntax. Far, far better to have the data separated from the code.

The only time it is a good idea to embed data in code is for testing (including SSCCE). Your test data may not change (usually should not change) as if it did your test results might not be conclusive or reproducible.

All IMHO, of course.


In reply to Re: What is better: Static input data in separate file or embedding static input data in code. by hippo
in thread What is better: Static input data in separate file or embedding static input data in code. by Perl300

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.