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 | |
by stevieb (Canon) on Nov 25, 2019 at 17:02 UTC | |
Re^4: Reversing the action of the while loop
by Scotmonk (Sexton) on Nov 25, 2019 at 17:10 UTC | |
by haukex (Archbishop) on Nov 25, 2019 at 21:23 UTC | |
by marioroy (Prior) on Nov 25, 2019 at 22:52 UTC |
In Section
Seekers of Perl Wisdom