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";
In reply to Mapping list to hash is dropping list items by almsdealer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |