#!/usr/bin/perl use warnings; use strict; use diagnostics; #my personal data left out! print "Generate statistics: Whitespace in context\n"; my $infile = $ARGV[0]; #define regexes as search target (in the array @regexes) my @regexes = (qr/§\s*[0-9]/, qr/Art\.\s*[0-9IVX]/, qr/Artikel\s*[0-9IVX]/, qr/Artikels\s*[0-9IVX]/, qr/Artikeln\s*[0-9IVX]/); open my $in, '<', $infile or die "Cannot open $infile for reading: $!"; #read input file in variable $xml my $xml; { local $/ = undef; $xml = <$in>; } #define array for frequency values my @tally; #count routine for each regex for my $i (0 .. $#regexes) { my $regex = $regexes[$i]; ++$tally[$i] while $xml =~ /$regex/g; } #define output file open my $out, '>', 'stats.txt' or die $!; #output statistics print {$out} "Statistics: Whitespace in context\n\ninput file: "; print {$out} "$infile"; print {$out} "\n========================================================================\n\n"; for my $i (0 .. $#regexes) { my $regex = $regexes[$i]; $regex =~ s/^\(\?\^://; $regex =~ s/\)$//; print {$out} "$regex:\t\t$tally[$i]\n"; } close $in; close $out;