Welcome to WebmasterWorld Guest from 54.163.49.19

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

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

     
2:31 am on Jan 22, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 5, 2003
posts:807
votes: 0


How do you make perl search for say today([^&]+)today, where ([^&]+) is any number, then make it a variable, for example $12345. Then change it to 123.45, and then do math to it, 123.45*.40 = 49.38 with 49.38 being what the $12345 variable is at the end?

Is it possible to change

$id2 =~ s*today([^&]+)today*$1*g;

so it creates the $12345 variable?
5:35 am on Jan 22, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 5, 2003
posts:807
votes: 0


What perl is searching is...

Today: today408today
Clicks: 34
Yesterday: yesterday555yesterday
Clicks: 61
This Month: this11360this
Clicks: 812
Last Month: last5350last
Clicks: 454

where the numbers are always changing, and I've come up with...

$id3 =~ /today(d+)today/;
my $ans = $1 * .40;
$final = ($ans / 100);

$id4 =~ /yesterday(d+)yesterday/;
my $ans2 = $2 * .40;
$final2 = ($ans2 / 100);

$id5 =~ /this(d+)this/;
my $ans3 = $3 * .40;
$final3 = ($ans3 / 100);

$id6 =~ /last(d+)last/;
my $ans4 = $4 * .40;
$final4 = ($ans4 / 100);

{
print "Content-Type: text/html\n\n";
print <<EOM;
<PRE>
$id2
<HR>
Original Number: $1 - $2 - $3 - $4
<HR>
After it's been converted: $ans - $ans2 - $ans3 - $ans4
After being divide by 100: $final - $final2 - $final3 - $final4


which spits out

spits out

Original Number: 5350 - - -
After it's been converted: 2140 - 0 - 0 - 0
After being divide by 100: 21.4 - 0 - 0 - 0

only messing with the last number, last5350last.
10:34 am on Jan 22, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 31, 2008
posts:661
votes: 0


$1 is always the first ($2 the second and so on) match in the current regular expression, not the total number of regular expressions, so
$id3 =~ /today(d+)today/; my $ans = $1 * .40; $final = ($ans / 100); $id4 =~ /yesterday(d+)yesterday/; my $ans2 = $2 * .40; $final2 = ($ans2 / 100);

shouldn't have $2 when matching for yesterday, but $1. also, it get's much easier to track not-matching regexps, if you check wether it matched:
if($id4 =~ m/yesterday(d+)yesterday/) { my $ans2 = $1 * .40; $final2 = ($ans2 / 100); } else { print "oh no, my regexp failed\n"; }

and last: is the text to be searched in one string or do you split it before searching the numbers desired? just asking because you could just run all those regular expressions on the original string, without splitting it in $id4, $id5 etc.
8:02 pm on Jan 22, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 5, 2003
posts:807
votes: 0


I've done no search or split yet. I'm a total n00bie on this.

Is this supposed to do it for all four of them? $id4 is all of the data.

if($id4 =~ m/yesterday(d+)yesterday/) {
my $ans = $1 * .40;
$final = ($ans / 100);}

if($id4 =~ m/today(d+)today/) {
my $ans2 = $1 * .40;
$final2 = ($ans2 / 100);}

if($id4 =~ m/this(d+)this/) {
my $ans3 = $1 * .40;
$final3 = ($ans3 / 100);}

if($id4 =~ m/last(d+)last/) {
my $ans4 = $1 * .40;
$final4 = ($ans3 / 100);}
9:08 pm on Jan 22, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 31, 2008
posts:661
votes: 0


yeah, that looks pretty good.
when in doubt, add else-blocks to the if-matches so you can see which one fails. but eah, that looks better.
.40 could be written as 0.40, just to make it easier to read.
11:21 pm on Jan 22, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 5, 2003
posts:807
votes: 0


SSH....

root@wor [/home/site82/public_html/cgi-bin]# perl stats.cgi
oh no, my regexp failed
oh no, my regexp failed
oh no, my regexp failed
oh no, my regexp failed



Here's the full code....

#!/usr/bin/perl
use CGI ':standard';
use LWP::Simple qw(!head);
use CGI qw(:standard); # then only CGI.pm defines a head()
my($id) = param('id');

#use warnings;
#use diagnostics;
#use strict;

$id2 = get ("http://www.domain.com/stats.php?id=$id");

$id2 =~ s*\[TODAY\] *Today: *g;

#A lot more search and replaces go right here to add the text before and after the numbers.

if($id4 =~ m/yesterday(d+)yesterday/) {
my $ans = $1 * .40;
$final = ($ans / 100);
} else {
print "oh no, my regexp failed\n";
}

if($id4 =~ m/today(d+)today/) {
my $ans2 = $1 * .40;
$final2 = ($ans2 / 100);
} else {
print "oh no, my regexp failed\n";
}

if($id4 =~ m/this(d+)this/) {
my $ans3 = $1 * .40;
$final3 = ($ans3 / 100);
} else {
print "oh no, my regexp failed\n";
}

if($id4 =~ m/last(d+)last/) {
my $ans4 = $1 * .40;
$final4 = ($ans3 / 100);
} else {
print "oh no, my regexp failed\n";
}

{
print "Content-Type: text/html\n\n";
print <<EOM;
$id2
<HR>
After it's been converted: $ans - $ans2 - $ans3 - $ans4
After being divide by 100: $final - $final2 - $final3 - $final4
<HR>
EOM
exit;
}
11:30 pm on Jan 22, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 31, 2008
posts:661
votes: 0


print $id4 just before starting your searches.

print "'" . $id4 . "'"

just to make sure that it really contains what you expect it to contain