Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Reading "slices" out of a file at known start markers

by smiffy (Pilgrim)
on Oct 03, 2008 at 06:43 UTC ( [id://715149]=note: print w/replies, xml ) Need Help??


in reply to Reading "slices" out of a file at known start markers

OK, here's yet another way of doing it. This uses a state machine (or at least that's what I think is what is meant by a state machine.)

I've tried to take it a bit further to show that this approach can be quite flexible - I've got it to pull out the YACCCOMPILE lines as well to show that you can look for more than one thing and have also added dummy YACCCOMPILE lines to show that you can actually pick up further lines as they occur.

You can, of course, get this to look for as many start points as you want. The code could probably be reduced, but attempting to do so would probably detract from the clarity of the example.

Hope this helps!

use strict; use warnings; my $state=0; my ($yacccompile,$sources); while (<DATA>) { if ($state==0) { # Wait until we see SOURCES. if ($_=~/^SOURCES/) { # Move to next state, re-read line # in that next state. $state=1; redo; } # ...or we see YACCCOMPILE elsif ($_=~/^YACCCOMPILE/) { $state=2; redo; } } elsif ($state==1) { # See if we have reached another section: if ($_=~/^[A-Z]/ && $_!~/^SOURCES/ ) { $state=0; redo; } else { # Supress blank lines. next unless $_=~/\w/; # Add the line to what we have so far. $sources.=$_; } } elsif ($state==2) { # See if we have reached another section: if ($_=~/^[A-Z]/ && $_!~/^YACCCOMPILE/ ) { $state=0; redo; } else { # Supress blank lines. next unless $_=~/\w/; # Add the line to what we have so far. $yacccompile.=$_; } } } # Now print it all out with some titles. print "SOURCES\n-------\n\n"; print $sources; print "\n"; print "YACCCOMPILE\n----------\n\n"; print $yacccompile; print "\n"; __DATA__ YACCCOMPILE = $(YACC) $(YFLAGS) $(AM_YFLAGS) LTYACCCOMPILE = $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) + \ --mode=compile $(YACC) $(YFLAGS) $(AM_YFLAGS) YLWRAP = $(top_srcdir)/ylwrap SOURCES = $(libpiuserland_la_SOURCES) $(pilot_addresses_SOURCES) + \ $(pilot_clip_SOURCES) $(pilot_csd_SOURCES) + \ $(pilot_debugsh_SOURCES) $(pilot_dedupe_SOURCES) + \ $(pilot_dlpsh_SOURCES) $(pilot_file_SOURCES) + \ $(pilot_foto_SOURCES) $(pilot_foto_treo600_SOURCES) + \ $(pilot_foto_treo650_SOURCES) $(pilot_getram_SOURCES) + \ $(pilot_getrom_SOURCES) $(pilot_getromtoken_SOURCES) + \ $(pilot_hinotes_SOURCES) $(pilot_install_datebook_SOURCES) + \ $(pilot_install_expenses_SOURCES) + \ $(pilot_install_hinote_SOURCES) $(pilot_install_memo_SOURCES) + \ $(pilot_install_netsync_SOURCES) $(pilot_install_todo_SOURCES) + \ $(pilot_install_todos_SOURCES) $(pilot_install_user_SOURCES) + \ $(pilot_memos_SOURCES) $(pilot_nredir_SOURCES) + \ $(pilot_read_expenses_SOURCES) $(pilot_read_ical_SOURCES) + \ $(pilot_read_notepad_SOURCES) $(pilot_read_palmpix_SOURCES) + \ $(pilot_read_screenshot_SOURCES) $(pilot_read_todos_SOURCES) + \ $(pilot_read_veo_SOURCES) $(pilot_reminders_SOURCES) + \ $(pilot_schlep_SOURCES) $(pilot_wav_SOURCES) + \ $(pilot_xfer_SOURCES) YACCCOMPILE = niddle naddle noo RECURSIVE_TARGETS = all-recursive check-recursive dvi-recursive + \ html-recursive info-recursive install-data-recursive + \ install-dvi-recursive install-exec-recursive + \ install-html-recursive install-info-recursive + \ install-pdf-recursive install-ps-recursive install-recursive + \ installcheck-recursive installdirs-recursive pdf-recursive + \ ps-recursive uninstall-recursive YACCCOMPILE = foo bar baz

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-24 03:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found