Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Selecting successive lines

by sundialsvc4 (Abbot)
on Sep 17, 2013 at 12:50 UTC ( #1054432=note: print w/ replies, xml ) Need Help??


in reply to Selecting successive lines

The customary approach to doing this sort of thing is the approach taken by the awk utility:  

/regular_expression /
  { code to execute if regex is matched }
... rinse and repeat ...

So, in this file, there would be .. it looks like .. about five different “kinds” of lines, including blank-line, and you have things-to-do with two of them.   For a student_name line, you capture the name and proceed.   For a Score(n) line, you extract the score and do something with it, using the most-recently captured student name.   Perhaps for a blak-line you forget the name.   And so on.

One advantage of this approach is that it is relatively “future-proof.”   You are making fewer assumptions about the data, such as “the third line.”   It is also now much easier for your program to recognize when there is a bug in the program that produced the file, which is another important consideration in production settings.


Comment on Re: Selecting successive lines

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2015-07-06 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (71 votes), past polls