Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Increasing the efficiency of the code

by Limbic~Region (Chancellor)
on Nov 29, 2006 at 13:08 UTC ( [id://586684]=note: print w/replies, xml ) Need Help??


in reply to Increasing the efficiency of the code

Anonymous Monk,
I have re-written your code to be to possibly be much more efficient. I have not provided comments so I suspect you won't immediately understand it. The strict and warnings pragmas can point out a lot of problems in your code and as such should be used.
my @sorted_keys = sort keys %conwithpostion; my @sorted_cols = sort {$a <=> $b} keys %cols_pos; my %dispatch = ( '=' => sub {$_[0] eq $_[1]}, '!=' => sub {$_[0] ne $_[1]}, '>' => sub {$_[0] gt $_[1]}, '>=' => sub {$_[0] ge $_[1]}, '<' => sub {$_[0] lt $_[1]}, '<=' => sub {$_[0] le $_[1]}, ); for (@array) { my $match = 0; my @line = split /\t/; for my $key_pos (@sorted_keys) { my ($op, $arg1, $arg2) = (@{$conwithposition{$key_pos}}[0, 1], + $line[$key_pos]); $match = $dispatch{$op}->($arg1, $arg2); last if ! $match; my $col = $sorted_cols[-1]; push @listcols, $col; push @result_arr, join '|', @line[@sorted_cols]; } }
Please feel free to ask questions, but try to find the answers on your own first. See Coping with Scoping and perldoc (also available from the command line) for starters. The trick I did to avoid the if/eslif chain is called a dispatch table.

Update: As GrandFather points out in his node, your use of $col is likely incorrect. I set $col to the last value of the sorted %cols_pos keys on the assumption you thought that is the value it would be after the loop. If $col is set elsewhere in your code then you will need to change this accordingly.

Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://586684]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-28 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found