The "chomp" is completely unnecessary, because the string returned by the Date::Simple object "$start" does not contain any sort of new-line character at the end. And even in cases where "chomp" should be used, calling it repeatedly on the full array each time you add one element to the array is frankly a bit stupid -- either chomp each string before pushing it on the array, or else chomp the array just once after the loop finishes (not inside the loop).while ( $start < $end ) { push @dates, "$start"; chomp(@dates); $start = $start->next; }
As for this part, pulling data out of the html page:
I wouldn't be so trusting as to assume that the web service you are pounding on so heavily will always use exactly four spaces and two digits for whatever it is that "@data" is supposed to capture, or that the particular values you want in @data will always be the first three strings in the page that match that pattern. You should use a real HTML parsing module, and base your data extraction on more explicit evidence about the content.my @data = ($page =~ /:\s\s\s\s(\d\d)/g);
(Update: Having just seen a sample of the page delivered by that Penn State web form, I'll retract the advice about using an HTML parser. Each page of single-day data is just a sequence of fixed-width plain text lines with a '<PRE>' tag at the start of each line. Interesting approach... In any case, the method suggested below would be an improvement: it can apply generally to any set of data fields on the page, and all you have to do is manage a hash of targeted data fields.)
In addition the GrandFather's point about capturing a single value into a scalar instead of an array, your code would actually make more sense using a hash, and you could eliminate two repetitions of 7 lines of code by looping over hash keys "rain snow depth". (A hash of hashes can also store the regex to be matched and format to be used for each key, as well as the extracted values.) That way, the next time you change something that affects all three fields, you only have to make one edit instead of three. (DRY: Don't Repeat Yourself.)
Anyway, I agree with merlyn's point that this is an offensive amount of web traffic to get a small amount of data that should be more easily (and reliably) accessible, in a more usable tabular form, by some other means.
In reply to Re: Improve My Code...
by graff
in thread Improve My Code...
by cheech
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |