Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi jnarayan81,

Yes, in general re-reading an input file multiple times is a bad idea. I/O is very "expensive" both in terms of CPU time, but also in terms of clock time.

A simple way to read the input file once, could be:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @matrix; # Array of Array, ["africa1",18,1,48,23] <DATA>; # throw away first line while (<DATA>) { next if /^\s*$/; # skip blank lines push (@matrix, [split(/\s+/,$_)]); # create AoA (2-D matrix) } print Dumper \@matrix; __DATA__ Name f1 f2 f3 f4 africa1 18 1 48 23 usa2 48 23 60 23 africa2 17 3 49 25 africa3 20 6 52 30 usa1 55 20 56 25 china1 35 37 55 87 china2 40 33 50 73 africa4 18 2 47 23
The above may or may not be the best data structure for your search requirements. But I suggest reading the input file only once.

It appears to me that you are comparing columns. I think that you want an output that is based "best match(es)" for each each column. I added "africa4" above.

What is supposed to happen if: my @array = (18,35,59,70); Should this first array value of 18 result in two matches in the first data column?, africa1 and africa4??

It is completely plausible that 16 and 20 are both equally close to 18. What should the output be in those cases?


In reply to Re: How to extract the best match from matrix by Marshall
in thread How to extract the best match from matrix by jnarayan81

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 studying the Monastery: (2)
As of 2024-04-26 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found