Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by GrandFather (Cardinal)
on Sep 14, 2012 at 01:05 UTC ( #993608=note: print w/ replies, xml ) Need Help??


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

Avoid needless slurping. Use while (<DATA>) instead of introducing an extra variable, slurping into it and using a for loop.

Slurping complicates the code (there are more moving parts), it doesn't scale well, it obscures the intent of the code because it decouples line reading from line processing, and it probably doesn't offer any useful efficiency gains (which shouldn't be a consideration in the first place).

You code could be "cleaned up" as:

#!/usr/bin/env perl use strict; use warnings; my @code_file; my @var_file; my $label_line; while (<DATA>) { if (/\s+EQU\s+\*/) { $label_line = $_; next; } next if ! $label_line; push @{/\A\s*DC/ ? \@var_file : \@code_file}, "$label_line$_"; $label_line = ''; } print join "\n", "CODE_FILE:", @code_file, "VARIABLE_FILE:", @var_file +; __DATA__ LABEL#1 EQU * $MAC ABORT LABEL#2 EQU * $NOT UPDATE,STOP T#TAB1 EQU * DC AL4(-1)
True laziness is hard work


Comment on Re^2: How to push the previous line based on the current line to two different files.
Select or Download Code
Re^3: How to push the previous line based on the current line to two different files.
by kcott (Abbot) on Sep 14, 2012 at 07:19 UTC
    "Avoid needless slurping. Use while (<DATA>) instead of introducing an extra variable, slurping into it and using a for loop."

    The OP (suno) is using a for loop to read through data in the @Read_Array array - that's a variable suno already has, not an extra variable.

    No indication is given of how that array is populated.

    my @Read_Array = <DATA>; (in my code) initialises @Read_Array with the data shown.

    suno's question is not about how to capture the data; it's about how to process it once it's in @Read_Array.

    Finally, suno specifically says "... without using while loop ...". I don't know why that is requirement: perhaps addressing your data capture comments to suno would be more appropriate.

    -- Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2014-09-21 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls