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

Recommended Answers

All 3 Replies

#!/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

Great Thanks, D5E. it working fine here

Great Thanks, D5E. it working fine here

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.