Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Counting matches and removing duplicates

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


in reply to Re: Counting matches and removing duplicates
in thread Counting matches and removing duplicates

First of all, many thanks for your valuable input and kind assistance!

I have managed to generate a statistics for the XML entities in a file in two steps:

  1. generate a list of XML entities via entityList.pl
  2. generate a statistics which describes each entity and its frequency via entityStat.pl
Would it be possible to combine the functionality of the 2 perl scripts into one comprehensive script? If yes, how could I achieve this?

entityList.pl

#!/usr/bin/perl use warnings; use strict; my $infile = $ARGV[0]; print "List of entities in ", $infile, "\n"; #define regexes as search target (in the array @regexes) my $regex = qr/(&[^;]+;)/; 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, '>', 'ent-list.txt' or die $!; #output list of entities print {$out} "$1\n" while $xml =~ /$regex/g; close $in; close $out;

entityStat.pl

#!/usr/bin/env perl use strict; use warnings; my $infile = $ARGV[0]; my $outfile = $ARGV[1]; open(IN, '<' . $infile) or die "Cannot open $infile for reading: $!"; open(OUT, '>' . $outfile) or die $!; chomp(my @matches = <IN>); my %freq; $freq{$_}++ for @matches; print OUT "Statistics of entities in '", $infile, "'\n================ +=================================\n"; for my $entity (sort keys %freq) { printf OUT "%-20s %10s \n", $entity, $freq{$entity}; }

Replies are listed 'Best First'.
Re^3: Counting matches and removing duplicates
by hippo (Archbishop) on Nov 15, 2024 at 16:24 UTC

    Yes, of course. In your combined script, instead of printing each line of matches, simply push them onto @matches. There is no need to write them out and then read them in again.


    🦛

      So instead of
      print {$out} "$1\n" while $xml =~ /$regex/g;
      , I would simply write

      my @matches; push(@matches, $1);
      and then continue with my code from entityStat.pl.

      Is that right?

        Yes, but you will need the same loop. ie.

        my @matches; push @matches, $1 while $xml =~ /$regex/g;

        🦛

Log In?
Username:
Password:

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

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

    No recent polls found