almsdealer has asked for the wisdom of the Perl Monks concerning the following question:
Greetings. I am baffled by a problem in my code below. I am trying to remove duplicate entries from @fileTags on line 29, but some non-duplicate items are also being dropped.
#!/usr/bin/perl use strict; use warnings; sub myTrim { my ($str) = @_; my ($trimmed) = ($str =~ /\s*(.*)\s*/); return $trimmed; } my @allTags = (); foreach my $file (<[0-9][0-9][0-9][0-9].txt>) { my @fileTags = (); open(my $handle, '<', $file) or die 'couldn\'t open file'; while (my $line = <$handle>) { if ($line =~ /^tags/) { my ($tagsString) = ($line =~ /^tags\s+(.*)/); @fileTags = (@fileTags, split(/,/, $tagsString)); } } close($handle); # some of the items in @fileTags disappear (not just duplicate ite +ms) my %uniqueTags = map { myTrim $_, 1 } @fileTags; @allTags = (@allTags, keys %uniqueTags); } print "@allTags\n";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Mapping list to hash is dropping list items
by haukex (Archbishop) on Mar 12, 2022 at 21:44 UTC | |
by almsdealer (Acolyte) on Mar 12, 2022 at 22:08 UTC | |
Re: Mapping list to hash is dropping list items
by eyepopslikeamosquito (Archbishop) on Mar 13, 2022 at 11:18 UTC | |
by almsdealer (Acolyte) on Mar 13, 2022 at 13:52 UTC | |
Re: Mapping list to hash is dropping list items
by jwkrahn (Abbot) on Mar 12, 2022 at 23:27 UTC | |
by almsdealer (Acolyte) on Mar 16, 2022 at 16:28 UTC | |
by Fletch (Bishop) on Mar 16, 2022 at 17:14 UTC | |
Re: Mapping list to hash is dropping list items
by Marshall (Canon) on Mar 12, 2022 at 22:28 UTC | |
by eyepopslikeamosquito (Archbishop) on Mar 13, 2022 at 12:06 UTC |
Back to
Seekers of Perl Wisdom