Reading whole file with while(INPUT) but I need to access each line



Hi perl hackers

Previously I was reading a file a line at a time

@lines = <input>

It was then pointed out that the FAQ has a better way of reading files.
This is with

open (INPUT, $file) || die "can't open $file: $!";
while (<INPUT>) {
chomp;
# do something with $_
}
close(INPUT) || die "can't close $file: $!";

However I need to change every line that is read. I also have to
sometimes add lines together based on a certain condtion so maybe 4
lines need to be turned into 1. I have a C background so I chose to
use a for loop so i can access different elements. Here is an example
of my code without all the file reads etc. I have put an example in an
array.

The example input from the C file
if( ( A) || ( B)
||()
||()
||()
{


CODE EXAMPLE
----------------------------------------------------------------------

#!/usr/bin/perl
use warnings;
use strict;

my @lines;
my $lines;
my $i;
my $j;

@lines = ("if( ( A) || ( B) \n", "||() \n", "||() \n", "||() \n",
"||()) \n", "{" );

for( $i = 0 ; $i < scalar @lines ; $i++)
{
chomp @lines;
if( $lines[$i] =~ /\)$/ )
{
$j = 1;
while ($lines[$i+$j] =~ /^\|\|/ ) # if line starts with || add it to
the previous line
{
$lines[$i] = $lines[$i]." ".$lines[$i+$j];
$j++;
}
}

unless ( $lines[$i] =~ /^\|\|/ ) #skip lines that start with || so
they dont get printed twice
{
print $lines[$i]."\n";
}
}

This comes out with the right result. It might be lucky but I have
tested it with a few examples of what might come up.


Now my question is.. How would I do this using the input file Code from
the Faq? Because in that case I would not have an array of lines. Is
there a way this can be done or do I need to create the array of lines?

The input files are C code so I want to create code that joins expanded
If statements that may go over as many as 5 lines.

after I have worked this out I will apply the same code for all extened
expressions that go over multiple lines.

.



Relevant Pages

  • Re: Reading whole file with while(INPUT) but I need to access each line
    ... work on it as if it was an array. ... It was then pointed out that the FAQ has a better way of reading files. ... chomp @lines; ... How would I do this using the input file Code from ...
    (perl.beginners)
  • Re: set/get in c++.
    ... >> of an array to the address of the first element except inside the ... I've contributed to the FAQ. ... > messages defending lame ideas on Usenet groups. ...
    (comp.lang.cpp)
  • Re: Proper coding? (JS newbie)
    ... Document content looks like HTML 4.01 Transitional ... also in the FAQ. ... array; one fewer thing to change if another button is added. ... Use W3C standard ...
    (microsoft.public.scripting.jscript)
  • FAQ 5.2 (Was: inserting lines)
    ... I recently complained about the lack of alternate approaches to the FAQ ... I think this fundamental Perl operation deserves more approaches other ... You can "slurp" one or more files into an array, modify the array, ...
    (comp.lang.perl.misc)
  • Re: making bytes out of bits
    ... Seee the comp.lang.c FAQ for more details. ... You could declare an array ... void load_flat_array (int *a, int n) ... And you have the FAQ book which is ...
    (comp.lang.c)