# Thread: Counting columns in an array?

1. Registered User
Join Date
Oct 2003
Location
Ireland
Posts
9

## Unanswered: Counting columns in an array?

Hi there,
I have a file of concatenated matrices (see below) and for each matrix within the file i need to find the number of columns excluding the first column (A, B, C etc)? Has anyone got ideas as to how to tackle this? I have tried selecting the first line of each matrix and counting across the row but i haven't succeeded.

Matrix1
A 0 1 0 0
B 1 1 0 0
C 1 0 1 1
D 0 1 1 1

Matrix2
A 1 0 0
B 1 1 0
C 0 0 0

etc...

As you can see, not all the matrices are the same size. Any help greatly appreciated.
Rho

2. Registered User
Join Date
Jan 2004
Location
Germany
Posts
167
How are the matrices seperated? Is there one blank line between the matrices?

try this (it's untested)
Code:
```my \$file = '/path/to/matrix_file';
\$/ = "\n\n";
open(R_MATRICES,"<\$file") or die \$!;
my @matrices = <R_MATRICES>;
close R_MATRICES;

foreach my \$matrix(@matrices){
my \$columns = 0;
my @m_lines = split(/\n/,\$matrix);
foreach my \$line (@m_lines){
if(\$line =~ /^A/){
\$columns = (scalar(split(/\s/,\$line)) - 1);
}
}
print \$matrix,"\nColumns: ",\$columns;
}```

3. Registered User
Join Date
Oct 2003
Location
Ireland
Posts
9

## Thanks i'll try that

Hi reneeb,
Yes there are blank lines separating the matrices in the file. Thanks for your code. I'll try it and let you know if it works.
Thanks again,
Rho

4. Registered User
Join Date
Oct 2003
Location
Ireland
Posts
9

## It worked!!

Thanks a million. The piece of code worked!
Rho

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•