Hello,

This is my first stab at writing a Perl script and could use another set of eyes. My current script is below and the error message I am getting.

Basically I am trying to get all the txt files from a directory, read through each file line by line, and then separate the records into separate files. Each record is pipe delimited with 13 items in each record. Separation of the records is determined by the value of the first three characters of the 8th item in each record.

Error Messages:

Use of uninitialized value $var8 in substr at test.pl line 24, <IN> li +ne 2. Use of uninitialized value $var8 in substr at test.pl line 24, <IN> li +ne 17. ksh: IN: cannot open server: Use of uninitialized value $var8 in substr at test.pl line 24, + <IN> line 3. ksh: IN: cannot open servername: Use of uninitialized value $var8 in substr at test.pl line + 24, <IN> line 4. ksh: IN: cannot open servername: Use of uninitialized value $var8 in substr at test.pl line + 24, <IN> line 5. ksh: IN: cannot open servername: Use of uninitialized value $var8 in substr at test.pl line + 24, <IN> line 6. ksh: IN: cannot open servername: Use of uninitialized value $var8 in substr at test.pl line + 24, <IN> line 7. ksh: IN: cannot open servername: Use of uninitialized value $var8 in substr at test.pl line + 24, <IN> line 8. ksh: IN: cannot open servername: Use of uninitialized value $var8 in substr at test.pl line + 24, <IN> line 9. ksh: IN: cannot open servername: Use of uninitialized value $var8 in substr at test.pl line + 24, <IN> line 10. ksh: IN: cannot open servername: Use of uninitialized value $var8 in substr at test.pl line + 24, <IN> line 11. ksh: IN: cannot open servername: Use of uninitialized value $var8 in substr at test.pl line + 24, <IN> line 12. ksh: IN: cannot open servername: Use of uninitialized value $var8 in substr at test.pl line + 24, <IN> line 19. Use of uninitialized value $var8 in substr at test.pl line 24, <IN> li +ne 20. Use of uninitialized value $var8 in substr at test.pl line 24, <IN> li +ne 21. <br><br>
Script:

#!/usr/bin/perl -w @files = </myhome/incoming/*.txt>; foreach $file (@files) { print $file . "\n"; open(IN,$file) || die ("Could not open file"); open(OUT131,'>>/myhome/outgoing/data131.txt'); open(OUT186,'>>/myhome/outgoing/data186.txt'); open(OUT999,'>>/myhome/outgoing/data999.txt'); while (<IN> ne "") { my $line = IN; chomp($line); my $firstchar = substr($line,0,1); if($firstchar eq "#") { next; } my ($var1,$var2,$var3,$var4,$var5,$var6,$var7,$var8,$var9,$var10 +,$var11,$var12,$var13)=split(/\|/, $line); #variables are separated b +y commas. my $type = substr($var8,0,3); if($type eq 131) { print OUT131 $line . "\n"; } elsif($type eq 186) { print OUT186 $line . "\n"; } else { print OUT999 $line . "\n"; } } close(IN); close(OUT131); close(OUT186); close(OUT999); my $newfile='/roamdev/htdocs/RIEFID/incoming/processed/new_file.txt +'; #use File::Copy; #move($file, $newfile) or die "The move operation failed: $!"; } exit 0;
Could use some assistance getting past this error. Thanks ahead of time!

In reply to New to Perl - Script Error by sumnerdu

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.