http://qs321.pair.com?node_id=11109171


in reply to Re^2: Reversing the action of the while loop
in thread Reversing the action of the while loop

Hi Scotmonk,

This is a great use-case for using an ordered hash (like a hash but with key insertion order preserved). Fortunately, such a module exists. For this demonstration, I'm more interested in the hash keys, not the hash values (i.e. hash key-driven implementation).

# https://www.perlmonks.org/?node_id=11109163 use strict; use warnings; use feature 'say'; use Hash::Ordered; tie my %elems, 'Hash::Ordered'; tie my %match, 'Hash::Ordered'; tie my %nomatch, 'Hash::Ordered'; my $skip_duplicates = 0; # set to 1 to skip duplicates my $num_elements = 13; # number of elements to read my $num_read = 0; # number of elements read my @lines = <DATA>; chomp @lines; # read elements while ( @lines && $num_read < $num_elements ) { my $line = shift @lines; foreach my $elem ( split / /, $line ) { if ( $skip_duplicates ) { $num_read++ unless exists $elems{ $elem }; } else { $num_read++; } $elems{ $elem } = undef; last if $num_read == $num_elements; } } say "data elements"; say join(' ', keys %elems); # matched, not matched if ( @lines ) { foreach my $elem ( split / /, shift @lines ) { ( exists $elems{ $elem } ) ? $match{ $elem } = undef : $nomatch{ $elem } = undef; } say "match"; say join(' ', keys %match); say "nomatch"; say join(' ', keys %nomatch); } else { say "no more lines"; } __DATA__ 1 2 6 4 5 6 7 8 9 10 1 2 11 12 13 6 14 15 16 17 2 18 19 20 21

Output: $skip_duplicates = 0

data elements 1 2 6 4 5 7 8 9 10 11 match 6 nomatch 14 15 16 17

Output: $skip_duplicates = 1

data elements 1 2 6 4 5 7 8 9 10 11 12 13 14 match 2 nomatch 18 19 20 21

Regards, Mario

Replies are listed 'Best First'.
Re^4: Reversing the action of the while loop
by Scotmonk (Sexton) on Nov 25, 2019 at 16:53 UTC
    Thankyou Mario

    I get an error on execution
    Can't locate Hash/Ordered.pm in @INC (@INC contains: C:/Dwimperl/perl/site/lib C:/Dwimperl/perl/vendor/lib C:/Dwimperl/perl/lib .) at compare001.pl line 7.

    How can I remedy this ?
    Thankyou
    Michael

      You have to install the library: cpanm Hash::Ordered.

Re^4: Reversing the action of the while loop
by Scotmonk (Sexton) on Nov 25, 2019 at 17:10 UTC
    I successfully installed Hash::Ordered
    still gettng this message. Is there something else I should do ?
    Can't locate Hash/Ordered.pm in @INC (@INC contains: C:/Dwimperl/perl/site/lib C:/Dwimperl/perl/vendor/lib C:/Dwimperl/perl/lib .) at compare001.pl line 8. BEGIN failed--compilation aborted at compare001.pl line 8.
      I successfully installed Hash::Ordered ... still gettng this message. Is there something else I should do ? ... Can't locate Hash/Ordered.pm

      Do you perhaps have two installations of Perl on your machine, and you installed it into the wrong one?

      Have you tried cpanm from the bin directory (assuming cpanm lives there). Not having Dwimperl, I took a guess for where the bin directory might be.

      C:\Dwimperl\perl\bin\cpanm Hash::Ordered