I'm working on a script to parse long .ics (iCal export) files to remove every event that occurs after a date.
I'm looking at the date section of the DTSTART event property of the VEVENT (see theRFC.) to determine whether the entire VEVENT component (as defined as everything that goes between BEGIN:VEVENT and END:VEVENT) should be included in the output file.
But for some reason this isn't happening properly. The entire input file is ending up in the output file here.
If I match for a Beginning event using !~/BEGIN:VEVENT/ in line 29 and =~ /BEGIN:VEVENT/ in line 30 no events are put into the output file. This is quite confusing.
#!/usr/bin/perl use strict; my ($foo, $bar); my $infile = $ARGV[0]; my $enddate = $ARGV[1]; if (!$infile||!$enddate){print "Syntax calarchiver.pl <filename> <endd +ate> (as 'YYYYMMDD')"; exit;} my $outfile=$infile; $outfile =~ s/.ics/.archive.ics/g; open (IN,'<', $infile) or die "Cannot open $infile"; open (OUT, '>', $outfile) or die "cannot open $outfile"; my $inevt =0; my $evt=''; my $badevt=0; my $instr; my @parts; my @dparts; while (<IN>) { $instr = $_; chomp($instr); @parts = split(/:/,$instr); PARSE: { if (($instr ne "BEGIN:VEVENT") && !$inevt){ print OUT $_; last PAR +SE;} if ($instr eq "BEGIN:VEVENT"){$inevt = 1; $evt .= $instr; last PAR +SE;} if ($parts[0] =~ /DTSTART/){ @dparts = split(/T/, $parts[1]); if ($dparts[0] > $enddate){$badevt=1; last PARSE;} else {$badevt =0; $evt .= $instr; last PARSE;} } if ($inevt && !$badevt) {$evt .=$instr; last PARSE;} if ($instr eq "END:VEVENT"){ if ($badevt){$inevt=0;$badevt=0;$evt='';print "bad event \n"; +last PARSE;} else {$evt .= $instr; print " good event \n"; print OUT $evt; + $inevt=0;$badevt=0;$evt=''; last PARSE;} } $foo=$bar; } } close OUT; close IN; exit;
In reply to Problems with seemingly simple string matching... by desertrat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |