get variable name from txt file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddsf
    New Member
    • Apr 2010
    • 1

    #1

    get variable name from txt file

    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
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    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
    Code:
    open (my $file_handle. '<', $file_to_read) || die "Can't open file: $file_to_read, $!\n";
    Read the file line by line.
    Code:
    while(<$file_handle>)
    Get rid of the newline endings.
    chomp

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

    Then do the rest of your stuff next.

    --Kevin
    Last edited by eWish; Apr 4 '10, 03:22 AM. Reason: Fixed Code Error

    Comment

    Working...