in reply to Reading selected portion of a data file

If you are the same Anonymous Monk who posted another question using this same sample data, why are you posting the data again, and still not posting any of your own code?

If you are not the same AM as the one who posted this data before, one of your classmates has already "done your work" for you (and may have given a better description of the homework problem). Refer to the link above for some clues.

The next time you post at PerlMonks with a homework problem, show your work (post some of your own code), and maybe spend some time searching and reading docs on your own before you post.

  • Comment on Re: Reading selected portion of a data file

Replies are listed 'Best First'.
Re^2: Reading selected portion of a data file
by Anonymous Monk on Jul 10, 2006 at 12:57 UTC

    I am the same person who posted the previous question.

    I have figured out a logic to build the data structure for which I had requested help in the previous post but I need some help on how to read a selected portion to proceed further.

    If you notice the difference in the question from the previous post, all I am asking is how to go to certain line no of the data file and start reading till another match is found.

    Can I use a 'seek' command or is there way to directly go to certain line no in a file and read subsequent lines?

      If you have already loaded the data into a hash structure, why would you need to seek back into the file and read it again? (Why not just work with the data in the hash?)

      Why are you still asking the question before trying something on your own? Or, if you have tried something, why did you not show that to us?

      If you are reading the data one "record" at a time (as suggested in the previous thread), you can keep track of the byte offsets at the start of each record (offset to first record is 0, offset to second record = length of first record, and so on). If you read the file line-by-line instead, you can store the byte offsets at the beginning of each line. In either case, the byte offsets should probably be values in a hash that are keyed by whatever indexing string will be used to choose a location in the file.

      Then, if you really do need to go back to a specific location in the file, use seek() with the appropriate byte offset. Naturally, if your goal is to write back to the file (changing its contents at specific locations), things will be more complicated, and you probably just want to rewrite the whole file from your initial hash structure that stores all the file data.

      "I have figured out a logic ... but I need some help"

      In such a case, please UPDATE (and NOT by overwriting your previous) the original node, or add info in a reply to the original node, with a note in the original pointing to the reply.

      and, re "seek"

      NO; not unless you know the "offset (re which, see above) -- the number of bytes into the file at which your "certain line" begins.

        I have added the code and comments in the original node. Please have a look and suggest for improvements