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

Monks, I am a newbie so please try to be patient with me. I am comparing two html tables in which one has a list of all the users and another has a list of users that completed a task. My goal is to create one list of users that have not completed the task. My code works, however I'm sure there is a much cleaner way of doing it. I appreaciate any suggestions. Thanks
#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Request::Common; use HTML::TableExtract; use URI; my $cookie = "cookie.txt"; my $ua = LWP::UserAgent->new; $ua->cookie_jar(HTTP::Cookies->new(file => $cookie , autosave => 1)); my $list = $ua->simple_request(POST '$foo', [ username=>'username', password=>'password', action => 'Login', ]); my $u = $list->header('location') or die "missing location: ", $list-> +as_string; $u = URI->new_abs( $u , $list->base ); my $list2 = $ua->request(GET "$foo2"); $list = $ua->request(GET $u); my %employee; my ($email, $host, $name); #creates hash of entire group my $te = new HTML::TableExtract(headers => [qw(Name Email)]); $te->parse($list->as_string); foreach my $ts ($te->table_states) { foreach my $row ($ts->rows) { foreach my $col (@$row){ chomp($col); $col =~ s/.//; $col =~ s/\s+?$//; if ($col =~ /@\S+/){ $col =~ s/,/./g; $email = $col; if ($email =~ /@(\S+)/){ $host = $1;}} elsif($col =~ /(\S+),\s+(\S+)/){ $name = $2 . " " . $1;} else{ $email="dublicate"; $host= "dublicate";}} $employee{$email}="$host##$name#$email";}} #array of people completed task my @finish; $te = new HTML::TableExtract(headers =>[qw(Email)]); $te->parse($list2->as_string); foreach my $ts ($te->table_states){ foreach my $row ($ts->rows){ foreach my $col (@$row){ push @finish, $col;}}} #deletes all those that completed module8 from hash foreach my $n (@finish){ foreach my $emp (keys %employee){ if ($n eq $emp){ delete $employee{$emp}; last;}}} open (LIST, ">IncompleteTask.txt"); foreach my $emp(keys %employee){ print LIST "$employee{$emp}\n";}

Replies are listed 'Best First'.
Re: comparing two tables
by Hofmator (Curate) on Jan 31, 2003 at 21:03 UTC
    Some suggestions:
    • The beginning looks fine, no comments there.
    • 'create hash of entire group' looks like it has a bit too complicated logic in the ifs in the innermost loop but the indentation is broken and I don't know the format your data is in, so it's hard to give advise there.
    • Don't put the finished people into an array. Instead directly manipulate the hash. Just set the corresponding value to undef - instead of the push you then have $employee{$col} = undef;
    • This obliviates the need for the delete loops.
    • Instead you just use the following to print out the list:print LIST $_, "\n" for grep defined, values %employee;i.e. print to the filehandle LIST only the defined values of the hash %employee.

    -- Hofmator