Search code examples
perlprintingweather-apii3

Print statement prints twice in outer block in perl


The following is a code adapted from this link for getting the weather and displaying on i3blocks.

#!/bin/bash 

METRIC=1 #Should be 0 or 1; 0 for F, 1 for C

if [ -z $1 ]; then
    echo
    echo "USAGE: weather.sh <locationcode>"
    echo
    exit 0;
fi

curl -s http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=${METRIC}\&locCode\=$1 | perl -ne 'use utf8;

if ( /Currently/ ) {

    chomp;
    /\<title\>Currently: (.*)?\<\/title\>/;

    my @values  = split(":", $1); 
    my $deg     = "°C";
    my @values2 = split("C", $values[1]);

    if ( $values[0] eq "Sunny" || $values[0] eq "Mostly Sunny" || $values[0] eq "Partly Sunny" || $values[0] eq "Intermittent Clouds" || $values[0] eq "Hazy Sunshine" || $values[0] eq "Hazy Sunshine" || $values[0] eq "Hot") {
        my $sun = "";
        binmode(STDOUT, ":utf8");
        print "$sun";
    }

    if ( $values[0] eq "Mostly Cloudy" || $values[0] eq "Cloudy" || $values[0] eq "Dreary (Overcast)" || $values[0] eq "Fog" ) {
        my $cloud = "";
        binmode(STDOUT, ":utf8");
        print "$cloud";
    }

    if ( $values[0] eq "Showers" || $values[0] eq "Mostly Cloudy w/ Showers" || $values[0] eq "Partly Sunny w/ Showers" || $values[0] eq "T-Storms"|| $values[0] eq "Mostly Cloudy w/ T-Storms"|| $values[0] eq "Partly Sunny w/ T-Storms"|| $values[0] eq "Rain" ) {
        my $rain = "";
        binmode(STDOUT, ":utf8");
        print "$rain";
    }

    if ( $values[0] eq "Windy" ) {
        my $wind = "";
        binmode(STDOUT, ":utf8");
        print "$wind";
    }

    if ( $values[0] eq "Flurries" || $values[0] eq "Mostly Cloudy w/ Flurries" || $values[0] eq "Partly Sunny w/ Flurries"|| $values[0] eq "Snow"|| $values[0] eq "Mostly Cloudy w/ Snow"|| $values[0] eq "Ice"|| $values[0] eq "Sleet"|| $values[0] eq "Freezing Rain"|| $values[0] eq "Rain and Snow"|| $values[0] eq "Cold" ) {
        my $snow = "";
        binmode(STDOUT, ":utf8");
        print "$snow";
    }

    if ( $values[0] eq "Clear" || $values[0] eq "Mostly Clear" || $values[0] eq "Partly Cloudy"|| $values[0] eq "Intermittent Clouds"|| $values[0] eq "Hazy Moonlight"|| $values[0] eq "Mostly Cloudy"|| $values[0] eq "Partly Cloudy w/ Showers"|| $values[0] eq "Mostly Cloudy w/ Showers"|| $values[0] eq "Partly Cloudy w/ T-Storms"|| $values[0] eq "Mostly Cloudy w/ Flurries" || $values[0] eq "Mostly Cloudy w/ Snow" ) {
        my $night = "";
        binmode(STDOUT, ":utf8");
        print "$night";
    }

    binmode(STDOUT, ":utf8");
    print  "$values2[0]$deg"; 
}'

(The text that might look like boxes are from the fontawesome font). All I added was the following lines

my $deg = "°C";
my @values2 = split("C",$values[1]);

and modified the last print statement which was originally print "$values[1]". The old print statement shows the temperature without a degree sign like 35C. My intention was just to add the degree in between like 35°C but the output is

35°C°C

Why is the substring printed twice? This happens even if I include it in a separate print statement or include the substring directly (print $values2[0]°C).

Oddly, including the value and unit in the inner blocks such as print "$sun$values2[0]$deg"; seems to work fine without duplication.


Solution

  • The script works by looking for the <title> tag in the RSS. If you do that manually, you'll see there are several.'

    curl -s 'http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=ASI|IN|IN031|MADRAS' | grep title
    <title>Madras, IN - AccuWeather.com Forecast</title>
                <title>Madras, IN - AccuWeather.com Forecast</title>
                <title>Currently: Partly Sunny: 35C</title> 
                    <title>5/23/2018 Forecast</title>
                    <title>5/24/2018 Forecast</title>
    <title>The AccuWeather.com RSS Center</title>
    

    If you add

    use strict;
    use warnings;
    

    you will get a ton of warnings about variables being undefined. That's because it doesn't find a match for title with that pattern, but it still prints.

      if (/Currently/) {
        chomp;
        /\<title\>Currently: (.*)?\<\/title\>/;
    
        # ....
        print "$values2[0]$deg";
      }
    

    That's also why you get the additional output.

    You need to skip the line if there is no match.

    /\<title\>Currently: (.*)?\<\/title\>/ or next;