Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

Replies are listed 'Best First'.
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 ("$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.

      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'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 defines a head() my($id) = param('id'); my($id2) = get ("$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.
      Its because you keep including that ENV{'QUERY_STRING'} garbage, get rid of it

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://883683]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2017-06-23 23:11 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (555 votes). Check out past polls.