Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Find array duplicates

by Win (Novice)
on Nov 11, 2003 at 15:54 UTC ( [id://306227]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

My heavenly ambitions are embedded as comments in the following code. Can anyone suggest a very tidy and short way of fulfilling these ambitions?

foreach (my @tables){ my $table_name = $_; #list of all table names ###### Hash out excluded _tables here ###### # I would like code here that only allows the following 3 variable + assignments to occur when the tables names are not in an array that +lists tables that are excluded from all schemas (this array is not sh +own here). my $Table_1 = "Table_name_A"; my $Table_2 = "Table_name_B"; my $Table_3 = "Table_name_C"; #close list of all table names if (my $table_name = $Table_1){

20031111 Edit by castaway: Changed title from 'One liner'

Replies are listed 'Best First'.
Re: Find array duplicates
by ysth (Canon) on Nov 11, 2003 at 16:19 UTC
    First of all, unless you are changing $table_name in the loop and don't want @tables to also change, try this instead: foreach my $table_name (@tables) if your version of perl supports the "foreach my $foo" syntax. I strongly doubt you want to say "my @tables", since this will refer to a new empty array, not whatever @tables means outside the loop.

    If your excluded names are keys in hash, you can just say

    unless ($excluded_table{$table_name}) { do assignments }
    If you need the excluded names in an array, try perldoc -q 'contained in a'
Re: Find array duplicates
by eric256 (Parson) on Nov 11, 2003 at 16:22 UTC

    What?

    I would say that settings $Table_1 thru three inside the foreach doesn't seem to make sense because they are the same each iteration, or are they? Are you asking to have the Table variables only set when the $table_name is not in a certain array? If that is the case do you want to skep to the next table in the @tables list?

    On second thought i have no clue what you want, i'm sure you will get the needed help if you reply with a litte more detail on exactly what you are trying to accomplish.


    ___________
    Eric Hodges
Re: Find array duplicates
by Art_XIV (Hermit) on Nov 11, 2003 at 16:38 UTC

    An executable example:

    use strict; #create the exclusion hash outside of loop #much cheaper than recreating though each pass inside of loop! my %excluded_tables = ( 'SYSTEM' => 1, 'OBJECTS' => 1 ); my @tables = qw(PEOPLE PLACES THINGS OBJECTS CONTACTS CUSTOMERS SYSTEM +); #foreach my $table_name (@tables) would be fine, too. #assigning vars the value of $_ inside of a loop is kind of funky for (@tables) { unless (exists $excluded_tables{$_}) { print "Assigning vars for $_\n"; my ($table_1, $table_2, $table_3) = ("Table_name_A", "Table_name_B", "Table_name_C"); } else { print "Skipping $_\n"; } }
    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: Find array duplicates
by Win (Novice) on Nov 11, 2003 at 16:46 UTC
    Before I loose any more reputation points. I was envisaging something like this:
    sub Check_excluded_tables_list (@) { @tables = @_; @Excluded_tables = qw/Table_A/; foreach (@tables){ $table_name = $_; if ($table_name =~ @Excluded_tables){ $table_name = ""; } else{ # do nothing } push (@list_of_accepted_tables, $table_name); } #close list of all table names if (@tables = @list_of_accepted_tables){ $status = "good"; } else{ $status = "bad"; } }

    I haven't had time to test this code yet.

      How about this?

      sub checkxtlist (@) { my @Excluded = qw(Table_A Table_B); my %Excludes = map { $_ => 1 } @Excluded; my @Accepted = grep { !$Excludes{$_} } @_; my $status = (@_ == @Accepted) ? "good" : "bad"; }
      Of course, you probably don't want to hard code the list of excluded tables in the subroutine (I don't know though, you might). And I'm not sure why you want a prototype on this subroutine either.

      Also, if you wanted to exclude by regular expression, you could do that too without much difficulty:

      sub checkxtpatlist ($@) { my $pat = shift; my @Accepted = grep { !/$pat/ } @_; return my $status = (@_ == @Accepted) ? "good" : "bad"; } my $s = checkxtpatlist(qr/Table_[ABC]/,@tables);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://306227]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found