Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Comment on

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

Some hints:

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");
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.


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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (3)
    As of 2018-01-21 18:52 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (229 votes). Check out past polls.