KevinADC 192 Practically a Posting Shark

Could you please give me a little explanation on the above code? I am not sure what some of it does.

I am guessing the $^I is pretty much the same as the -i switch when editing a file on the command line with the substitute command.

I know what while(<>) does.

The area that i am really getting fuzzy with is with the if statement. I know the first part looks for a line starting with pageroncall:, but the rest is greek to me. Could you please explain this to me?

Correct, $^I is the same as -i.

if(/^pageoncall: \d+(\Q@pager.com\E)/) {

the "if" condition is a regexp that finds the line that starts wih "pageoncall:" followed by a space and one or more digits: \d+. This part (\Q@pager.com\E) captures the email address
and stores it in $1.

\Q tells perl to escape meta characters like @ and . in the string so they are treated as literal characters, the \E tells perl to where to stop escaping meta characters.

The next line replaces the new number with the old and reprints the line back into the file.

print "pageoncall: $num$1\n";

the other 'print' line just prints the rest of the lines back into the file.

Did it work?

KevinADC 192 Practically a Posting Shark

Install the module using PPM. Read the activestate documentation in the activestate folder on your hard drive for instructions on PPM for your particular version of activestate perl.

KevinADC 192 Practically a Posting Shark

assume you have the pager number already in a scalar, untested code:

my $newpagernum = '1234567890';
my $file = '/etc/aliases';
change_pager($newpagernum,$file);

sub change_pager {
   local @ARGV = @_;
   if (@ARGV != 2) { 
      die "Usage: Incorrect number of arguments [pagernumber, file]: $0\n"; 
   }
   my $num = shift @ARVG;
   local $^I = '.bak'; #inplace edit mode and creates a backup of the file
   while(<>) {
      if(/^pageoncall: \d+(\Q@pager.com\E)/) {
         print "pageoncall: $num$1\n";
      }
      else {
         print;
      }
}

Try the above on a dummy aliases file and see how it works, then work it into your existing program if all is well.

KevinADC 192 Practically a Posting Shark

I don't understand what you are trying to do. Can you explain it better? What does the aliases file look like?

KevinADC 192 Practically a Posting Shark

I really have no idea if strawberry perl will help solve your dilema. But it does give you access to more modules.

Read the help files of PPM and learn how to add repositories. You can find more modules that way.

Maybe ask on perlmongers.com where there are more perl people with more knowledge.

KevinADC 192 Practically a Posting Shark

I have installed apache, php, mysql, cgi

By CGI do you mean perl? I use activestate perl and mysql but on an old Windows98 test server. If the mysql drivers do not work for XPpro I am not sure what you can do.

You could try installing strawberry-perl instead of activestate perl. www.strawberryperl.com in which case you have full access to CPAN instead of having to use PPM repositories to find modules/drivers. I have no idea if that will help solve your dilema though.

KevinADC 192 Practically a Posting Shark

Are we supposed to know what gearman is?

KevinADC 192 Practically a Posting Shark

Question: is the directory you are walking on a local drive, or are you trying to scrape web pages at another site? That will determine how you do this.

He said in the first post that they are local files.

but this is 40,000 pages stored locally

KevinADC 192 Practically a Posting Shark

will give a list of all the htm(l) files in the given path and open and read them one at a time:

@ARGV = <path/to/files/*.htm*>;
while(<>){
    .......
}

how you parse each file depends on what you are searching for. There are a number of html parsing modules on CPAN that will probably work better than any parsing code you can write yourself.

KevinADC 192 Practically a Posting Shark
KevinADC 192 Practically a Posting Shark

start with LWP or LWP::Simple as for the links, search CPAN for linkextractor and you should find the module.

KevinADC 192 Practically a Posting Shark

The -A file test operator is good for what you are trying to do. It will list the files last accessed date in days since the program started (your perl program).

So something like:

my @files = grep {(-f) && (-A) > 10} </home/any/logs/*>;
print "$_\n" for @files;

The parenthesis are important in the above code so make sure to include them. -f tests if the file is a regular file before testing the last accessed date, and the glob </home/any/logs/*> slurps all files and folders in from the directory.

Edit: if you need to drill down through subfolders then File::Find can be used and the -f and -A file test operators used as necessary.

KevinADC 192 Practically a Posting Shark

emails can be viewed as plain text or as html encoded, and even if html encoded the end user can elect to see emails as plain text. You should decide which route to take, text or html encoded, and then work on a solution.

A side-note to this is that it really has nothing to do with perl or CGI, but how you elect to send the emails, as text or html.

KevinADC 192 Practically a Posting Shark
KevinADC 192 Practically a Posting Shark

To make your code a bit more readable, these constructs:

($_ =~ /Deny/)

can be simplified to:

(/Deny/)

perl automatically binds regexps to $_. You have to actually bind regexps to any other scalar variables using the binding operators, like =~, !~, etc:

if ($foo =~ /foo/)

many perl programmers consider it bad to use $_ too much, named variables can make code easier to understand, but it your code it would be OK since the usage is rather obvious.

If you think using named variabls is not so important, that only indicates you have not written enough perl programs yet. ;)

KevinADC 192 Practically a Posting Shark

Because $msg is scoped to just the one block of code (sendMail), declare the object with 'my' when you create it:

my $msg = new MIME::Lite;

KevinADC 192 Practically a Posting Shark

first thing you must do, is start using the strict pragma:

use strict;

you already have "use warnings;" which would have been my next suggestion.

This line in your code is wrong:

if($lastline = $_)

You assign the value of $_ to $lastline, what you mean to do is check to see if they are the same, for that you would use a regexp (to check for patterns) or a string operator, probably "eq", to check for equivalence:

if($lastline eq $_)

KevinADC 192 Practically a Posting Shark

There are a lot of logfile parsers alreay written on CPAN. I suggest you search there and see if there is one for the particular log file you are working with. If not, you need to tackle your parsing issues one problem at a time.

The code you posted will only print lines containing the substrings "up" or "down".

KevinADC 192 Practically a Posting Shark

OK, what have you tried so far?

KevinADC 192 Practically a Posting Shark

Whats the title of the book you are using from the Library?

KevinADC 192 Practically a Posting Shark

try this, assumes @array is populated correctly:

foreach $string (@array){
   @blah = split(/\W+/,$string);
   $count += @blah;
   print $count;
}

What is the title of the book?

KevinADC 192 Practically a Posting Shark

Is this school/class/course work? Be more specific about what you are having trouble with instead of just posting your code and saying "its not working" or "I am having trouble".

This whole block of code is just totally wrong for various reasons:

foreach $string (@array){
$blah=shift($string);
$blah=split(/\W+/);
$count+=blah;
print $count;
}

Add this line to the top of your script:

use warnings;
KevinADC 192 Practically a Posting Shark

This thread is three years old surinder oberoi and this forum is for asking perl questions.

KevinADC 192 Practically a Posting Shark

Not exactly like that but that is the general idea.

Pseudo code:

here print all html haedrs and any stuff that gets printed before opening the file.

open file

while (<>){
here print only the stuff that gets generated from reading the file
}

close file

print the ending html stuff here

KevinADC 192 Practically a Posting Shark

You have to generate your output while the file is opened and you are processing the data line by line. You generate the output after the file is closed, so naturally you will only ever get the last line of the file. A generic example of one way to do it:

print "<table>";
while (<INPUT_FROM_FILE>) {
  # do some stuff here with your data like split() and calculating averages, etc.
  print "<tr><td>$yourdata</td></tr>\n";
}
print "</table>";
KevinADC 192 Practically a Posting Shark

Okay, I've changed that. But I really need to figure out how to do the things I initially asked about...

OK, a generic example:

my $new_var = param('foo');
open (FH, "file.txt");
while(<FH>) {
    my ($foo,$bar) = split(':::');
    my $average = ($foo+$new_var)/2;
    print $average;
}
KevinADC 192 Practically a Posting Shark

The original question is nearly 2 months old now, I doubt the OP is still interested in replies, but maybe.

KevinADC 192 Practically a Posting Shark

Don't bother with heredocs, they are just too perl 4, use an appropriate quote operator like qq:

#generate page
print qq{
<html>
<head><title>Current Cowboys Statistics</title></head>
<body bgcolor="#CCCCCC">
<font size="3">Week $weekNum :<br />Played at: $whereAt<br /></font>
</body></html>
};
KevinADC 192 Practically a Posting Shark

Not only is the code not complete, it has a number of errors. Fix the errors before continuing.

KevinADC 192 Practically a Posting Shark

You need to clarify your question and post any code you have written so far.

KevinADC 192 Practically a Posting Shark

In this case they are for all practical purposes equal:

use Benchmark qw/timethese cmpthese/;
my $string = "    Mary had a little lamb.   ";

my  $results = timethese(200000, 
        {
            'First' => sub {$string =~ s/^\s*//;$string =~ s/\s*$//;},
            'Second' => sub {$string =~ s/^\s*(.+)\s*$/$1/;}
        },
    );
