1.11M Members

two 2D Array handling

 
0
 

Hi Experts,
Please help me, i am writing code for csv conversion, in middle i facing one issue. i have one array which contains 3 elements like wise
@array contanis:
var1= '455,0,0,0,0\n
135,0,0,0,0\n
199,0,0,0,0\n',

var2= '131.253.4.131,1,0\n
131.253.7.54,1,0\n
10.80.5.3,1,0\n';

i need to convert like wise as below
var1= '455,0,0,0,0 , 131.253.4.131,1,0\n
135,0,0,0,0, 131.253.7.54,1,0\n
199,0,0,0,0', 10.80.5.3,1,0\n';

Thanks in Advance,
Senthil. V

 
0
 
#!/usr/bin/perl
use strict;
use warnings;

my (@array, $foundvar1, $foundvar2);

while (<DATA>){
    chomp;
    if (m/Var1 data:/){
        $foundvar1 = 1;
    }
    elsif (m/Var2 data:/){
        $foundvar1 = 0;
        $foundvar2 = 1;
    }
    else {
        if ($foundvar1){
            my @rec = split /,/, $_;
            push @{$array[0]}, join ',', @rec;
        }
        if ($foundvar2){
            my @rec = split /,/, $_;
            push @{$array[1]}, join ',', @rec[0..2];
        }
    }
}

foreach(0 ..  $#{$array[0]}){
    print "${$array[0]}[$_], ${$array[1]}[$_]\n";
}
__DATA__
Var1 data:
455,0,0,0,0
135,0,0,0,0
199,0,0,0,0
Var2 data:
131.253.4.131,1,0,,
131.253.7.54,1,0,,
10.80.5.3,1,0,,

Outputs the following:

455,0,0,0,0, 131.253.4.131,1,0
135,0,0,0,0, 131.253.7.54,1,0
199,0,0,0,0, 10.80.5.3,1,0
 
0
 

Great Thanks, D5E. it working fine here

 
0
 

Great Thanks, D5E. it working fine here

You're welcome. Please don't forget to mark this thread solved.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article