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

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

by kcott (Archbishop)
on Sep 13, 2012 at 06:27 UTC ( [id://993377]=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.

G'day suno,

This does what you want based on your input. I'll leave you to supply appropriate filehandles for the output.

#!/usr/bin/env perl use 5.010; use strict; use warnings; my @Read_Array = <DATA>; my (@code_file, @var_file); my $var_file_re = qr{ \A \s* DC }x; my $label_re = qr{ \s+ EQU \s+ \* }x; my $label_line = ''; for (@Read_Array) { if (/$label_re/) { $label_line = $_; next; } if ($label_line) { $label_line .= $_; if (/$var_file_re/) { push @var_file, $label_line; } else { push @code_file, $label_line; } $label_line = ''; } } say 'CODE_FILE:'; print join "\n" => @code_file; say 'VARIABLE_FILE:'; print join "\n" => @var_file; __DATA__ LABEL#1 EQU * $MAC ABORT LABEL#2 EQU * + $NOT UPDATE,STOP T#TAB1 EQU * + DC AL4(-1)

Output:

$ pm_capture_last.pl CODE_FILE: LABEL#1 EQU * $MAC ABORT LABEL#2 EQU * + $NOT UPDATE,STOP VARIABLE_FILE: T#TAB1 EQU * + DC AL4(-1)

-- Ken

Replies are listed 'Best First'.
Re^2: How to push the previous line based on the current line to two different files.
by GrandFather (Saint) on Sep 14, 2012 at 01:05 UTC

    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
      "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

Re^2: How to push the previous line based on the current line to two different files.
by suno (Acolyte) on Sep 13, 2012 at 07:10 UTC
    Hi ,

    Thanks for the help. But here in this i am able to print only the line immediate after 'EQU *'...

    I wish to pring evrything after EQU *..

    say suppose my input is

    LABEL#1 EQU * $MAC ABORT LABEL#2 EQU * $NOT UPDATE,STOP T#TAB1 EQU * DC AL4(-1) L#DESK DC XL6 DESKRIPTOR L#SYMBOL DC CL30 SYMBOL L#TKKEY DC XL4 VALOREN-NUMMER FNK#005 EQU * CLI V#MUT,X'FF' WURDE MUTIERT ? BNE F05#000 NEIN MVI FUNK,4 MDF-TG ERZEUGEN $CALL MOD850,UP F$SFH TR_COMMIT
    here according the above perl code u gave me, the output i aquire is:
    CODE_FILE: LABEL#1 EQU * $MAC ABORT LABEL#2 EQU * $NOT UPDATE,STOP FNK#005 EQU * CLI V#MUT,X'FF' WURDE MUTIERT ? VARIABLE_FILE: T#TAB1 EQU * DC AL4(-1)

    where i wish to get...

    CODE_FILE: LABEL#1 EQU * $MAC ABORT LABEL#2 EQU * $NOT UPDATE,STOP FNK#005 EQU * CLI V#MUT,X'FF' WURDE MUTIERT ? BNE F05#000 NEIN MVI FUNK,4 MDF-TG ERZEUGEN $CALL MOD850,UP F$SFH TR_COMMIT B F05#100 VARIABLE_FILE: T#TAB1 EQU * DC AL4(-1) L#DESK DC XL6 DESKRIPTOR L#SYMBOL DC CL30 SYMBOL L#TKKEY DC XL4 VALOREN-NUMMER

    What change shall i make in the above perl code so as to get this output?

      What change shall i make in the above perl code so as to get this output?

      Do you have any guess?

Log In?
Username:
Password:

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

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

    No recent polls found