-1

I have two arrays in perl that I need to merge into one, based on their first 3rd column value, as described below;

Array1:

0 1 3 some text

0 1 6 more text

1 2 0 and more

Array2:

0 1 3 where missing on page2

1 2 0 to do with it

0 1 6 to read on tuesday

And I want to achieve:

0 1 3 some text where missing on page2

0 1 6 more text to read on tuesday

1 2 0 and more to do with it

Can you please help and provide some explanation where necessary?

RobC
  • 22,977
  • 20
  • 73
  • 80
Danyel
  • 3
  • 3
  • 2
    Can you provide your data as actual perl data structures? Are those supposed to be arrays of strings, arrays of array refs, etc.? – Shawn Oct 16 '19 at 08:49
  • [DBD::CSV](http://p3rl.org/DBD::CSV) is the answer. Example: https://stackoverflow.com/a/51271732/46395 – daxim Oct 16 '19 at 08:50
  • 1
    What have you tried? What problems are you having? Please show us your code. – Dave Cross Oct 16 '19 at 13:49

1 Answers1

1

Welcome to Stack Overflow. You should post what you have tried so far :) then people will understand and help/solve your problem.

If I understood your problem, Let's consider you are having array of array like below. We have to compare both array till the specified index value. So I used the variable name called $matchedColums. Then we have to make nested loop to compare both array values.

So what I did was, I got the values from the given index and comparing with both values, and pushing into the another array. Then you can get the new array..

use warnings;
use strict;
use Data::Dumper;

my $array =  [["0","1","3","some text"], ["0","1","6","more text"], ["1","2","0","and more"]];

my $array1 =  [["0","1","3","where missing on page2"], ["0","1","6","to do with it"], ["1","2","0","to read on tuesday"]];

my $matchedColums = 3;
my $mc = $matchedColums-1;


my @finalArray ;

for my $a1 (@{$array}){ #Iterating loop for array elements.

    my $matchA1 = join ( "" , @{$a1}[0..$mc] );    # joining the elements from the given index
    my @a2String;

    for my $a2 (@{$array1}){
        my $matchA2 = join ( "" , @{$a2}[0..$mc] );    
        if ($matchA1 == $matchA2) { 
            @a2String = @{$a2}[$mc+1..$#$a2];
            last;
        }
    }

    my @fn = (@{$a1}, @a2String);
    push @finalArray, \@fn; #pushing it as an array reference.

}

print Dumper \@finalArray;
mkHun
  • 5,891
  • 8
  • 38
  • 85
  • 1
    Straightforward!! Thank you @mkHun! – Danyel Oct 17 '19 at 11:44
  • Morning All; Thank you and sorry for late reply_ To Shawn : They were array of strings._ To Daxim : DBD::CSV, I will be looking at it._ To Dave Cross : I directly query the data from two tables in mysql, and _inner join_ them _on_ the first 3 columns where their value are use to match the corresponding rows. I was looking for a way to do this with perl because the data were populated from a perl script and inserted into two different tables in mysql where we joined queries to match the corresponding rows. (I am doing a one reply, sorry). – Danyel Oct 17 '19 at 11:46