By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

get variable name from txt file

P: 1
Hello, I just a new Perl user. I have a question regarding the variables handleing.

I have a tab dlm text file. the format of the file looks like below: (it may contants about more than 1000 columns in the file)

ID name ...... age ........
1 a ......18 .....
2 b ...... 26 .....

I need extract the values for variable ID and age, then generate a new txt file with the two variables' information only. The variable age may locate at any column. So, how should I do?

Thanks a lot

dd sf
Apr 2 '10 #1
Share this Question
Share on Google+
1 Reply


eWish
Expert 100+
P: 971
I am hoping that you at least tried to complete this task yourself first. So, if you post your code we might be able to help further.

Some stuff to read about in the meantime that might help you as well.

Make use of these two pragmas.
strict
warnings

Open the file for reading.
open
Expand|Select|Wrap|Line Numbers
  1. open (my $file_handle. '<', $file_to_read) || die "Can't open file: $file_to_read, $!\n";
Read the file line by line.
Expand|Select|Wrap|Line Numbers
  1. while(<$file_handle>)
Get rid of the newline endings.
chomp

Split on the delimiter then only get the columns you want.
split
slices
Expand|Select|Wrap|Line Numbers
  1. my ($id, $name, $age) = (split/\\t/, $_)[0,1,2];

Then do the rest of your stuff next.

--Kevin
Apr 3 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.