Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Greetings all, I'm confused with a little piece of code I've written. I was asked to come up with a program that would take two or more files, and tell what action would be taken by the first that would not be taken by any of the others. The file's format is for each line, it is either a colon separated list, starts with an 'I' which means to include another file, comments begin with a #, and blank lines are ignored. So, I recursively parse these files (when I hit an 'I', I open that file and process it). What I find wierd is that the while(<$fh>) structure seems to be reading an extra line at the end of each file that is more than one level deep in the recursion. I "fixed" the problem by turning warnings off, but for curiosity's sake I would like to know what's going on. Any ideas? My code is below
#!/usr/local/perl/bin/perl -w use strict; my %hash; my $cal = shift; read_cal($cal, 0); foreach my $file (@ARGV) { read_cal($file, 1); } foreach my $key (sort keys %hash) { print "$key\n" if $hash{$key} == 1; } sub read_cal { my $cal = shift; my $fh; print "---------------$cal-----------------\n"; my $mode; open($fh, $cal) or die; while(<$fh>) { chomp; s/\s//g; next if /^#/; next if /^\s*$/; next if $_ eq ""; if (/^I/) { read_cal(substr($_,1), $mode); } print "|$_|\n"; my $schedule = ((split(':'))[-1]); if (!$mode || exists($hash{$schedule}) ) { $hash{$schedule}++; } } }

thanks in advance,
thor


In reply to Recursive file opening: reading an undef at the end of file by thor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-25 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found