problem with a foreach loop

I have the following  code.  After the first foreach loop, I have the data I want in @temp.  I've printed it, there are just a lot of dupes.  I can't figure out why my second foreach loop isn't getting rid of the dupes. The regex isn't matching $var and I don't get anything on the print.

#!/usr/bin/perl

@vxdisk = `vxdisk -o alldgs list`;
foreach ( @vxdisk ) {
        if ( m{.*\d+\s+(\S+)\s+.*} ) {
                if ( $1 =~ m{auto} ) {
                        next;
                }
                push ( @temp, "\n$1" );
        }
}

# Get rid of the dupes
foreach $var ( @temp ) {
        if ( !grep( m{$var}, @temp ) ) {
                print "$var\n";
                # push ( @dg_list, "$var" );
        }
}

#print "@dg_list\n";
LVL 1
David AldridgeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David AldridgeAuthor Commented:
Actually, I see why the print command wouldn't work, but the push I have commented out doesn't work either. Nothing gets pushed to @dg_list.
0
David AldridgeAuthor Commented:
Ok, I figured out I was using the wrong array on the grep, but I'm still getting dupes.
0
David AldridgeAuthor Commented:
Got it!  

#!/usr/bin/perl

@vxdisk = `vxdisk -o alldgs list`;
foreach ( @vxdisk ) {
        if ( m{.*\d+\s+(\S+)\s+.*} ) {
                if ( $1 =~ m{auto} ) {
                        next;
                }
                push ( @temp, "\n$1" );
        }
}

# Get rid of the dupes
foreach $var ( @temp ) {
        if ( !grep( m{$var}, @dg_list ) ) {
                # print "$var\n";
                push ( @dg_list, "$var" );
        }
}

print "@dg_list\n";
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

David AldridgeAuthor Commented:
If you see a better way to do this, please post it.  I'll leave it up for a while.
0
ozoCommented:
push ( @dg_list, "$var" ); is commented out, so it will never execute

If there are no meta characters in $var, grep( m{$var}, @temp ) will always succeed

For a better way, you may want to look at
perldoc -q "How can I remove duplicate elements from a list or array"
0
David AldridgeAuthor Commented:
I uncommented it on the second piece of code.  It's working now, but if you can see a better way to do it, I'll take it.
0
David AldridgeAuthor Commented:
It also gives me a blank line at the top which I would prefer to get rid of when I print @dg_list. I know it's from the push ( @temp, "\n$1" ), but can't figure out how to get rid of it.
0
ozoCommented:
a better way from the perldoc faq
my %seen=();
 @dg_list = grep !$seem{$_}++ @temp;

or, if order does not matter
my %seen;
@seen{@temp}=();
print for keys %seen;
0
ozoCommented:
why not
 push @temp,  $1;

and
print "$_\n" for @temp;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David AldridgeAuthor Commented:
This is what I get from the first one and the code is right below it.

testbox-[root]/home/share/daldrid/work/Perl/sapwork> vxdisk_chk
Array found where operator expected at vxdisk_chk line 14, near "++ "
        (Missing operator before  ?)
syntax error at vxdisk_chk line 14, near "++ @temp"
Execution of vxdisk_chk aborted due to compilation errors.


my %seen=();
@dg_list = grep !$seen{$_}++ @temp=();
print "@dg_list\n";
exit;
0
ozoCommented:
@dg_list = grep !$seen{$_}++, @temp;
0
David AldridgeAuthor Commented:
I don't get an error with it now, but @dg_list get nothing in it.

Your other suggestion for getting rid of the blank at the top is great.
0
David AldridgeAuthor Commented:
This worked.

%hash = map {$_, 1 } @temp;
print "$_\n" for keys %hash;


Thanks ozo.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.