cmpthese( $results ) ;

output:

Benchmark: timing 200000 iterations of First, Second...
     First:  3 wallclock secs ( 2.36 usr +  0.00 sys =  2.36 CPU) @ 84745.76/s (n=200000)
    Second:  2 wallclock secs ( 2.58 usr +  0.00 sys =  2.58 CPU) @ 77519.38/s (n=200000)
          Rate Second  First
Second 77519/s     --    -9%
First  84746/s     9%     --

I would still recommend the two regexp solution as a force of good habit though.

KevinADC 192 Practically a Posting Shark

Maybe try a Benchmark test

KevinADC 192 Practically a Posting Shark

more efficient suggestion:

my $string = '  Mary had a little lamb.  ';
$string =~ s/^\s+//; #remove leading spaces
$string =~ s/\s+$//; #remove trailing spaces
print $string;

use the Email::Valid module to check email addresses

http://search.cpan.org/~rjbs/Email-Valid-0.176/lib/Email/Valid.pm

KevinADC 192 Practically a Posting Shark

If you are running the script as a CGI from a browser of course you will not see the html tags. You will see them in the source code,

KevinADC 192 Practically a Posting Shark

oops, looks like you need to use those numbered keys too, like: '1951626474'

print $xml_ref->{Photos}->[0]->{photo}->{1951626474}->{ownner};

