Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Search for abcNUMBERdef, make it a variable, then do math?

by Albannach (Prior)
on Jan 22, 2011 at 15:04 UTC ( #883683=note: print w/ replies, xml ) Need Help??


in reply to Search for abcNUMBERdef, make it a variable, then do math?

Unless you need to store the values for later use, I'd do it this way:

use strict; use warnings; while(<DATA>) { if(/([\w\s]+:)\s([a-z]+)(\d+)\2/) { print "$1 ", sprintf("%5.2f\n",$3*0.4/100); } } __DATA__ Today: today408today Clicks: 34 Yesterday: yesterday555yesterday Clicks: 61 This Month: this11360this Clicks: 812 Last Month: last5350last Clicks: 454
which gives
Today: 1.63 Yesterday: 2.22 This Month: 45.44 Last Month: 21.40
Edited: Removed unnecessary extra colon in print (I had captured it already from the input) and inserted actual output generated - duh!

--
I'd like to be able to assign to an luser


Comment on Re: Search for abcNUMBERdef, make it a variable, then do math?
Select or Download Code
Re^2: Search for abcNUMBERdef, make it a variable, then do math?
by Jesse Smith (Novice) on Jan 22, 2011 at 19:35 UTC
    #!/usr/bin/perl use warnings; use strict; if (length ($ENV{'QUERY_STRING'}) > 0){ my($buffer) = $ENV{'QUERY_STRING'}; my(@pairs) = split(/&/, $buffer); foreach my($pair) (@pairs){ ($name, $value) = split(/=/, $pair); my($value) =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1) +)/eg; $in{$name} = $value; } } my($id) = param('id'); use LWP::Simple; $id2 = get ("http://www.domain.info/stats.php?id=$id"); $id2 =~ s*\[Yesterday\] *Yesterday: *g; while(<DATA>) { if(/([\w\s]+:)\s([a-z]+)(\d+)\2/) { print "$1 ", sprintf("%5.2f\n",$3*0.4/100); } } spits out Missing $ on loop variable at stats.cgi line 9. on SSH.
      Its because you keep including that ENV{'QUERY_STRING'} garbage, get rid of it

      I would assume that Perl is right when it points you to line 9.

      ... foreach my($pair) (@pairs){ ...

      This is not valid Perl code. The valid Perl code would be (note the lack of parentheses)

      foreach my $pair (@pairs){

      But in all seriousness, why are you trying to do what is CGI.pm's job? Just use CGI; and then query $q->Vars to get a hash of parameters passed to your script. Don't parse query strings yourself.

        #!/usr/bin/perl use warnings; use strict; use LWP::Simple qw(!head); use CGI qw(:standard); # then only CGI.pm defines a head() my($id) = param('id'); my($id2) = get ("http://www.domain.com/stats.php?id=$id"); $id2 =~ s*\[Yesterday\] *Yesterday: *g; **LOTS more search and replaces go right here** while(<DATA>) { if(/([\w\s]+:)\s([a-z]+)(\d+)\2/) { print "$1 ", sprintf("%5.2f\n",$3*0.4/100); } } Name "main::DATA" used only once: possible typo at stats.cgi line 15. Use of uninitialized value in concatenation (.) or string at stats.cgi + line 11. (Line geting the URL) readline() on unopened filehandle DATA at stats.cgi line 15.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-08-01 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (257 votes), past polls