Hello ardibehest, and welcome to the Monastery!

As LanX says, once you’ve opened a file you need to access it via the filehandle, not the filename. In addition, you should get into the habit of using the 3-argument form of open, using lexical variables instead of barewords for filehandles, calling close on the filehandle when the file is no longer being used, and always checking open and close for success or failure:

#!/usr/bin/perl use strict; use warnings; use constant { DATA_FILE => 'try.txt', MATCHED_FILE => 'matched.txt', UNMATCHED_FILE => 'unmatched.txt', }; open(my $input, '<', DATA_FILE) or die "Cannot open file '" . DATA_FILE . "' for reading: $!" +; open(my $matched, '>', MATCHED_FILE) or die "Cannot open file '" . MATCHED_FILE . "' for writing: $!" +; open(my $unmatched, '>', UNMATCHED_FILE) or die "Cannot open file '" . UNMATCHED_FILE . "' for writing: $!" +; while (my $line = <$input>) { chomp $line; my ($left, $right) = split /=/, $line; my @left_array = split / /, $left; my @right_array = split / /, $right; my $left_count = scalar @left_array; my $right_count = scalar @right_array; if ($left_count == $right_count) { print $matched "$line\n"; } else { my $diff = abs($left_count - $right_count); print $unmatched $line, "($diff) \n"; } } close $unmatched or die "Cannot close file '" . UNMATCHED_FILE . "': $ +!"; close $matched or die "Cannot close file '" . MATCHED_FILE . "': $ +!"; close $input or die "Cannot close file '" . DATA_FILE . "': $ +!";

Some notes:

It’s great that you use strict and warnings. Here are some additional pragmas you will find handy:

As you are new to Perl, be sure to check out chromatic’s book Modern Perl, which is available for free download at http://modernperlbooks.com/mt/index.html.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Identifying unmatched data in a database by Athanasius
in thread Identifying unmatched data in a database by ardibehest

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.