Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: How to extract the best match from matrix

by Marshall (Canon)
on Mar 12, 2017 at 23:23 UTC ( [id://1184354]=note: print w/replies, xml ) Need Help??


in reply to How to extract the best match from matrix

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?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found