Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > storing values from file into an array.-newbie

Reply
Thread Tools

storing values from file into an array.-newbie

 
 
Go Perl
Guest
Posts: n/a
 
      10-20-2003
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);

}

}
 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      10-20-2003
Go Perl wrote:

> 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:umper;
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

--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      10-20-2003
On Mon, 20 Oct 2003 15:51:02 -0700, Go Perl wrote:
> I am having difficulties accessing these values outside this while
> loop.


Have you tried declaring '@c_value' outside the loop?


--
Tore Aursand <(E-Mail Removed)>
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-20-2003
Go Perl <(E-Mail Removed)> wrote:
> 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++;


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Images: Storing in Files VS Storing in Database Jonathan Wood ASP .Net 1 06-02-2008 05:56 PM
storing pointer vs storing object toton C++ 11 10-13-2006 11:08 AM
Reading and "storing" 32 bits values Clemens VHDL 1 01-29-2005 06:37 PM
Need to concatenate all files in a dir together into one file and read the first 225 characters from each file into another file. Tony Perl Misc 5 04-19-2004 03:28 PM
storing radio button values in database - binary, int or varchar?? Hazzard ASP .Net 4 07-09-2003 12:59 PM



Advertisments