bigip2000 has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that runs a do statement as follows:

my @list = (@lines); foreach (@lines) { my @fields = split /,/; if ( $fields[2] eq '1' || $fields[2] eq '0' ) { @arr1 = do {"@fields[4]\n"}; my @out = uniq @arr1; print "this is @out"; } }

Even though I am using use List::MoreUtils qw(uniq); I never get the duplicates removed. What I get from the output of the @arr1 is

this is out XY-CAR-SHOP1_RADIATOR-PARTS this is out XY-CAR-SHOP1_RADIATOR-PARTS this is out QZ-CAR-STORE2-PRACTICAL_SNOWTIRE-02-X3-PARTS this is out QZ-CAR-STORE2-PRACTICAL_SNOWTIRE-02-X3-PARTS this is out QZ-CAR-STORE2-PRACTICAL_SNOWTIRE-02-X3-PARTS this is out QZ-CAR-STORE2-PRACTICAL_SNOWTIRE-02-X3-PARTS this is out QZ-CAR-STORE2-PRACTICAL_SNOWTIRE-02-X3-PARTS this is out QZ-CAR-STORE2-PRACTICAL_SNOWTIRE-02-X3-PARTS

Why don't the duplicates get removed? It should look like this: XY-CAR-SHOP1_RADIATOR-PARTS QZ-CAR-STORE2-PRACTICAL_SNOWTIRE-02-X3-PARTS I have tried also the following:

my @list = (@lines); foreach (@lines) { my @fields = split /,/; if ( $fields[2] eq '1' || $fields[2] eq '0' ) { @arr1 = do {"@fields[4]\n"}; } } my @out = uniq @arr1; print "this is @out";

But that only causes one line to print out, the last one:

QZ-CAR-STORE2-PRACTICAL_SNOWTIRE-02-X3-PARTS
and misses

XY-CAR-SHOP1_RADIATOR-PARTS
can anyone help?

Replies are listed 'Best First'.
Re: removing duplicates from do script
by 1nickt (Canon) on Feb 28, 2017 at 17:09 UTC

    Because in the first case you are still inside the loop when you print the array @out, and in the second case you reassign to the array @arr1 each time through the loop.

    Update: try this (untested):

    my @wanted; for my $line ( @lines ) { my @fields = split /,/, $line; if ( $fields[2] eq '1' || $fields[2] eq '0' ) { push @wanted, $fields[4]; } } @wanted = uniq @wanted; print "$_\n" for @wanted;

    Hope this helps!


    The way forward always starts with a minimal test.

      That's it!!!! worked like a real charm. I tried a similar method but didn't eliminate the "do" from the script, that is why it didn't work. THANK YOU!!!

Re: removing duplicates from do script
by thanos1983 (Parson) on Feb 28, 2017 at 17:14 UTC

    Hello bigip2000,

    Try something like this:

    I did not test your code as it is incomplete.

    my @list = (@lines); $array_element = 0; foreach my $line (@lines) { my @fields = split /,/ $line; if ( $fields[2] eq '1' || $fields[2] eq '0' ) { #@arr1 = do {"@fields[4]\n"}; splice @arr1, arr1[$array_element], 1; } $array_element++; } my @out = uniq @arr1; print "this is @out";

    Read more about it here: (How do I completely remove an element from an array?)

    Update: I got confused I thought you want to remove the whole line. The correct solution is what 1nickt proposed.

    Seeking for Perl wisdom...on the process of learning...not there...yet!