in reply to Removing doubles and printing only unique values

Hi zarath. $seen{$fields[1]} is always going to be false unless you set it to true. One possible way to do what you want is:

(undef, my $vk) = split ";" , $line; # define input-lines push (@vk,$vk) unless $seen{$vk}++;
$seen{$vk}++ will first return the current value (ie 0 the first time), and then increment it by one, so the value will be true from that point. By the way, I also changed your split, you can affect the output of split directly to a list, with undef in the place of elements you want to ignore. And split will be optimized to stop splitting after the list is full (eg: here you only have two elements on the left, so it will only split twice).

Replies are listed 'Best First'.
Re^2: Removing doubles and printing only unique values
by zarath (Beadle) on Oct 31, 2017 at 13:15 UTC

    Hi,

    Thank you sooo much! This does exactly what I need it to do.

    At first I did not understand at all how it would 'know' which field it needs to extract, but I guess it is because my $vk is the second field in (undef, my $vk)? So if I would need the third field, this part of the code would be (undef, undef, my $vk)? Is that right?

      Yes! And you can of course fetch several values with: my (undef, $second, $third) = split .... my (undef, $var, $other) is the same as (undef, my $var, my $other)

      Note that haukex is right about Text::CSV though, as long as your input stays simple (ie: no field contains the ; character, or a newline) split will do the work, but if there's any chance that your data can be more complex, it's just either to use the module rather than try to handle all the special cases by hand.