Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Some hints:

#!/usr/bin/perl
Should be followed immediately with
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

open (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");
becomes
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


In reply to Re: Out of memory error!!! by McA
in thread Out of memory error!!! by sathya83aa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (11)
    As of 2015-07-02 08:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (31 votes), past polls