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



Hi Hamish

I think what you are looking might be Tie::File
http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm

This way you don't have to read the file into memory, but can still
work on it as if it was an array.

Hope that helps,
Marcel


HAMISH wrote:
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

  • Reading whole file with while(INPUT) but I need to access each line
    ... It was then pointed out that the FAQ has a better way of reading files. ... How would I do this using the input file Code from ... Because in that case I would not have an array of lines. ...
    (perl.beginners)
  • Inputstream e BufferedReader....help me!
    ... I have to develop a JSF application that allow user to upload a txt ... Later the application has to store the file into a byte's array in ... Populate an HashMap from an input file. ... first value on the line is a String. ...
    (comp.lang.java.programmer)
  • Re: I sure hope someone can help with this. how to use getline and multible arrays
    ... A slight adjustment allows you to store them into an array. ... string, ... > I first thought that I could open 2 streams to the input file. ... Losing the pointer argument isn't hard... ...
    (alt.comp.lang.learn.c-cpp)
  • Re: NumberFormatException:please help me......!!!
    ... Record that has string and array of double as members) and file could ... follows it on a line of the input file, you could use the String as a key to ... contain String representations of doubles. ...
    (comp.lang.java.programmer)
  • Re: Reading a table
    ... Resulting array: ... void no_free; ... ** Close temp input file after reading. ... if (rc!= EOF) { ...
    (comp.lang.c)