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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie - Parsing a file and storing data in columns

Status
Not open for further replies.

dirtyholmes

Programmer
Mar 17, 2004
43
AU
Hi Guys,

Can anyone advise. I have a text file that is basically made up of information similar to below. I want to parse through the file, and store the information in some sort of array by column. i.e I want a storage array that will hold 002425321,11242531,54243351,
and similarly in another array I want to store HOLLAND,GERMANY,CHINA etc.

Can anyone advise how I would do this.

00242531 HOLLAND 4572357225
11242531 FRANCE 6454353535
54243351 CHINA 7676689998
9842665 DENMARK 8985543327
54242313 HUNGARY 4678777979
44446663 GERMANY 3546456557

Regards

DHolmes
 
use split
Code:
open FH, "<myfile.txt" or die "$!";
my $num1; my $country; my $num2;
my @nums1;my @countries;my$nums2;
while (<FH>) {
  ($num1, $country, $num2)=split (/\s+/, $_); split on one or more spaces
  push (@nums1, $num1);
  push (@nums2, $num2);
  push (@countries, $country);
}
close FH;

Haven't tested it but it looks ok
--Paul

This isn't homework is it? The file looks similar to one a while ago
 
Paul - wow, that looks familiar. :)

Code:
my (@col1, @col2, @col3);	# First number - Country Name - Last Number
my $i = 0;

open IN, "< somefile.txt" or die $!;
while (<IN>) {
	($col1[$i], $col2[$i], $col3[$i++]) = split /\s+/;
}
close IN;

 
Thanks Guys. No its not homework . I did post something a bit similar a while back. Just trying to learn quickly thats all !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top