1

I'm new to perl. And I'm writing this script which should fetch any kind of changes that are recorded for a particular table in database from the log file containing a complete log of changes. And these changes in the particular table should be stored in a new file (for now I'm stroing in .txt file but in real have to show in browser, but thats next step).

So my problem is that the output is getting stored as I wish but all the spaces are missing and thus the output is appearing as one string with no spaces. Can anyone help out? HOW SHOULD I BRING THE SPACES BETWEEN WORDS?

Also on debugging I found that(according to me I mean) this is happening because of "while(<@file>)" as this line is taking whole file as one string with no spaces. so can anyone suggest some substitute or solution for this.

The Script is:

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

#take the file as input, (if want from command line use ARGV[0])
open(FILE, "</home/apareek/sample.txt") or die '\nfile not opened $!\n';
open(my $out, ">/home/apareek/output.txt") or die ":P :P";

my $line;
my @file = <FILE>;

#take table name as input
print "\nEnter the table name you want to make query for\n";
my $tbl_name = <STDIN>;
chomp($tbl_name); 
my $new_tabname = $tbl_name . "..."; 

print "\nTable Name: $new_tabname\n"; 
if (grep(m/$new_tabname/, @file) eq 1) {
    print "Table name exsts.\n";
    while(<@file>) {                        
        if (/^$tbl_name/ .. /^Check/) { 
            if($_ !~ /$new_tabname/ and $_ !~ /Checking/) {
                print $out $_;   # $out (is the handler of output file)
            }
        }
    }
}

Input Data:

Checking table BOARD_EQSL34V...
Checking table CFMGlobalTbl...
Checking table CFMMaTable...
!!!!!! Diff001: differences in record definition section
!!!!!! mdIdx : Record definition type differs Unsigned Int(4 Byte)(db1) vs. (db2)
Checking table CFMMdTable...
!!!!!! Diff002: differences in record definition section
!!!!!! mdIdx : Record definition type differs  : Unsigned Int (4 Byte) (db1) vs. (db2)
Checking table CFMMepListTbl...
Checking table CFMMepTable...
ashish
  • 19
  • 7
  • Could you maybe post a section of the input data and how you are reading it into `@file`? – Fabricio Mar 04 '14 at 08:07
  • I have included the input data as you asked. and additional part of the script too so that you can get to know how im reading it into @file. – ashish Mar 04 '14 at 09:05
  • What do you expect `while (<@file>) { ... }` to do? Do you mean `for (@file) { ... }`? – Borodin Mar 04 '14 at 09:14
  • I want while(<@file>) for reading the content of log file so that changes corresponding to the table name can searched – ashish Mar 04 '14 at 10:02

1 Answers1

0

You have a combination of two different ways of doing this. Either you read the whole file into an array in one go

  my @file = <FILE>;
  ....
  for (@file) {

Or you read the file one line at a time (which is preferable if the file might be large)

  while ( <FILE> ) {

I would write this as

  my $filename = '/home/apareek/sample.txt' ;
  open my $file , '<', $filename  or die 'file $filename not opened $!\n';
  while ( <$file> ) {

See Why is three-argument open calls with autovivified filehandles a Perl best practice? for reasons.

Community
  • 1
  • 1
justintime
  • 3,601
  • 4
  • 22
  • 37
  • if I'm writing it as you are doing i.r. "while (<$file>)" it gives error on runtime that is "readline() on unopened filehandle at ./str_parse2.pl" – ashish Mar 04 '14 at 11:14