Since you do not need to output all the data in a sorted form,
and obviously not store the data sorted somewhere,
I would probably not try to solve the problem with any sort at all.
I see the problem more as a data extraction problem.
- Read all lines, one by one.
- Find the highest value in every line.
- Check if you already have this state in your hash.
- If not, save the state with the highest value found on the line to the hash.
- If already in the hash and the newly found value is higher than the highest value already stored for this state, replace with the new value.
- Loop until end of file.
- You will probably have one hash for max and another hash for min temp, but that's not necessary. You could store it all in the same hash if you prefer.
This is Bepa
| [reply] |
What I threw together below is I think very much like the response Bepa gave, although I didn't see it until I refreshed the page before submitting this. The code below seems to work, and includes sample data (including data to cause the multiple-city temperature case, which your original data, where Dallas was at 23, would not). I know it can probably be done better, however, and I would hope other, more experienced monks would comment so we all might learn from their experience. Good luck in finding your solution.
begin 664 test8.pl
M(R$O=7-R+V)I;B]P97)L("UW"G5S92!S=')I8W0["G5S92!W87)N:6YG<SL*
M"FUY("5R97-U;'1S(#T@*"`B;6EN(B`]/B`M,2P@(FUA>"(@/3X@,2`I.PIM
M>2`H)6UI;FUA>"P@)&QI;F4L("1L;V-A;&4L("1C:71Y+"`D<W1A=&4L("1R
M97-U;'0L($!T96UP<RD["@IW:&EL92`H)&QI;F4@/2`\1$%403XI('L*"6-H
M;VUP*"1L:6YE*3L*"2@D;&]C86QE+"!`=&5M<',I(#T@<W!L:70H+UQS*R\L
M("1L:6YE*3L*"2@D<W1A=&4L("1C:71Y*2`]('-P;&ET*"\L+RP@)&QO8V%L
M92P@,BD["@H)>PH)"2,@4V]R="!T:&4@=&5M<&5R871U<F5S"@D);7D@0'-T
M96UP<R`]('-O<G0@>R1A(#P]/B`D8GT@0'1E;7!S.PH)"4!T96UP<R`]($!S
M=&5M<',["@E]"@H)9F]R96%C:"!M>2`D=&5M<"`H<7<H;6EN(&UA>"DI('L*
M"0DD<F5S=6QT(#T@,3L*"0EI9B`H)'1E;7`@97$@(FUA>"(I('L*"@D)"2,@
M4F5V97)S92!T:&4@=&5M<&5R871U<F4@87)R87D@:68@;&]O:VEN9R!F;W(@
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-B`W
1."`W.2`X,R`X-"`X-B`X-PH`
`
end
Update: After finding shortly after posting this that there was a high probability this was indeed homework (compare with Sort hash of hashes), I was quite angry, and in my anger posted it to both, thinking perhaps they might at least get caught in their attempt to dupe those willing to help. Perhaps it was poor etiquite of me to post the solution on both questions, and if so, I appologize to the other monks herein. For a while, I considered removing my code altogether, but was unsure if that would be appropriate either. I am grateful for the sage advice of tilly, jeffa, and others, who suggested another course of action. The block that has now replaced the code above is actually the code, as I tested it, UUEncoded. Perhaps it only slows down those who would take advantage of the willingness to help found here, perhaps not. It is my hope that damage to trust by those involved is realized. It is my hope that damage to trust by those involved is realized. It is sad to think that now, with each post I read and consider, the question in my mind will be, "is this a real request for help, or homework?" | [reply] [d/l] |
Here's another, similar, approach...
#!/usr/bin/perl -w
use strict;
# put data in an array (however you want to do it)
my @weather_data = split "\n", <<_END_;
Ca,Freemont 87 45 91 56 89 54 67 45 76 43 81 48 76 43
Ca,Berkley 56 34 67 38 66 34 56 31 67 45 71 45 65 42
Tx,Houston 88 34 77 36 75 31 72 30 81 31 79 51 75 28
Tx,Dallas 76 43 79 45 87 28 86 32 84 45 78 46 83 23
_END_
# store data here
my %temp = ();
# cycle through all data
for (@weather_data) {
# grab state and city
next unless (/^(\w+)\W+(\w+)/);
my $state=$1;
my $city=$2;
# set as default if none set
$temp{$state}{'max_city'} ||= $city;
$temp{$state}{'min_city'} ||= $city;
# grab and compare temperatures
while (s/(\d+)//) {
my $this_temp = $1;
# set as max/min if none set
$temp{$state}{'max'} ||= $this_temp;
$temp{$state}{'min'} ||= $this_temp;
# check whether temp new max or min and assign data
if ($temp{$state}{'max'} < $this_temp) {
$temp{$state}{'max_city'} = $city;
$temp{$state}{'max'} = $this_temp;
}
elsif ($temp{$state}{'min'} > $this_temp) {
$temp{$state}{'min_city'} = $city;
$temp{$state}{'min'} = $this_temp;
}
}
}
# display results
for (keys %temp) {
print "$_ max temp $temp{$_}{'max'} $temp{$_}{'max_city'}\n";
print " min temp $temp{$_}{'min'} $temp{$_}{'min_city'}\n\n";
}
Of course, you'll have to amend this if you want all results shown in the case of a tie :)
cLive ;-) | [reply] [d/l] |