in reply to Regex to Array lookup question

Thank you everyone for your responses. Sorry for my delayed response.

I've looked at your examples and I have tried a few different things.

I think I am going to go with a bit simpler approach looking at some other similar code, that so far in testing seems to be working for what I need.

# Remove leading url $icon =~ s/https:\/\/api.weather.gov\/icons\/land\///; # Split at / my @iconSplit = split(/\//, $icon); my $dayNight = $iconSplit[0]; # Split at ? to remove size info $icon = $iconSplit[1]; my @iconSplit2 = split(/\?/, $icon); $icon = $iconSplit2[0]; # Split at , for % of precip if present my @iconSplit3 = split(/\,/, $icon); $icon = $iconSplit3[0]; my $poP = $iconSplit3[1]; if (not defined $poP) { $poP = 0; # null } my %newIcons= ( 'skc' => "clear-day.png", 'few' => "partly-cloudy-day.png", 'sct' => "partly-cloudy-day.png", 'bkn' => "cloudy.png", 'ovc' => "cloudy.png", 'wind_skc' => "wind.png", 'wind_few' => "wind.png", 'wind_sct' => "wind.png", 'wind_bkn' => "wind.png", 'wind_ovc' => "wind.png", 'snow' => "snow.png", 'rain_snow' => "sleet.png", 'rain_sleet' => "sleet.png", 'snow_sleet' => "sleet.png", 'fzra' => "sleet.png", 'rain_fzra' => "sleet.png", 'snow_fzra' => "sleet.png", 'sleet' => "sleet.png", 'rain' => "rain.png", 'rain_showers' => "rain.png", 'rain_showers_hi' => "rain.png", 'tsra' => "thunderstorm.png", 'tsra_sct' => "thunderstorm.png", 'tsra_hi' => "thunderstorm.png", 'tornado' => "tornado.png", 'hurricane' => "thunderstorm.png", 'tropical_storm' => "thunderstorm.png", 'dust' => "fog.png", 'smoke' => "fog.png", 'haze' => "fog.png", 'hot' => "clear-day.png", 'cold' => "clear-day.png", 'blizzard' => "snow.png", 'fog' => "fog.png" ); $icon = $newIcons{$icon};

With DarkSky shutting down their API later this year, I need to move over to NWS API, and match current build parameters and icon sets as close as possible.

Then later I can go back and add more icons to the existing sets, and clean up some of the other smaller details that are changing because of this move.

Thanks again!

Replies are listed 'Best First'.
Re^2: Regex to Array lookup question
by AnomalousMonk (Archbishop) on Apr 13, 2020 at 07:16 UTC
    $icon =~ s/https:\/\/api.weather.gov\/icons\/land\///;

    This substitution needs more backslashes (escapes)! (Never thought I'd say that.) The regex  . (dot) operator matches, by default, any character except a newline. E.g.:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $icon = 'https://apiXweatherXgov/icons/land/fooble'; $icon =~ s/https:\/\/api.weather.gov\/icons\/land\///; print qq{'$icon'}; " 'fooble'
    To match only a period, escape the dot metacharacters:
        $icon =~ s/https:\/\/api\.weather\.gov\/icons\/land\///;
    Of course, a dot also matches a period and that's all that seems to be in those particular positions in your strings so you might never have known the difference, but just for future reference...

    BTW: One way to cut down on escapes in a regex is by the wise choice of a regex delimiter. E.g.:
        $icon =~ s{https://api\.weather\.gov/icons/land/}{};

    Please see perlre, perlretut, and perlrequick.


    Give a man a fish:  <%-{-{-{-<