> Hi all,
>
> I am practicing using grep, but have a problem. Instead of directly
> building an array in a traditional format with grep:
>
> @array = (grep $_, @input);
>
> I want to push items into a new array that DO NOT appear within the
> current array being grepped for. Hence:
>
> use strict;
>
> my @array = ();
> my @clean = qw(one one two two two three four six five six six seven
> nine ten ten eight nine);
>
> for (@clean) {
>         if (! grep ($_, @array)) {
>                 push (@array, $_);
>                 print "$_";
>         }
> }

I got it...

for my $item (@clean) {
        if (! grep ($_ eq $item, @array)) {
                push (@array, $item);
                print "$item\n";
        }
}

I didn't clue in that in the previous, $_ was actually referring to
each element of @array, not @clean, so I added $item foreach @clean,
and then compared $_ == $item for each item grepped in @array.

I also suppose that if (grep ($_ ne $item, @array) would work too, but
I haven't tried that yet.

;o)

Steve

>
> I'd like to take each item from @clean, grep through the initally
> empty @array, and if the element from @clean does not appear in
> @array, push it to the @array.
>
> When I run the app, I get 'one' in the new @array, but it stops there.
> # perl -d program steps through, and on the first pass of the for
> statement, does the push, and the print. On subsequent runs, $_
> contains the next element from @clean, but the if statement for some
> reason is false, thereby skipping the statement block in if{}, and
> starting over.
>
> perldoc -f grep was not too much help, unless I overlooked something
> in that very short doc page.
>
> Can someone please advise on where I am going wrong?
>
> TIA,
>
> Steve
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to