We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,437 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

two 2D Array handling

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

2
Contributors
3
Replies
1 Week
Discussion Span
1 Year Ago
Last Updated
4
Views
senthilamp4
Newbie Poster
14 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1

Great Thanks, D5E. it working fine here

senthilamp4
Newbie Poster
14 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Great Thanks, D5E. it working fine here

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

d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0658 seconds using 2.67MB