http://www.perlmonks.org?node_id=991770

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

Hi all ,this perl code uses the three text files(included after the code).Im getting this error -- Unknown format list.txt. at code.pl line 30, <$fh> line 41.

code.pl

#!/usr/bin/perl use strict; use warnings; use List::Util qw/ max /; my @matrix; my $path = '.'; # (current directory - '.') or path to data files my @file = qw/list.txt one.txt another.txt/; for my $file ( @file ) { my %data; my @substrate; open my $fh, "<", "$path/$file" or die "Unable to open $file for reading. $!"; while (<$fh>) { if (/^substrate/) { @substrate = /\d+/g; } elsif (/^product/) { while (/(\d+)/g) { for my $sub (@substrate) { $data{$sub}{$1} = 1 ; } } } else { die "Unknown format $file. $!"; } } close $fh or die "Unable to close $file. $!"; print "Processing file: $file\n"; process(%data); push @matrix, \%data; } for my $i (0 .. $#matrix) { for my $j ($i+1 .. $#matrix) { print "Combining $file[$i] and $file[$j]\n"; my %data = combine($matrix[$i], $matrix[$j]); process(%data); } } sub process { my %data = @_; my %seen; my @product = sort {$a <=> $b} grep ! $seen{$_}++, map keys %$_, values %data; # to get column width for print my $wid = 1 + max map length, @product; printf "%7s" . "%${wid}s" x @product . "\n", 'prod->', @product; for my $substrate (sort {$a <=> $b} keys %data) { printf "%7s", $substrate; printf "%${wid}s", $data{$substrate}{$_} || '-' for @product; + print "\n"; } printf "\n%5s\n%5s\n%s\n\n", '^', '|', 'substrate'; } sub combine { my ($matrix1, $matrix2) = @_; my %new_hash = %$matrix1; for my $substrate (keys %$matrix2) { $new_hash{$substrate}{$_} = 1 for keys %{ $matrix2->{$substrat +e} }; } return %new_hash; }
-----3 DATA FILES---

list.txt

substrate: 1 2 product: 3 substrate: 6 9 product: 8 10 substrate: 3 product: 6 substrate: 9 product: 5 substrate: 5 product: 2 substrate: 3 product: 9 substrate: 8 product: 9 substrate: 8 product: 1 substrate: 7 product: 11 substrate: 19 product: 17 substrate: 14 product: 13 substrate: 14 product: 11 substrate: 18 product: 19 substrate: 7 14 product: 15 substrate: 7 16 product: 7 17 substrate: 5 product: 6 substrate: 18 15 product: 7 substrate: 7 8 product: 8 18 substrate: 6 product: 9 substrate: 11 product: 12
one.txt
substrate[s]: 3649 product[s]: 3419 3648 substrate[s]: 3645 product[s]: 3647 substrate[s]: 3659 product[s]: 3647 substrate[s]: 3675 product[s]: 3674 substrate[s]: 3674 product[s]: 3490 3489 substrate[s]: 3489 product[s]: 3490 substrate[s]: 3490 product[s]: 3485 substrate[s]: 3485 product[s]: 3486 substrate[s]: 3486 product[s]: 3488 substrate[s]: 3488 product[s]: 3487 substrate[s]: 3487 product[s]: 3877 substrate[s]: 3877 product[s]: 3419 substrate[s]: 3182 product[s]: 1875 substrate[s]: 2809 product[s]: 3182 substrate[s]: 3186 product[s]: 2809
another.txt
substrate[s]: 3647 product[s]: 3419 3248 substrate[s]: 3645 product[s]: 3641 substrate[s]: 3609 product[s]: 3645 substrate[s]: 3685 product[s]: 3374 substrate[s]: 3674 product[s]: 3390 3490 substrate[s]: 3491 product[s]: 3490 substrate[s]: 3490 product[s]: 3485 substrate[s]: 3489 product[s]: 3486 substrate[s]: 3986 product[s]: 3488 substrate[s]: 3499 product[s]: 3487 substrate[s]: 3287 product[s]: 3877 substrate[s]: 3877 product[s]: 3419 substrate[s]: 3182 product[s]: 1875 substrate[s]: 2809 product[s]: 3182 substrate[s]: 3186 product[s]: 2809

Replies are listed 'Best First'.
Re: Bad file descriptor error
by Corion (Patriarch) on Sep 05, 2012 at 08:11 UTC

    Maybe you want to improve your error message to also output the offending line, instead of only outputting the number of the offending line?

    die "Unknown format in file '$file' line $. : [$_]";
      Sorry for that,but this is the only error Im getting :-
      Unknown format list.txt. Bad file descriptor at code.pl line 30, <$fh> + line 41.
        And what is on the line 41 of list.txt? An empty line?
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        So, have you looked at line 30?

        What have you done to improve the error message that line 30 produces?

        Maybe consider reading die to find out what that subroutine does.

Re: Bad file descriptor error
by choroba (Cardinal) on Sep 05, 2012 at 08:14 UTC
    How is the title related to the question?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Oh - I completely missed that.

      The title in the question likely stems from a erroneous use of $! in the die message.

      As an explanation to the OP, please read perlvar about $!. It rightly says there:

      Many system or library calls set errno if they fail, to indicate the cause of failure.

      Using $! in situations that are not immediately after a related system call is usually wrong.