Hello,

I'm pretty new to Perl too, but I have just finished a code that contains some elements that might be useful to you, so I may aswell share it.

#!perl use strict; # because we should use warnings; # because we should use autodie; # die if problem reading or writing a file my $input = 'C:/MIG6/Uploadfiles/Input/AllRAW.txt'; # where to get dat +a my $dir = 'C:/MIG6/Uploadfiles/Output'; # where to put the new files open FILEin, $input; binmode(FILEin); while (my $line = <FILEin>){ if ($line =~ /^"EE",/){ my $search = index($line,'54250213200'); $search += 16; my $dgo = substr($line,$search,13); $search += 16; my $ean = substr($line,$search,18); my $outfile = $dir.'/e07_'.$dgo.'_err_b2c_MIG6testing_'.$ean.' +.txt'; open FILEout, '>'.$outfile; print FILEout $line; } else { print FILEout $line; } } close FILEout; close FILEin; exit 0; # all done, let's have some cake!

Every time a line is found in FILEin that starts with the string "EE", a new file is created and that line is written to the new file, the following lines are also written to that same file until another line that starts with "EE", is found again, which triggers the creation of a new file and so on.

I don't use close anywhere inside the while-block and it works, so I guess I'm doing something right :-)

Not sure if the 'close FILEout;' is of any use outside the while-block, it just looks cleaner to me.

If the code itself brings nothing useful to the table, then only consider the tip of using 'use autodie;', so you can forget about typing 'or die $!;' every time.


In reply to Re: Open Filehandle once by zarath
in thread Open Filehandle once by Dunkellbusch

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!
  • 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, details, 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, summary, 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.