Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: Increasing the efficiency of the code by Limbic~Region
in thread Increasing the efficiency of the code by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found