Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Matching Values in an Array

by Laurent_R (Canon)
on Nov 21, 2014 at 19:54 UTC ( [id://1108047]=note: print w/replies, xml ) Need Help??


in reply to Matching Values in an Array

Your approach is going the wrong around (although you could still make it work). Basically, in such a case, you need to read data from Station.CSV and lookup data from Parameters.CSV. The best way to do that is to first load Parameters.CSV into memory (in a hash of hashes, I would say, but an array of hashes would also fit the bill), and, once you've done that to read sequentially Station.CSV and to the necessary hash lookups. Your data is not very large, the second file will fit into memory without any problem.

Possibly something like this (quick untested code) to populate the HoH:

my @fieldnames = qw /S_10 S_11 S_12 S_13 S_14 S_15 T_10 T_11 T_12 T_13 + T_14 T_15/; # Note you could also make it dynamic and generate it from the in +put my %params; while (<$Param>) { my ($index, @values) = split /,/, $_; $params{$index} = { map {$fieldnames[$_], $values{$_}} 0..15;} }
Retrieving the data is then quite simple in the %params HoH.

No time now, but I'll try to give a complete solution in a couple of hours.

Update: corrected a mistake in the code above. And below the full solution, a bit later than I originally planned:

use strict; use warnings; my (undef, @fieldnames) = split /,/, <DATA>; chomp @fieldnames; my %params; while (<DATA>) { chomp; my ($index, @values) = split /,/, $_; $params{$index} = {map {$fieldnames[$_], $values[$_]} 0..$#field +names}; } my $stat = "1,10 2,11 3,12 4,13 5,14"; open my $STAT, "<", \$stat or die "cannot open $stat $!"; while (<$STAT>) { chomp; next if /sta/i; my ($stat, $code) = split /,/; print "Station $stat : Salinity: ", $params{$stat}{"S_$code"}, "; + Temperature: ", $params{$stat}{"T_$code"}, "\n"; } __DATA__ Station,S_10,S_11,S_12,S_13,S_14,S_15,T_10,T_11,T_12,T_13,T_14,T_15 1,31,29,29,31,29,29,15,14,23,15,14,23 2,33,28,23,33,28,23,17,15,23,17,15,23 3,23,27,33,23,27,33,18,16,23,18,16,23 4,25,26,28,25,26,28,23,14,15,23,14,15 5,26,26,27,26,26,27,23,18,17,23,18,17 6,27,33,31,27,33,31,14,17,18,14,17,18 7,33,29,29,33,29,29,12,18,23,12,18,23
Output:
Station 1 : Salinity: 31; Temperature: 15 Station 2 : Salinity: 28; Temperature: 15 Station 3 : Salinity: 33; Temperature: 23 Station 4 : Salinity: 25; Temperature: 23 Station 5 : Salinity: 26; Temperature: 18

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-20 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found