ImpalaSS has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I have a small problem. Again lemme state this is not homework, but an assignment for my boss. He wants me to take 3 files, and combine the last lines of them in a table. The files follow this format:
 Fri Oct 13 2000 20:11:54                                              Page  1

Weekly Stats for DE-NJ-MD sites

 RF Block | DCCH Block | Disp Queue | RF Drop | Erl/BR |
   1.40 % |    0.24 %  |    1.39%   |   2.04% |   2.35 |
The problem is, when i open the file and load it into an array, for somereason, the array shows up blank. Here is part ofmy code incase i am doing something wrong:
$site_dnm = "/mnt/csphil/home/fburns/domereports/Weekly_Reports/de-nj- +md"; $site_pa = "/mnt/csphil/home/fburns/domereports/Weekly_Reports/pa_only +"; $site_pm = "/mnt/csphil/home/fburns/domereports/Weekly_Reports/phila_m +arket"; # ------ Read files #read De-Nj-Md open($site_dnm); @DNM = <$site_dnm>; close($site_dnm); #read PA open(pa, $site_pa); @PA = <pa>; close(pa); #read in lat and long file open(LL, $site_pm); @PM = <PM>; close(PM);

Thank you very much
Dipul Patel

Replies are listed 'Best First'.
Re: opening a file
by Fastolfe (Vicar) on Oct 31, 2000 at 23:34 UTC
    It would probably help you immensely if you followed these oft-spoken guidelines while debugging your code:
    1. Always use strict;. This would have pointed out a few of your problems right away.
    2. Always check to see if your system functions succeeded or failed. Specifically, your open call.
    3. Always use strict;.
    4. When debugging, run your script using Perl's -w flag (or use warnings; in Perl 5.6). This tends to point out other things you're doing wrong.
    In your first chunk of code, your open call is missing an argument. In your third chunk of code, you're opening the LL file handle but reading from the PM file handle. Again, 'use strict' and '-w' would have caught this. As far as the array in the 2nd chunk of code, perhaps it's failing to open the file for some reason? Check for the success/failure of open to see.

    Hope this helps.

RE: opening a file
by arturo (Vicar) on Oct 31, 2000 at 22:34 UTC

    Your code:

    open($site_dnm); @DNM = <$site_dnm>; close($site_dnm);

    One immediate problem is that open requires a filehandle AND a filename, i.e. open FOO, "foo.txt";

    Or, even better,

    open FOO, "foo.txt" or die "Can't open foo.txt: $!\n";

    Which will abort the script and tell you why it couldn't open.

    Then, when you read, you read from the filehandle, i.e. my @foo = <FOO>;

    See perlfunc:open for more details.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      Technically speaking, open does not require a filehandle and a filename. From the page you referred to:

      Opens the file whose filename is given by EXPR, and associates it with FILEHANDLE. If FILEHANDLE is an expression, its value is used as the name of the real filehandle wanted. If EXPR is omitted, the scalar variable of the same name as the FILEHANDLE contains the filename. (Note that lexical variables--those declared with my()--will not work for this purpose; so if you're using my(), specify EXPR in your call to open.)

      That said, it's rare/dangerous/not-a-good-idea to use this fuctionality. I'd recommend using the two-parameter form you describe.

      Cheers,
      Shendal
      The problem is, the files dont have an extension, they are just called de-nj-md for example. Kilinrax, I tried it the way you showed me and the array's still came up blank? Any suggestions??
      Thanks Guys
      Dipul
        This script will check the files you named to see that they all exist, are readable by the script, and are not empty.
        Hopefully, running it will help you pinpoint the problem.
        #!/usr/bin/perl -w use strict; my @files = ("/mnt/csphil/home/fburns/domereports/Weekly_Reports/de-nj +-md", "/mnt/csphil/home/fburns/domereports/Weekly_Reports/pa_only", " +/mnt/csphil/home/fburns/domereports/Weekly_Reports/phila_market"); my $file; foreach $file (@files) { if (! -e $file) { warn "$file does not exist"; next; } warn "$file is unreadable" unless -r $file; warn "$file has zero size" if -z $file; }
Re: opening a file
by kilinrax (Deacon) on Oct 31, 2000 at 22:32 UTC
    Your syntax is all over the place, this is how you want to read the files:
    open DNM, "/mnt/csphil/home/fburns/domereports/Weekly_Reports/de-nj-md +"; @dnm = <DNM>; close DNM; print @dnm;
    I would also suggest you read File Input and Output in the Tutorials section.
Re: opening a file
by c-era (Curate) on Oct 31, 2000 at 22:48 UTC
    This should work (I don't have perl on the pc I'm on now, but it should work). Each of the last lines should be in @lines (this way also uses up less memory).
    my @files = ("/mnt/csphil/home/fburns/domereports/Weekly_Reports/de-nj +-md", "/mnt/csphil/home/fburns/domereports/Weekly_Reports/pa_only", " +/mnt/csphil/home/fburns/domereports/Weekly_Reports/phila_market"); my $file; my (@data, @lines); for $file (@files){ open (FH, $file) || die "Unable to open file $file\n"; @data = <FH>; close (FH); push (@lines, $data[$#data]); } undef @data; # Don't waste memory
Re: opening a file
by ImpalaSS (Monk) on Oct 31, 2000 at 23:48 UTC
    Thank you Everyone. IT works now :)
    Dipul