in reply to Tricky Problem

Also how do i find the number of lines in a file(text or any flatfile?)

I didn't see an answer to this part of the question in the replies, so here goes: (Choose the one that fits best into your program.)

sub get_num_lines_method1 { my ($file_name) = @_; my $num_lines = 0; local *FILE; open(FILE, $file_name); $num_lines++ while (<FILE>); close(FILE); return $num_lines; } sub get_num_lines_method2 { # Really same as get_num_lines_method1. my ($file_name) = @_; my $num_lines = 0; local *FILE; open(FILE, $file_name); while (<FILE>) { $num_lines++; } close(FILE); return $num_lines; } sub get_num_lines_method3 { # Loads the whole file into an array. my ($file_name) = @_; local *FILE; open(FILE, $file_name); my @file = <FILE>; close(FILE); return scalar(@file); } sub get_num_lines_method4 { # Loads the whole file into a scalar. my ($file_name) = @_; local *FILE; open(FILE, $file_name); local $/; my $file = <FILE>; close(FILE); return $file =~ tr/\n/\n/; } printf("Method 1: %d\n", get_num_lines_method1($0)); printf("Method 2: %d\n", get_num_lines_method2($0)); printf("Method 3: %d\n", get_num_lines_method3($0)); printf("Method 4: %d\n", get_num_lines_method4($0)); __END__ output: ======= Method 1: 62 Method 2: 62 Method 3: 62 Method 4: 62

Update: None of the methods count the empty string between the last \n and the EOF as a line. Method 4 will not count a non-empty string between the last \n and the EOF as a line, although it should and could be modified to do so.