Reading fields from the text file.

I need some help in getting all the fields without hardcording the count of the columns.

I have a text file which has 4 lines. Each line has 64 fields. I want to read each field and print it out.

I am hardcoding the count to be 256. Is there a way I can do it without hardcording the column count?

use strict;
use warnings;
open(FILE,$datafile) or die "No, can't open the file";
my $string;

while (<FILE>){
      $string .= $_;

my @array = split(/\|/, $string);

for (my $i=0; $i <= 256; $i++) {

print "$array[$i]\n";

if ($array[$i] eq "") {
      print "No Value\n";
}#while loop
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

print $_||"No Value","\n" for @array ;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Why are you concatenating file content into $string? That wastes memory.

 If you want to get the the four lines to be seen as one, you can keep count and check of it is modulus 4 at which point push(@array,$string) while $string is reset to''

it sounds as though you need a two dimensional array.

Could you post a sample of the data at least 5 lines at most 9.
sduser1Author Commented:
Thanks for the replies.  I am attaching the sample file.  One more thing.  The first line in the file is the header. The rest of the 4 lines are the data for the header.

I want to print the header along with the array contents.  The below line of code worked to display the contents of the array without hardcoding. Thank you.

print $_||"No Value","\n" for @array ;
$array[0] will contain the header fields.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.