In your original code, the value of
$_ gets clobbered after the recursive call to
read_cal. The work around is to save the input line into a temporary variable, instead of relying on the Perl built-in
$_ variable which can get modifed due to side effects somewhere else in the script.
I have modified your script and I believe the following will work without problem -
#!/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, $mode) = @_;
print "---------------$cal-----------------\n";
my $fh;
open($fh, $cal) or die;
while(my $line = <$fh>) {
chomp($line);
next if $line =~ /^\s*$/;
next if $line =~ /^#/;
$line =~ s/\s//g;
# print "before $cal, |$line|\n";
read_cal(substr($line,1), $mode) if $line =~ /^I/;
print "|$line|\n";
my $schedule = (split ':', $line)[-1];
if (!$mode || exists $hash{$schedule}) {
$hash{$schedule}++;
};
}
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.