Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Splitting a file based on matched conditions

by Marshall (Abbot)
on Sep 19, 2017 at 18:27 UTC ( #1199691=note: print w/replies, xml ) Need Help??


in reply to Splitting a file based on matched conditions

Below is some "starter code" for you showing another method of doing this. Rather setting flags in the main body of the code, calling a subroutine to handle processing the record can be a good way. If you are in the subroutine, then that means that you are inside the record - no need to have a specific flag for that status..

100K lines isn't big enough to worry much about seeking around. I'd just save the data in a buffer and then decide what to do with it when you see the End line.

You can also look at: Flipin good, or a total flop?

I use a variety of methods for similar tasks with some attention to who I'm writing the code for and the level of Perl expertise I expect them to have. Most of the time the number of lines of code is meaningless as long as it is clear and well structured.

#/usr/bin/perl use strict; use warnings; #### Not complete code #### #### This is just a starting framework ### while (my $line = <DATA>) { handle_section ($line) if $line =~ /^Start:/; } sub handle_section { my $start_line = shift; my @buf; my ($parm) = $start_line =~ /Start:(.*)/; push @buf, $start_line; print "Staring Parm is: $parm\n"; my $line; while (defined ($line = <DATA>) and $line !~ /End/) { push @buf, $line; } # do something based upon End line (still in $line) # If not an End line, then malformed record # end of data was reached without an End line } __DATA__ Start:/abc/def ..... End 1.2 Start:/xyz/uvw ..... End 2.8

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1199691]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2017-12-18 05:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (467 votes). Check out past polls.

    Notices?