Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Output minimal occurrences of matching regex(es)

by LexPl (Sexton)
on Nov 14, 2024 at 11:16 UTC ( [id://11162700]=note: print w/replies, xml ) Need Help??


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:

  • The output only shows $regex, but not $1 (the current value), so for example &[^;]+;\s&[^;]+;: for each occurrence in the original file and for example –§: in the modified file.
  • There is a warning with respect to the line that prints the occurrences.

    print {$out} "$regex: $1\n" while $xml =~ /$regex/g;

    Use of uninitialized value $1 in concatenation (.) or string at entityPairs.pl line 42, <$in> line 1 (#1)
    (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.

    To help you figure out what was undefined, perl will try to tell you the name of the variable (if any) that was undefined. In some cases it cannot do this, so it also tells you what operation you used the undefined value in. Note, however, that perl optimizes your program and the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program.

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 $

    🦛

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11162700]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2025-02-15 14:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found