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

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.