Hi Aravind,

On Thu, 03 May 2012 13:34:35 +0200
venkates <venka...@nt.ntnu.no> wrote:

> Hi all,
> 
> I am trying to parse a tab-delimited file which has repeating lines. 
> This is causing  problems while parsing it to the data structure (see 
> below). I would appreciate if you could help me solve this.
> 

If your file is not too large, you can use an in-memory hash to detect if a
line has already been encountered. See:

http://perldoc.perl.org/perlfaq4.html#How-can-I-remove-duplicate-elements-from-a-list-or-array%3f

Else, you may opt looking at the UNIX "uniq" or "sort -u" commands.

Now a few comments on your code:

> sub parse {
> 
>      my $pazar_file_path = shift;
>      my $pazar_data; # ref to a hash holding the parsed data
> 
>      open FH, '<', $pazar_file_path or croak ( "Cannot open file 
> '$pazar_file_path': $!" );

FH should be a lexical file handle:

        open my $fh, '<',
>      while ( my $data = <FH> ) {
> 
>          chomp $data;
>          my @record_lines = split /\t/, $data;
>          my ( $pazar_p_id, $prot_id, $pazar_g_id, $ensembl_id ) = splice 
> ( @record_lines, 0, 4 );
>          push @{ 
> $pazar_data->{$pazar_p_id}{$prot_id}{$pazar_g_id}{$ensembl_id} }, 
> @record_lines;

This is better written as 

my ( $pazar_p_id, $prot_id, $pazar_g_id, $ensembl_id, @rest_of_record ) = 
@record_lines;

Also @record_lines is misnamed because it handles only one line. Maybe
@record_fields .
> 
>      } # end of <FH>
>      close FH;
>      $pazar_data ? return $pazar_data : carp "No data!";
> 

Please don't put statements with such side-effects inside the
expression conditional ("?:"):

http://perl-begin.org/tutorials/bad-elements/#ternary_operator_instead_of_if_else

Regards,

        Shlomi Fish


-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
"The Human Hacking Field Guide" - http://shlom.in/hhfg

Chuck Norris doesn’t make mistakes. (Su‐Shee) He corrects God. (Shlomi Fish)

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to