http://www.perlmonks.org?node_id=11128122

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

I was not able to compare 2 sepearate columns exists in 2 separate sheets, need your help to identify the mistake in my code

for my $worksheet1 ( $workbook1->worksheets() ) { $worksheet->write(0, 0, 'AD Match' , $my_format); $worksheet->write(0, 1, 'APP STATUS' , + $my_format); $worksheet->write(0, 2, 'APP LABEL' , +$my_format); my $appssheet = $worksheet1->get_name(); print "APP Sheet name: $appssheet . \n"; my ( $row_min, $row_max ) = $worksheet1->row_range(); my ( $col_min, $col_max ) = $worksheet1->col_range(); for $row ( $row_min .. $row_max ) { for my $col ( $col_min = 0) { if ( $appssheet eq $appsshhetname ) { my $cell = $worksheet1->get_cell( $row, 0 ); next unless $cell; $cellA = $cell->value(); #$cellA =~ s/@(.*)//g; $cellA =~ s/@(AD.ABC.COM)//g; if ( $cellA=~ /(\d{9})/ ) { push @cellA, $cellA; #print "DUMP: @cellA"; } } #print "$cellA \n"; } } %params = map { $_ => 1 } @cellA; print Dumper %params; if ( $appssheet eq $ADdata ) { for my $row1 ( $row_min .. $row_max ) { for my $col1 ( $col_min = 0) { my $cellB = $worksheet1->get_cell( $row1, 0 ); next unless $cellB; $cellC = $cellB->value(); #$cellC =~ s/@(AD.ABC.COM)//g; print "2nd Entry :$cellC\n"; #push @cellC , $cellC; if(exists($params{$cellC})) { $worksheet->write($r, 0, $cellC); } } } $r += 1; }
  • Comment on Unable to compare 2 arrays from 2 separate columns of separate sheets.
  • Download Code

Replies are listed 'Best First'.
Re: Unable to compare 2 arrays from 2 separate columns of separate sheets.
by GrandFather (Saint) on Feb 09, 2021 at 04:03 UTC
    "identify the mistake in my code"

    There are a multitude. For a start there are a bunch of variables that seem to be global to the code you give, which is (kinda) OK, but including something to show that those variables are supposed to be global to the outer for loop would be smart ($workbook1, $worksheet, @cellA, ...).

    Some variables are clearly not declared correctly ($cellA, $row, ...). $r isn't declared and it's scope isn't obvious.

    Your indentation is all over the place. You need to turn most of your if statements inside out:

    for my $col ($col_min = 0) { if ($appssheet eq $appsshhetname) { my $cell = $worksheet1->get_cell($row, 0); ... } }

    should be:

    for my $col ($col_min = 0) { next if $appssheet ne $appsshhetname; my $cell = $worksheet1->get_cell($row, 0); ... }

    Because of all the above issues (except maybe the if statements stuff) it isn't possible to check the logic of your code with any certainty. Perhaps you need to turn on strictures and tighten up the scope of your variables as a first step?

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: Unable to compare 2 arrays from 2 separate columns of separate sheets.
by choroba (Cardinal) on Feb 09, 2021 at 09:11 UTC
    You seem to be using a spreadsheet module. Which one is it? Please, try to provide a SSCCE so we can run your code and experiment with the fixes.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Thanks! Can you please check my code snippet and let me know the exact issue while updating unique values after comparison or where i am doing wrong here?

      #!/usr/bin/env perl use strict; no warnings; use Spreadsheet::XLSX; use Spreadsheet::ParseXLSX; use Excel::Writer::XLSX; my $appsshhetname = "syslog_-prod"; my $ADdata = "AD-Data"; my $no = "NOt exist"; my $cellA; my $cellB; my $cellC; our @cellA; our @cellC; our @redi; my $responseid; my $row; my %params; my $workbook = Excel::Writer::XLSX->new( 'C:/PERL/SYNC-OKTA-User-Prof +ile-Apps-Analysis-Details.xlsx' ); my $worksheet = $workbook->add_worksheet(); my $r = 1; my $my_format = $workbook->add_format( bold => 1, color => 'blue', size => 18, ); my $ +parser = Spreadsheet::ParseXLSX->new(); my $workbook1 = $parser->parse('C:/Users/Documents/Work.xl +sx'); if ( !defined $workbook1 ) { die $parser->error(), ".\n"; } for my $worksheet1 ( $workbook1->worksheets() ) { $worksheet->write(0, 0, 'AD Match' , $my_format); $worksheet->write(0, 2, 'AD MATCH' , $ +Synchrony_format); $worksheet->write(0, 3, 'Creation Date +' , $Synchrony_format); my $appssheet = $worksheet1->get_name( +); for $row ( $row_min .. $row_max ) { for my $col ( $col_min = 0) { if ( $appssheet eq $appsshhetname ) { my $cell = $worksheet1->get_cell( $row, 1 ); next unless $cell; $cellA = $cell->value(); #$cellA =~ s/@(.*)//g; $cellA =~ s/@(AD.ABC.COM)//g; if ( $cellA=~ /(\d{9})/ ) { push @cellA, $cellA; #print "DUMP: @cellA"; } } #print "$cellA \n"; %params = map { $_ => 1 } @cellA; #print Dumper %params; my @uniq = keys %params; if ( $appssheet eq $ADdata ) { for my $row1 ( $row_min .. $row_max ) { for my $col1 ( $col_min = 1) { my $cellB = $worksheet1->get_cell( $row, 1 ); next unless $cellB; $cellC = $cellB->value(); #$cellC =~ s/@(AD.ABC.COM)//g; #print "2nd Entry :$cellC\n"; #push @cellC , $cellC; if(exists($params{$cellC})) { $worksheet->write($r, 0, $cellC); } else { #$worksheet->write($r, 0, $no); } } } $r += 1; }
        You should fix the indentation. Also, SSCCE should compile, but yours doesn't, as it contains several undeclared variables. I was able to fix it, I also created an input XLSX document with two sheets, one of them called AD-Data containing
        1 123456789 2 123456790 3 123456791 4 123456792 5 123456793 6 123456794 7 123456795 8 123456796
        and another called syslog_-prod containing
        1 2 6 123456789 11 123456790 16 123456791 21 123456792 26 123456793 31 123456794 36 123456795 41 123456796
        Running the edited code

        I got no errors nor warnings and the output file was created with a coloured header and the following contents in the first column:

        123456790 123456791 123456792 123456793
        but formatted as 1.23E+08.

        Sorry, what was your question again?

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]