in reply to Re: Output minimal occurrences of matching regex(es)
in thread Output minimal occurrences of matching regex(es)

First of all, many thanks for your valuable input! I learn a lot from your comments :-)

I understood that my ($infile, $param) = @ARGV; is a smart way to read two input parameters from the command line. What happens if the second parameter is missing, i.e. perl entityPairs.pl input.xml? Will that mean that the condition in ternary operator expression evaluates to false and the values after ":" are taken?

I've got two issues:

FYI, the current version of the script:

#!/usr/bin/perl use warnings; use strict; use diagnostics; print "Find pair of entities without/with separating space\n"; # read input file and param ('mod' = for modified files) my ($infile, $param) = @ARGV; my @regexes = $param ? (qr/&ndash;&sect;/, qr/&ndash;&Uuml;/, qr/&szlig;&sect;/) : (qr/&[^;]+;\s&[^;]+;/); open my $in, '<', $infile or die "Cannot open $infile for reading: $!" +; #read input file in variable $xml my $xml; { local $/ = undef; $xml = <$in>; } #define output file open my $out, '>', 'pairs.txt' or die $!; #output statistics print {$out} "Find pair of entities without/with separating space\n\ni +nput 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: $1\n" while $xml =~ /$regex/g; } close $in; close $out;

Replies are listed 'Best First'.
Re^3: Output minimal occurrences of matching regex(es)
by hippo (Archbishop) on Nov 14, 2024 at 11:49 UTC
    I've got two issues:

    Actually, you have only one issue with 2 symptoms. Both symptoms are because you are not capturing the results of the match. Use brackets to do that. eg:

    $ perl -we '$regex = qr/foo/; print "$regex: $1\n" while "foob" =~ /$r +egex/g;' Use of uninitialized value $1 in concatenation (.) or string at -e lin +e 1. (?^:foo): $ perl -we '$regex = qr/(foo)/; print "$regex: $1\n" while "foob" =~ / +$regex/g;' (?^:(foo)): foo $

    🦛