KevinADC 192 Practically a Posting Shark

Your output looks like a hash of array of hashes. Try this

print $xml_ref->{Photos}->[0]->{photo}->{ownner};
print $xml_ref->{Photos}->[0]->{photo}->{isfriend};
print $xml_ref->{Photos}->[0]->{photo}->{ispublic};
etc
etc
etc

KevinADC 192 Practically a Posting Shark

What are the two sets of

(':::');

for?

the ':::' is the file delimiter. So your lines might look like this when stored to disk:

1:::3:::4:::23:::7:::12:::55

Later when you read the file you split each line using ':::' and store the individual fields in a list like I already showed you.

How you go about adding/averaging is up to you. You an use sepreate files (for each week) and you would open and read each file and add/average the fields that you want.

KevinADC 192 Practically a Posting Shark

I suggest you use a single line to hold all the data:

#save to file
open(OUTFILE, ">>","nflStats.txt")
	or die "Error opening nflStats.txt $!, stopped";
print OUTFILE join(':::',
                           $whichWeek, $whereAt, $yourTmScore, $otherTmScore,
                           $passYrdPGame, $rushYrdPGame, $sacks, $sacksAllowed, 
                           $turnovForcedPerGame, $turnovAllowedPGame, $tds, $tdsAllowed), "\n";
close(OUTFILE);

Then you open the file and split each line into a list of variables:

#calculate stats
open(INFILE, "<", "nflStats.txt");
while(<INFILE>) {
   my ($whichWeek, $whereAt, $yourTmScore, $otherTmScore,
          $passYrdPGame, $rushYrdPGame, $sacks, $sacksAllowed, 
          $turnovForcedPerGame, $turnovAllowedPGame, $tds, $tdsAllowed) = split(':::');
  ### do whatever you want with each variable now
}
close INFILE;
KevinADC 192 Practically a Posting Shark

read the file however you want, get the data you want into variables and use the appropriate operators to do the data munging. Where are you stuck?

KevinADC 192 Practically a Posting Shark

I suggest you test those two pages on as many machines as possile. Check your server logs too.

KevinADC 192 Practically a Posting Shark

Impossible to say. May have nothing to do with the browser. Third party software, proxies. firewalls, etc, anyone of them might have something to do with any particular problem.

KevinADC 192 Practically a Posting Shark

is this school work?

KevinADC 192 Practically a Posting Shark

Hi Chris,

try this on your 1and1 server:

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Works";

it should work. The CGI module does not have to be loaded to run a CGI script.

KevinADC 192 Practically a Posting Shark

I have no experience with REST but there are a lot of REST modules listed on CPAN:

http://search.cpan.org/search?query=REST&mode=all


maybe you can check through them and see if one or more will be helpful for you.

KevinADC 192 Practically a Posting Shark

Did my suggestion work?

KevinADC 192 Practically a Posting Shark

BTW, is this school work?

KevinADC 192 Practically a Posting Shark

search on script archive sites like www.hotscripts.com for "file manager" scripts.

KevinADC 192 Practically a Posting Shark

$clob looks like a reference to a scalar, so try this:

print ${$clob},", $str, $num\n";

KevinADC 192 Practically a Posting Shark

I have a feeling there is a module or two that already does this type of stuff, search CPAN for 'matrix' and you may find some useful modules.

Not well tested:

use strict;
use warnings;
open (IN, '<',"alpha_carbon.dat") or die "Could not find file\n";
my $A = <IN>;#get first line of data
chomp($A);
while ($A && (my $B = <IN>)) {
   chomp $B;
   my @A = split(/\s+/,$A);
   my @B = split(/\s+/,$B);
   my $distance = sqrt( ($A[2] - $B[2]) ** 2 +
                        ($A[3] - $B[3]) ** 2 +
                        ($A[4] - $B[4]) ** 2
                       );
   if ($distance > 4.3){
      print "$A[0],$B[0] distance = $distance,\n";
   }
   $A = $B;
}
close IN;
KevinADC 192 Practically a Posting Shark

works OK for me.