in reply to Mapping list to hash is dropping list items

This may work better (UNTESTED):

#!/usr/bin/perl use strict; use warnings; my @allTags; { local @ARGV = <[0-9][0-9][0-9][0-9].txt>; my %uniqueTags; while ( my $line = <> ) { next unless $line =~ s/^tags\s+//; $line =~ s/\s+\z//; $uniqueTags{ $_ } = 1 for split /\s+,\s+/, $line; } @allTags = keys %uniqueTags; } print "@allTags\n";

Replies are listed 'Best First'.
Re^2: Mapping list to hash is dropping list items
by almsdealer (Acolyte) on Mar 16, 2022 at 16:28 UTC
    If I understand correctly, <> will read from @ARGV unless it is empty in which case it will read from STDIN?

    Also, why did you wrap everything between my @alTags and print in a block?

      When used like this the diamond operator is actually glob which generates a list of filenames; those filenames are used to populate @ARGV and (yes) then the plain <> readline version will implicitly read from those files. The block was (my guess) probably intended to scope the local change to @ARGV to just that section of code.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.