Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Regex to Array lookup question

by Aldebaran (Deacon)
on Apr 07, 2020 at 05:18 UTC ( #11115150=note: print w/replies, xml ) Need Help??


in reply to Re: Regex to Array lookup question
in thread Regex to Array lookup question

This gets the textual defs of each abbreviation from the website and translates the first "word" argument of the last path in the URL to that textual definition. I translated each of the URL's you provided. Please explain what else you need...

Shoot, Marshall, I want to replicate this interesting script, but I can't see any braces or underscores out of place. I did snip off the documentation to try to shake this error, but it remains unchanged:

$ ./1.marshall.pl JSON error at line 110, byte 2639/2647: Unexpected character '_' parsi +ng initial state: expecting whitespace: 'n', '\r', '\t', ' ' at ./1.m +arshall.pl line 8, <DATA> line 1. $ cat 1.marshall.pl #!/usr/bin/perl -w use 5.016; use JSON::Parse 'parse_json'; my $json = do{local $/ = undef;<DATA>}; my $out = parse_json $json; my %xlated_abbrev; #simple abbreviation table => description foreach my $key (keys %{$out->{icons}}) #gen simple xlate table { $xlated_abbrev{$key} = $out->{icons}{$key}{description}; } my @urls = ( 'https://api.weather.gov/icons/land/day/tsra_sct,20/tsra_sct,40?size=m +edium', 'https://api.weather.gov/icons/land/day/rain_showers,30/tsra_hi,30?siz +e=medium', 'https://api.weather.gov/icons/land/night/rain_showers,30/rain_showers +?size=medium', 'https://api.weather.gov/icons/land/day/bkn?size=medium' ); foreach my $url (@urls) { my $last_path = (split('/',$url))[-1]; my ($abbrev_to_xlate) = $last_path =~ /^(\w+)/; print "URL = $url\n"; print " $abbrev_to_xlate => \'$xlated_abbrev{$abbrev_to_xlate}\'\ +n\n"; } __DATA__ { "@context": [], "icons": { "skc": { "description": "Fair/clear" }, "few": { "description": "A few clouds" }, "sct": { "description": "Partly cloudy" }, "bkn": { "description": "Mostly cloudy" }, "ovc": { "description": "Overcast" }, "wind_skc": { "description": "Fair/clear and windy" }, "wind_few": { "description": "A few clouds and windy" }, "wind_sct": { "description": "Partly cloudy and windy" }, "wind_bkn": { "description": "Mostly cloudy and windy" }, "wind_ovc": { "description": "Overcast and windy" }, "snow": { "description": "Snow" }, "rain_snow": { "description": "Rain/snow" }, "rain_sleet": { "description": "Rain/sleet" }, "snow_sleet": { "description": "Rain/sleet" }, "fzra": { "description": "Freezing rain" }, "rain_fzra": { "description": "Rain/freezing rain" }, "snow_fzra": { "description": "Freezing rain/snow" }, "sleet": { "description": "Sleet" }, "rain": { "description": "Rain" }, "rain_showers": { "description": "Rain showers (high cloud cover)" }, "rain_showers_hi": { "description": "Rain showers (low cloud cover)" }, "tsra": { "description": "Thunderstorm (high cloud cover)" }, "tsra_sct": { "description": "Thunderstorm (medium cloud cover)" }, "tsra_hi": { "description": "Thunderstorm (low cloud cover)" }, "tornado": { "description": "Tornado" }, "hurricane": { "description": "Hurricane conditions" }, "tropical_storm": { "description": "Tropical storm conditions" }, "dust": { "description": "Dust" }, "smoke": { "description": "Smoke" }, "haze": { "description": "Haze" }, "hot": { "description": "Hot" }, "cold": { "description": "Cold" }, "blizzard": { "description": "Blizzard" }, "fog": { "description": "Fog/mist" } } } __END__ $

Replies are listed 'Best First'.
Re^3: Regex to Array lookup question
by Marshall (Abbot) on Apr 07, 2020 at 05:45 UTC
    When using a __DATA__ segment, you can't use an __END__ segment. Delete that __END__ line that you added. This is the reason that I embedded the output as a Perldoc instead of attaching the output after an __END__ segment.

    Also, add "use strict;" to the code like I did. This will help you as you experiment with the code.

    Update: this error: "JSON error at line 110, byte 2639/2647: Unexpected character '_' parsing initial state: expecting whitespace: 'n', '\r', '\t', ' ' at ./1.marshall.pl line 8, <DATA> line 1." Is complaining about the first underscore in the __END__ line that you added. The result is invalid JSON syntax. Error messages are often hard to figure out.

      My mistake:

      $ ./1.marshall.pl URL = https://api.weather.gov/icons/land/day/tsra_sct,20/tsra_sct,40?s +ize=medium tsra_sct => 'Thunderstorm (medium cloud cover)' URL = https://api.weather.gov/icons/land/day/rain_showers,30/tsra_hi,3 +0?size=medium tsra_hi => 'Thunderstorm (low cloud cover)' URL = https://api.weather.gov/icons/land/night/rain_showers,30/rain_sh +owers?size=medium rain_showers => 'Rain showers (high cloud cover)' URL = https://api.weather.gov/icons/land/day/bkn?size=medium bkn => 'Mostly cloudy' $
        This is more like a "boo-boo" than a mistake. I am glad that you can run my code now. Let the Monks know how to help you further.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2020-05-25 15:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (146 votes). Check out past polls.

    Notices?