I have a text file containing contents like
1 A 0 -1 -2 -3 -4
2 A -1 2 3 -4 -5

So I read the file and used split function as @a=split(/ /,$_) and took each line into array a.
But now when I take these into array a they are like 1A0-1-2-3-4 and 2A-123-4-5.
I need to add 1st line and 2nd line ie (0+-1, -1+2, -2+3, -3+-4, -4+-5).
Tried separating array elements with comma. If that is done adding is easy. But am finding difficult.
Can u help in first separting elements with comma.

Thanx in advance

Hi,
The code below solves the problem. Take this as a guide. Test, verify and ask if you still have some other questions.

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

my $data;

while (<DATA>) {
    chomp;
    my $value = substr $_, 4;    # you can use regex instead
    push @{$data}, [ split / /, $value ];
}

if ( @{ $data->[0] } == @{ $data->[1] } ) {    # test both arrays of arrays
    for ( 0 .. $#{ $data->[0] } ) {
        printf "($data->[0][$_]+$data->[1][$_])= %d,",
          $data->[0][$_] + $data->[1][$_];
    }
}

__DATA__
1 A 0 -1 -2 -3 -4
2 A -1 2 3 -4 -5

OUTPUT

(0+-1)= -1,(-1+2)= 1,(-2+3)= 1,(-3+-4)= -7,(-4+-5)= -9,

Program is really helpful. Thanx. But I am bit confused. Actually my data is in a file and file has many rows ie file looks like
1 A 0 1 -2 -3 -4 5 6
2 B 2 1 3 -4 -4 0 -2
3 C 0 0 -1 -2 -4 1 2
4 A 0 3 5 2 1 -7 -3
5 C 0 0 -1 -1 -2 -3 2
When subsring is used its taking only values and I am not able to compare the alphabets.
I want to add rows when alphabets are same. ie. above has 1st row and 4th row (both are A's), then it should add.Similarly for C and othet alphabets.So it has to go till the end of the file and wherever alphabets are same it should go on adding.
So output file should have things like
A 0 4 3 -1 -3 -2 3
B 2 1 3 -4 -4 0 -2
C 0 0 -2 -3 -6 -2 4
Hope I am clear now. This was the reason why I read each line into an array. Hope u can give me some other soloution.

Hi,

Actually my data is in a file and file has many rows ie file looks like

Like I said above the script I gave will do. Even when you are reading from a file. All you need do is open a file and read into a filehandles.

When subsring is used its taking only values and I am not able to compare the alphabets.

Again, like I said before now, you may use REgex, then to compare and take the alphabets into consideration use a hash.

Hope u can give me some other soloution

Yes, using the data you provided.
But you MAY have to improve on it, if the rows with the same alphabets are MORE THAN TWO! Really, it's simple.
Here goes the solution:
Let say the file contain all your data is named matrix.txt

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

my $file = 'matrix.txt';
my $matrix_has_ref;

open my $fh, '<', $file or die "can't open file: $!";
while ( defined( my $line = <$fh> ) ) {
    chomp $line;
    if ( my @data = ($line) =~ m/(\d+?).(\w).(.+?)$/s ) {
        push @{ $matrix_has_ref->{ $data[1] } }, [ split / /, $data[2] ];
    }
}
close $fh or die "can't close file: $!";

my %iDisplay;

foreach my $row ( keys %{$matrix_has_ref} ) {
    undef $iDisplay{$row};
    my $row_data = $matrix_has_ref->{$row};
    if ( @{$row_data} == 2 ) {
        if ( @{ $row_data->[0] } == @{ $row_data->[1] } ) {
            for ( 0 .. $#{ $row_data->[0] } ) {
                push @{ $iDisplay{$row} },
                  $row_data->[0][$_] + $row_data->[1][$_];
            }
        }
    }
    else {
        push @{ $iDisplay{$row} }, @{$_} for @{$row_data};
    }
}

$" = " ";
print $_, ' ', "@{$iDisplay{$_}}", $/ for sort keys %iDisplay;

OUTPUT

A 0 4 3 -1 -3 -2 3
B 2 1 3 -4 -4 0 -2
C 0 0 -2 -3 -6 -2 4

Am glad you got it! Please, mark this question as solved.

Thanks

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.