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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |