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

Hey Monks
I have run in to an brick wall with the problem below. I have a subroutine which input the array below and I need to orgaise it into a SQL form. (SQL knowledge is not necessary to annswer this question so please read on!) Any of you familiar with SQL will see I am trying to prevent duplicate results being printed. I have an array (@array2) which contains the columns which would be duplicated so what I want to do is remove duplicate occurances of them from the full statement (stored in @array1) and create a new array (@result) which I can then pass on to the DBI.

INPUT: ------ @array1 = ("tmp0.size", "tmp0.buffersize", "tmp0.value", "tmp1.size", +"tmp1.buffersize", "tmp1.value"); @array2 = ("size", "buffersize"); OUTPUT: ------- @result = ("tmp0.size", "tmp0.buffersize", "tmp0.value", "tmp1.value") +;

Have two results and I need to remove the values in @array2 that are partially matched in @array1. But I need to leave the first occurances. Need it to work for any number of values in both.

I also regret to inform everyone that pending an answer for this question it will be my last post on this site, maybe for ever, because I am finishing up my work experience (this is my last obsticle) and then I'm going back to college and java world.Have to say this site has been such a help. Easily the best resource or community on the net for any problems I have. Thanks and I hope to return in the future as I have been impressed by Perl.

But I'm not finished yet so any help on the above problem is appreciated. This maybe a fairly easy problem to work out but it's just not coming to me and I have only a day to get over it and finish my program. (I hate deadlines!) So please be patient. Thanks in advance.

j o h n i r l .

 Sum day soon I'Il lern how 2 spelI (nad tYpe)

Replies are listed 'Best First'.
Re: Partial Array Comparision
by BrowserUk (Patriarch) on Sep 24, 2002 at 15:46 UTC

    Something like this?

    #! perl -sw use strict; my @array1 = ("tmp0.size", "tmp0.buffersize", "tmp0.value", "tmp1.size +", "tmp1.buffersize", "tmp1.value"); my @array2 = ("size", "buffersize"); my %seen = map { $_ => 0 } @array2; my @results = grep { /.+\.(\w+)/; my $s = $1; !exists $seen{$s} or ($seen{$s}++ < 1); } @array1; local $" = $/; print "@results\n"; __DATA__ C:\test>200392 tmp0.size tmp0.buffersize tmp0.value tmp1.value C:\test>

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: Partial Array Comparision
by jmcnamara (Monsignor) on Sep 24, 2002 at 16:03 UTC

    Here is one possible method:
    #!/usr/bin/perl -wl my @array1 = ("tmp0.size", "tmp0.buffersize", "tmp0.value", "tmp1.size", "tmp1.buffersize", "tmp1.value"); my @array2 = ("size", "buffersize"); my @result = @array1; foreach my $pattern (@array2) { my $seen = 0; my @tmp; foreach (@result) { push @tmp, $_ if /\.$pattern$/ and not $seen++; push @tmp, $_ if not /\.$pattern$/; } @result = @tmp; } print "@result";

    --
    John.

Re: Partial Array Comparision
by sauoq (Abbot) on Sep 24, 2002 at 16:12 UTC
    #!perl -w @array1 =("tmp0.size", "tmp0.buffersize", "tmp0.value", "tmp1.size","tmp1.buffersize", "tmp1.value"); @array2 = ("size", "buffersize"); my %patterns = map {(qr/\.$_$/, 0)} @array2; my @result; COL: for my $col (@array1) { for my $pat (keys %patterns) { if ($col =~ /$pat/) { push @result, $col unless ($patterns{$pat}++); next COL; } } push @result, $col; } print "$_\n" for @result;
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Partial Array Comparision
by DamnDirtyApe (Curate) on Sep 24, 2002 at 19:55 UTC
    #! /usr/bin/perl use strict ; use warnings ; use Data::Dumper ; $|++ ; my @array1 = qw( tmp0.size tmp0.buffersize tmp0.value tmp1.size tmp1.buffersize tmp1.value ) ; my @array2 = qw( size buffersize ) ; my $exclusion_str = join '|', @array2 ; my @result = map { join '.', @$_ } grep { $_->[0] eq 'tmp0' || $_->[1] !~ m/\b$exclusion_str +\b/ } map { [ split /\./, $_ ] } @array1 ; print Dumper( \@result ) ; exit ;

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche