Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with array

Status
Not open for further replies.

DNAmatics

Technical User
Apr 27, 2002
3
US
Hello;

I am very new with Perl. I am writing a program to read a text file, containing float numbers (96 numbbers arranged into 8 rows, 12 numbers each row) I am able to read and store the file into an array, but its seems like the lenght of array is 8 instead of 96,which I'd like it to be.

Please tell me how to read the file one char at a time.

Any help much appreciated.

Chinh
 
I'm sort of confused. If they're float numbers, wouldn't each number have to be more than one character? Anyway, since I don't know how the numbers are delimited, I'll do it character by character.
Code:
open(FILE,&quot;<file.txt&quot;); # Open the file
while (<FILE>) { # Loop through the file
  push @Numbers, split(//); # Split each line char by char and push the results onto the array @Numbers
}
close FILE; # Close the file
print &quot;The numbers are: @Numbers&quot;;


 
Kerl,
I am sorry for the confusion. The numbers are delimited by tab. I am so new at Perl I didn't even know. Iam a Biochemist guy attempting computer programming.

The text file is the OD absorbance (for DNA) of a 96 well plate. what I meant was how to split the array into each individual number.

Thanks for your help Kerl
Chinh
 
Try this out DNA -

# begin code:

# begin anonymous scoping block

{

my $counter;
#declare a local variable

open(FILE, 'path/to/file');
#just opens the file

#iterates over each line, storing
# the line of text in $line. Note: beginning a new
# innter scope

while($line = <FILE>){


# split that $line on the tabs, store in array

my @line_of_numbers = split(/\t/, $line);

# store that local array in two dimensional array
# which is a package global named @master_array

@{$master_array[$counter]} = @line_of_numbers;

# increment our counter, which we use to keep track of
# where we are in the master array

$counter++;

}
# end while loop
close(FILE);
}
# close anonymous scoping block

# now to access that data:
# first line, 3rd datum:


print &quot;first line, 3rd datum: &quot;, $master_array[0][2], &quot;\n&quot;;

# Or, print all the data:


foreach $line_of_data(@master_array){
foreach $datum(@{$line_of_data}){
print $datum, &quot; :: &quot;;
}
print &quot;\n&quot;;
}


Hope this helps you figure out how to make the perfect perl programmer... ;)

--Jim
 
Jim's code will work great if you want a 2D array with rows and columns. If you just want a 96-element array, here's the code:

Code:
my @Numbers = ();
open(FILE,&quot;<file.txt&quot;); # Open the file
while (<FILE>) { # Loop through the file
  push @Numbers, split(/\t/,chomp($_)); #split $_ by tabs and add to @Numbers
close FILE; # Close the file
print &quot;The numbers are: @Numbers&quot;;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top