in reply to Re^2: Script Issues
in thread Script Issues

Hauke, thanks for the additional help. The fatalstobrowser definitely helps in not having to refresh the error log continuously.

I did attempt to look around for another Perl install, but didn't have any luck. It looks as if I'm stuck with 5.8.8 for the moment.

Basically what I'm attempting to do, is copy information for an user (listed at the top of the code) from the main gr.txt file as listed in the code, and place it along with some other text in a pre-formatted output file. I've attached a copy of the output below, along with my updated script based on the suggestions made here.

Updated Code:

#!/usr/bin/perl -w use LWP::Simple; use CGI::Carp qw(fatalsToBrowser); use strict; use warnings; @show = ( 'Mike Cox*', ); print <<EOF; Refresh: 1 Threshold: 999 Title: Into The Meso SN Locations Font: 1, 14, 0, "Arial" IconFile: 1, 22, 22, 11, 11, "http://www.spotternetwork.org/icon/spott +ernet.png" IconFile: 2, 15, 25, 8, 25, "http://www.spotternetwork.org/icon/arrows +.png" IconFile: 6, 22, 22, 11, 11, "http://www.spotternetwork.org/icon/spott +ernet_new.png" EOF my $sn = get 'http://www.spotternetwork.org/feeds/gr.txt'; $x = length $sn; $sn = substr($sn, 343, $x-343); my @values = split('End:', $sn); foreach my $val (@values){ if($val =~ m/Text:\s15\,\s10\,\s1\,\s\"(.+)\"/g){ my $name = $1; if (grep {$name eq $_} @show){ ...; }{ print $val; print "End:\n" } } }
Sample output:
Refresh: 1 Threshold: 999 Title: Into The Meso SN Locations Font: 1, 14, 0, "Arial" IconFile: 1, 22, 22, 11, 11, "http://www.spotternetwork.org/icon/spott +ernet.png" IconFile: 2, 15, 25, 8, 25, "http://www.spotternetwork.org/icon/arrows +.png" IconFile: 6, 22, 22, 11, 11, "http://www.spotternetwork.org/icon/spott +ernet_new.png" Object: 41.3149986,-93.1019974 Icon: 0,0,000,6,2,"Mike Cox*\n2016-04-09 01:22:27 UTC\nSTATIONARY\nEma +il: mike@iowachaser.com\nHam: 146.550 - K0SVR\nTwitter: https://twitt +er.com/IntoTheMeso\nWeb: www.intothemeso.com\nNote: Into The Meso Cha +se Team" Text: 15, 10, 1, "Mike Cox*" End:

Replies are listed 'Best First'.
Re^4: Script Issues
by haukex (Archbishop) on Apr 09, 2016 at 09:41 UTC

    Hi Mike,

    I don't have a copy of 5.8.8 at the moment to test on, but I did take a look at your code. Here's what I found so far:

    You're missing a my in front of the first mention of @show and $x, and you're missing an else between the if and its following block, assuming you didn't want just a bare {...} block after the if. I'd also very much recommend you indent your blocks, perltidy or a good IDE can help there.

    It looks like the regex for extracting the name works, it's just that at the moment there's no "Mike Cox" in the list. Assuming you want to print only the records that match the names in @show, then you've got your logic reversed: the print $val; print "End:\n"; needs to go in the if block, not the else block (it can also be written print $val, "End:\n";). Also, I notice you've got an asterisk in the string 'Mike Cox*', is that intentional? Is it supposed to be a regex, or do you want to match 'Mike Cox*' literally?

    When I make the above changes and add a name that's on the list to @show, the code works for me and the output looks like what you wanted. If you're still having problems, it would be very helpful if you could include the actual output your program is giving you (including error messages).

    Here's another small optimization: the declaration of @show can be changed to my %show = map {$_=>1} 'Mike Cox*', '...'; (i.e. turn it into a hash %show where the keys are the names and the values are just "1", or any other true value), and then you can replace if(grep {$name eq $_} @show) with a simple hash lookup if($show{$name}).

    Hope this helps,
    -- Hauke D

Re^4: Script Issues
by Iowachaser (Novice) on Apr 09, 2016 at 17:41 UTC

    Thanks for everyone's help!

    I was finally able to get the script to run successfully on my web server as well as learning a few things about Perl scripting.

    I will definitely be utilizing the resources on this sight to help learn Perl more in depth and to possibly help others in the future.

    Again, I appreciate everyone who chipped in to help me with my script and I'm grabbing the offering plate to show my gratitude.

    Until next time,

    Mike

      ... I'm grabbing the offering plate ...

      Yay! Extra beer and gooseberries for all the monks tonight!


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