storing values from file into an array.-newbie

G

Go Perl

I am having difficulties accessing these values outside this while
loop. I am able to get only the last elements of the array outside.
what should i do to store each of these elements c_array[1],...in an
array and how to access them later ?

while ($c_line=<CONTROL_FILE>) {
chop($c_line);
@c_array = split(/\t/, $c_line);
if ($c_array[4] != "flag" && $c_array[4] == 1) {
$bit_length = blength($c_array[3],$c_array[2]);
$number_Par = $number_Par + 1;
$BIT = $BIT + $bit_length;
printf OUT_CONTROL ("%d\t%d\t%d\t%d\n", $c_array[1], $c_array[2],
$c_array[3], $bit_length);

}

}
 
B

Bob Walton

Go said:
I am having difficulties accessing these values outside this while
loop. I am able to get only the last elements of the array outside.
what should i do to store each of these elements c_array[1],...in an
array and how to access them later ?

while ($c_line=<CONTROL_FILE>) {
chop($c_line);
@c_array = split(/\t/, $c_line);
if ($c_array[4] != "flag" && $c_array[4] == 1) {
$bit_length = blength($c_array[3],$c_array[2]);
$number_Par = $number_Par + 1;
$BIT = $BIT + $bit_length;
printf OUT_CONTROL ("%d\t%d\t%d\t%d\n", $c_array[1], $c_array[2],
$c_array[3], $bit_length);

}

}

Well, every time through the loop you are reassigning @c_array, so of
course only the last line's worth is in it when you are done. If you
want to save that array in another array, add something like:

push @output_array,[@c_array];

in the vicinity of your printf statement.

And BTW, you have an improper use of a relational operator.

use warnings;

would have told you that. Always always always use warnings; and use
strict; during development! Something like:

use warnings;
use strict;
my $c_line;
my @c_array;
my $bit_length;
my $number_Par;
my $BIT;
my @output_array;
while ($c_line=<DATA>) {
chop($c_line);
@c_array = split(/\t/, $c_line);
if ($c_array[4] != "flag" && $c_array[4] == 1) {
$bit_length = blength($c_array[3],$c_array[2]);
$number_Par = $number_Par + 1;
$BIT = $BIT + $bit_length;
printf ("%d\t%d\t%d\t%d\n", $c_array[1], $c_array[2],
$c_array[3], $bit_length);
push @output_array,[@c_array];
}
}
use Data::Dumper;
print Dumper(\@output_array);
sub blength{
return 42; #or something
}
__END__
9
2
3
2
1
1
2
3
4
1
3
4
5
6
1
 
T

Tad McClellan

Go Perl said:
I am having difficulties accessing these values outside this while
loop. I am able to get only the last elements of the array outside.


That's because you overwrite the old values each time through
the loop.

what should i do to store each of these elements c_array[1],...in an
array and how to access them later ?


Don't overwrite the old values each time through the loop. :)

while ($c_line=<CONTROL_FILE>) {
chop($c_line);


That's how it was done over 7 years ago.

Where are you learning your Perl from?

chomp($c_line);

@c_array = split(/\t/, $c_line);


Assignment overwrites whatever used to be there.

You haven't said what exactly you want in @c_array.

If you want _all_ of the values in @c_array:

push @c_array, split(/\t/, $c_line);

If you want a LoL in @c_array:

push @c_array, [ split(/\t/, $c_line) ];

if ($c_array[4] != "flag" && $c_array[4] == 1) {
^^
^^

You should always enable warnings when developing Perl code!

You are using the wrong operator there.

$number_Par = $number_Par + 1;

$number_Par++;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top