in reply to Out of memory error!!!
Some hints:
Should be followed immediately with#!/usr/bin/perl
use strict; use warnings;
You mix up several ways to open a file. You should take consistently the three parameter form. Also use scalars as file handles. So your
becomesopen (INFO, "<", "$File") or die("Cannot open the flat file"); open(my $LOG,">>LOG_file") or die("Cannot open"); open(XML,">xml_file") or die("Cannot open");
open(my $INFO, '<', $File) or die "Cannot open the '$File': $!; open(my $LOG, '>>' 'LOG_file') or die "Cannot open 'LOG_file: $!"; open(my $XML, '>', 'xml_file') or die "Cannot open 'xml_file': $!";
As stated by davido, you should iterate line by line over the file:
while(defined(my $line2 = <$INFO>)) { }
Every $line2 will have a LF at the end. Probably you want to get rid of it. In this case it's not necessary.
chomp $line2;
It seems you're working on fixed column format files (COBOL output?). The format of the row (line) is defined by the first two bytes (segment). So, instead of creating a big if-elsif-else-tree it would be more maintainable to create a dispatcher based on the segment. The usage of unpack may be your friend to extract the data easily. So I propose the following:
my %config = ( '00' => { 'unpack' => "x40A8", 'fields' => [qw(Var1)], }, 'A0' => { 'unpack' => "x2A9x3A3x3A42A30", 'fields' => [qw(Var1 Var2 Var3 Var4)], }, ); while(defined(my $line = <$INFO>)) { my $segment = substr $line, 0, 2; if(exists $config{$segment}) { my @fields = unpack $config{$segment}->{'unpack'}, $line; my %record; $record{$config{$segment}->{'fields'}->[$_]} = $fields[$_] for +each (0..$#fields); print Dumper(\%record), "\n"; } else { die "ERROR: Unknown segment '$segment'. Config or data error?" +; } }
At the end you have to close the filehandles and not the 'filenames':
close $INFO or die "Can't close file '$File': $!"; close $LOG or die "Can't close file 'LOG_file': $!"; close $XML or die "Can't close file 'xml_file': $!";
These proposals reflect only my personal opinion. Hopefully a little bit helpful.
McA
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Out of memory error!!!
by sathya83aa (Acolyte) on Mar 28, 2013 at 08:12 UTC | |
by Corion (Patriarch) on Mar 28, 2013 at 08:16 UTC | |
by McA (Priest) on Mar 28, 2013 at 08:35 UTC | |
by sathya83aa (Acolyte) on Mar 28, 2013 at 08:59 UTC | |
by McA (Priest) on Mar 28, 2013 at 10:23 UTC | |
by sathya83aa (Acolyte) on Mar 28, 2013 at 13:27 UTC | |
|