Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to push the previous line based on the current line to two different files.

by aaron_baugher (Deacon)
on Sep 13, 2012 at 07:14 UTC ( #993388=note: print w/ replies, xml ) Need Help??


in reply to How to push the previous line based on the current line to two different files.

Forbidding the use of a while loop seems like an odd homework requirement, since a while loop is the idiomatic way to read data in Perl. But you're welcome to use this 100% while-free solution:

UPDATE: I see that the requirements changed, and records can no longer be assumed to be divided by a blank line. But the point remains: rather than reading line-by-line and trying to succeeding lines based on previous ones, it's often much easier to read block-by-block, if it's possible to find a way to set the input record separator to do so.

#!/usr/bin/env perl use Modern::Perl; open my $labels, '>', 'labels.txt'; open my $tables, '>', 'tables.txt'; { $/ = ''; START: my $record = <DATA>; exit unless $record; my $fd = $record =~ /\*\s+DC/s ? $tables : $labels; print $fd $record; goto START; } __DATA__ LABEL#1 EQU * $MAC ABORT LABEL#2 EQU * + $NOT UPDATE,STOP T#TAB1 EQU * + DC AL4(-1)

Aaron B.
Available for small or large Perl jobs; see my home node.


Comment on Re: How to push the previous line based on the current line to two different files.
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-07-29 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (212 votes), past polls