Re: breaking-apart and printing an input file

2009-02-20 Thread pablo
 
 Your data is perfect for the paragraph mode* of readline:
 
Ahh, I forgot about paragraph mode.

 #!/usr/bin/perl
 
 use strict;
 use warnings;
 
 my $user  = shift;
 my $regex = qr/\A$user/;
 
 local $/ = '';
 while (my $record = DATA) {
   chomp $record;
   if ($record =~ /$regex/) {
   print $record\n;
   last;

This didn't work at first.  Then I realized it was because the file's
lines were terminated with \r\n instead of just \n.  Removing the
\r fixed the problem and it worked.

thanks for the help.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




breaking-apart and printing an input file

2009-02-19 Thread pablo
I have a single input file with entries as follows:

--snip--
USER1 [20090101] note
bla bla bla
bla bla bla

USER2 [20090104] note
bla bla bla
bla bla bla

--snip--

What I'm trying to do is create a single-argument script which displays
all entries for a given user.

So calling it as 'filter.pl user1' will print all entries for USER1.


Here is what I've got so far:

---
my $user = shift;

# slurp file
my $file = do {
open my $fh, 'inputfile.txt' or die $!;
local $/;
$fh;
};

if ( $file =~ /(^$user.*?)^$/smg ) {
print $1;
}
---

Here's my thinking behind the non-functional regex.
It matches a set of lines starting with a line which begins with $user,
followed by all non-empty lines, and terminated with an empty line (^$).

Running my script produces zero output.  My questions are:
1. Is there a simpler way to approach/accomplish this?
2. If not, what's wrong with my regex?



Thanks in advance for any help.

/P

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: breaking-apart and printing an input file

2009-02-19 Thread Rob Dixon
pa...@compugenic.com wrote:
 I have a single input file with entries as follows:
 
 --snip--
 USER1 [20090101] note
 bla bla bla
 bla bla bla
 
 USER2 [20090104] note
 bla bla bla
 bla bla bla
 
 --snip--
 
 What I'm trying to do is create a single-argument script which displays
 all entries for a given user.
 
 So calling it as 'filter.pl user1' will print all entries for USER1.
 
 
 Here is what I've got so far:
 
 ---
 my $user = shift;
 
 # slurp file
 my $file = do {
 open my $fh, 'inputfile.txt' or die $!;
 local $/;
 $fh;
 };
 
 if ( $file =~ /(^$user.*?)^$/smg ) {
 print $1;
 }
 ---
 
 Here's my thinking behind the non-functional regex.
 It matches a set of lines starting with a line which begins with $user,
 followed by all non-empty lines, and terminated with an empty line (^$).
 
 Running my script produces zero output.  My questions are:
 1. Is there a simpler way to approach/accomplish this?
 2. If not, what's wrong with my regex?
 
 
 
 Thanks in advance for any help.

I don't kno what results you're getting, but this works fine for me. The only
change I would make is to change the 'if' to a 'while', otherwise only the first
matching record will be found.

There may be an issue with reading the entire file if it is at all large, in
which case I recommend reading in 'paragraph' mode by setting the input record
separator to the null string

  local $/ = ;

after which each read from the file will return a single complete multi-line
record, terminated by a blank line.

HTH,

Rob



use strict;
use warnings;

my $user = 'USER1';

# slurp file
my $file = do {
local $/;
DATA;
};

if ( $file =~ /(^$user.*?)^$/smg ) {
  print $1;
}

__DATA__
USER1 [20090101] note
bla bla bla
bla bla bla

USER2 [20090104] note
bla bla bla
bla bla bla




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: breaking-apart and printing an input file

2009-02-19 Thread Chas. Owens
On Thu, Feb 19, 2009 at 22:42,  pa...@compugenic.com wrote:
 I have a single input file with entries as follows:

 --snip--
 USER1 [20090101] note
 bla bla bla
 bla bla bla

 USER2 [20090104] note
 bla bla bla
 bla bla bla

 --snip--

 What I'm trying to do is create a single-argument script which displays
 all entries for a given user.

 So calling it as 'filter.pl user1' will print all entries for USER1.
snip

Your data is perfect for the paragraph mode* of readline:

#!/usr/bin/perl

use strict;
use warnings;

my $user  = shift;
my $regex = qr/\A$user/;

local $/ = '';
while (my $record = DATA) {
chomp $record;
if ($record =~ /$regex/) {
print $record\n;
last;
}
}

__DATA__
USER1 [20090101] note
bla bla bla
USER2 loves this user

USER2 [20090104] note
bla bla bla
bla bla bla

USER3 [20090107] note
bla bla bla
bla bla bla


* http://perldoc.perl.org/perlvar.html#$/

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Searching text file and printing to new file

2008-01-18 Thread Rob Dixon

Gunnar Hjalmarsson wrote:

[EMAIL PROTECTED] wrote:

On Jan 14, 5:08 pm, [EMAIL PROTECTED] (Gunnar Hjalmarsson) wrote:

[EMAIL PROTECTED] wrote:

I have a large text file with
information essentially broken into lines like this:

findable text with a regexp
information I care about
more findable text

There are plenty of sections like this in the file. How can I write a
program that opens the file then searches for the middle line and
prints it to a new file?


 open my $IN,  '', 'infile.txt' or die $!;
 open my $OUT, '', 'outfile.txt' or die $!
 while ( $IN ) {
 print $OUT scalar $IN if /^findable/;
 }


OK here's what I've got:

#!/usr/local/bin/perl
use strict;
use warnings;

open (OUT, output.txt) or die Couldn't open output: $!;
open (IN, input.txt) or die Couldn't open input: $!;

while (IN) {
print OUT $_;
}

close OUT;
close IN;

This (obviously) copies the whole text file. How can I select only
certain lines to copy based on either the line above or below it?


I suggested a solution. Why do you ignore my suggestion and repeat your 
question?


Your suggestion didn't tell him how to solve his problem Gunnar.

A construct like the program below may help.

Rob


use strict;
use warnings;

my $wanted;

while (DATA) {
  if (/findable text with a regexp/) {
$wanted = 1;
  }
  elsif (/more findable text/) {
$wanted = undef;
  }
  elsif ($wanted) {
print;
  }
}

__DATA__
junk
junk
junk
findable text with a regexp
information I care about
more findable text
junk
junk
junk

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Searching text file and printing to new file

2008-01-18 Thread Gunnar Hjalmarsson

Rob Dixon wrote:

Gunnar Hjalmarsson wrote:

[EMAIL PROTECTED] wrote:

On Jan 14, 5:08 pm, [EMAIL PROTECTED] (Gunnar Hjalmarsson) wrote:


 open my $IN,  '', 'infile.txt' or die $!;
 open my $OUT, '', 'outfile.txt' or die $!
 while ( $IN ) {
 print $OUT scalar $IN if /^findable/;
 }


OK here's what I've got:


code that copies the whole file snipped


This (obviously) copies the whole text file. How can I select only
certain lines to copy based on either the line above or below it?


I suggested a solution. Why do you ignore my suggestion and repeat 
your question?


Your suggestion didn't tell him how to solve his problem Gunnar.


Of course it did (and still does).

C:\hometype test.pl
while ( DATA ) {
print scalar DATA if /^findable/;
}

__DATA__
junk
junk
findable text with a regexp
information I care about
more findable text
junk
junk
findable text with a regexp
more information I care about
more findable text
junk
junk

C:\hometest.pl
information I care about
more information I care about

C:\home


A construct like the program below may help.


another solution snipped

Sure, that would work too.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Searching text file and printing to new file

2008-01-18 Thread Tri Trinh
On Jan 18, 10:27 am, [EMAIL PROTECTED] (Rob Dixon) wrote:
 Gunnar Hjalmarsson wrote:
  [EMAIL PROTECTED] wrote:
  On Jan 14, 5:08 pm, [EMAIL PROTECTED] (Gunnar Hjalmarsson) wrote:
  [EMAIL PROTECTED] wrote:
  I have a large text file with
  information essentially broken into lines like this:

  findable text with a regexp
  information I care about
  more findable text

  There are plenty of sections like this in the file. How can I write a
  program that opens the file then searches for the middle line and
  prints it to a new file?

   open my $IN,  '', 'infile.txt' or die $!;
   open my $OUT, '', 'outfile.txt' or die $!
   while ( $IN ) {
   print $OUT scalar $IN if /^findable/;
   }

  OK here's what I've got:

  #!/usr/local/bin/perl
  use strict;
  use warnings;

  open (OUT, output.txt) or die Couldn't open output: $!;
  open (IN, input.txt) or die Couldn't open input: $!;

  while (IN) {
  print OUT $_;
  }

  close OUT;
  close IN;

  This (obviously) copies the whole text file. How can I select only
  certain lines to copy based on either the line above or below it?

  I suggested a solution. Why do you ignore my suggestion and repeat your
  question?

 Your suggestion didn't tell him how to solve his problem Gunnar.

 A construct like the program below may help.

 Rob

 use strict;
 use warnings;

 my $wanted;

 while (DATA) {
if (/findable text with a regexp/) {
  $wanted = 1;
}
elsif (/more findable text/) {
  $wanted = undef;
}
elsif ($wanted) {
  print;
}

 }

 __DATA__
 junk
 junk
 junk
 findable text with a regexp
 information I care about
 more findable text
 junk
 junk
 junk

Something like this would be a little more efficient with 1 check
statment.  I've also include File handle if you have multiple files.

#!/usr/bin/perl

use strict;
use warnings;

#put your open file here
#open (IF,  Some file) || die Can't open $!\n;

my $regmatch = something 123;

while (DATA){

print if (/$regmatch/);

}

#close (IF);

__DATA__
123
123
123
something 123


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Searching text file and printing to new file

2008-01-17 Thread PlagueMagazine
On Jan 14, 5:08 pm, [EMAIL PROTECTED] (Gunnar Hjalmarsson) wrote:
 [EMAIL PROTECTED] wrote:
  I'm a nearly absolute beginner to Perl,

 Then this site ought to be useful:http://learn.perl.org/

  and a lot of the text manipulation things confuse me.

 Really? Which things specifically, and in what way?

  I have a large text file with
  information essentially broken into lines like this:

  findable text with a regexp
  information I care about
  more findable text

  There are plenty of sections like this in the file. How can I write a
  program that opens the file then searches for the middle line and
  prints it to a new file?

 What have you tried so far?

 Example:

  open my $IN,  '', 'infile.txt' or die $!;
  open my $OUT, '', 'outfile.txt' or die $!
  while ( $IN ) {
  print $OUT scalar $IN if /^findable/;
  }

 --
 Gunnar Hjalmarsson
 Email:http://www.gunnar.cc/cgi-bin/contact.pl

OK here's what I've got:

#!/usr/local/bin/perl
use strict;
use warnings;

open (OUT, output.txt) or die Couldn't open output: $!;
open (IN, input.txt) or die Couldn't open input: $!;

while (IN) {
print OUT $_;
}

close OUT;
close IN;

This (obviously) copies the whole text file. How can I select only
certain lines to copy based on either the line above or below it?


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Searching text file and printing to new file

2008-01-17 Thread Gunnar Hjalmarsson

[EMAIL PROTECTED] wrote:

On Jan 14, 5:08 pm, [EMAIL PROTECTED] (Gunnar Hjalmarsson) wrote:

[EMAIL PROTECTED] wrote:

I have a large text file with
information essentially broken into lines like this:

findable text with a regexp
information I care about
more findable text

There are plenty of sections like this in the file. How can I write a
program that opens the file then searches for the middle line and
prints it to a new file?


 open my $IN,  '', 'infile.txt' or die $!;
 open my $OUT, '', 'outfile.txt' or die $!
 while ( $IN ) {
 print $OUT scalar $IN if /^findable/;
 }


OK here's what I've got:

#!/usr/local/bin/perl
use strict;
use warnings;

open (OUT, output.txt) or die Couldn't open output: $!;
open (IN, input.txt) or die Couldn't open input: $!;

while (IN) {
print OUT $_;
}

close OUT;
close IN;

This (obviously) copies the whole text file. How can I select only
certain lines to copy based on either the line above or below it?


I suggested a solution. Why do you ignore my suggestion and repeat your 
question?


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Searching text file and printing to new file

2008-01-14 Thread PlagueMagazine
I'm a nearly absolute beginner to Perl, and a lot of the text
manipulation things confuse me. I have a large text file with
information essentially broken into lines like this:

findable text with a regexp
information I care about
more findable text

There are plenty of sections like this in the file. How can I write a
program that opens the file then searches for the middle line and
prints it to a new file?


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Searching text file and printing to new file

2008-01-14 Thread Gunnar Hjalmarsson

[EMAIL PROTECTED] wrote:

I'm a nearly absolute beginner to Perl,


Then this site ought to be useful: http://learn.perl.org/


and a lot of the text manipulation things confuse me.


Really? Which things specifically, and in what way?


I have a large text file with
information essentially broken into lines like this:

findable text with a regexp
information I care about
more findable text

There are plenty of sections like this in the file. How can I write a
program that opens the file then searches for the middle line and
prints it to a new file?


What have you tried so far?

Example:

open my $IN,  '', 'infile.txt' or die $!;
open my $OUT, '', 'outfile.txt' or die $!
while ( $IN ) {
print $OUT scalar $IN if /^findable/;
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




problem printing contents of file in directory

2007-06-28 Thread alok nath
Hi,
Can anybody tell me why its not printing the contents of each
inside a particular folder ?
I lost all my hairs scratching my head.
Thanks
Alok.

my $tstToRunDir = C:\\perlScripts ;
my $fileInTstToRunDir ;

opendir TST2RUN, $tstToRunDir || die Failed to open $tstToRunDir $!\n ;
open RESULTS_LOG, ResultLog.log || die Failed to open log\n ;

while ($fileInTstToRunDir = readdir (TST2RUN)){
 chomp $fileInTstToRunDir ;
 #open the file and get connection description and test description
 open FILE_2RUN, $fileInTstToRunDir || die  Failed to open 
$fileInTstToRunDir:$!\n ;
 print File is : $fileInTstToRunDir\n ;
 next if($fileInTstToRunDir =~ m/^./ ) ;

 while( FILE_2RUN ){
  print $_ ;
  if ($_ =~ m/Test Description/ ){
   print $_ ;
  }
 } 
 
 print RESULTS_LOG File is : $fileInTstToRunDir\n ;
 #$count++  ;
# close FILE_2RUN ; 
 
}
close RESULTS_LOG ;
close TST2RUN ;


 

Sucker-punch spam with award-winning protection. 
Try the free Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/features_spam.html

Re: problem printing contents of file in directory

2007-06-28 Thread John W. Krahn

alok nath wrote:

Hi,


Hello,


Can anybody tell me why its not printing the contents of each
inside a particular folder ?


Yes I can, and so can Perl's documentation:

perldoc -f readdir



my $tstToRunDir = C:\\perlScripts ;
my $fileInTstToRunDir ;

opendir TST2RUN, $tstToRunDir || die Failed to open $tstToRunDir $!\n ;
open RESULTS_LOG, ResultLog.log || die Failed to open log\n ;


The logical or operator '||' has relatively high precedence so neither opendir 
nor open will die if an error is encountered.  You need to either use parentheses:


opendir( TST2RUN, $tstToRunDir ) || die Failed to open $tstToRunDir $!\n ;
open( RESULTS_LOG, ResultLog.log ) || die Failed to open log\n ;

or use the low precedence 'or' operator:

opendir TST2RUN, $tstToRunDir or die Failed to open $tstToRunDir $!\n ;
open RESULTS_LOG, ResultLog.log or die Failed to open log\n ;



while ($fileInTstToRunDir = readdir (TST2RUN)){
 chomp $fileInTstToRunDir ;


$fileInTstToRunDir comes directly from the file system so there is no reason 
to use chomp.




 #open the file and get connection description and test description
 open FILE_2RUN, $fileInTstToRunDir || die  Failed to open 
$fileInTstToRunDir:$!\n ;


Again, there is a precedence problem with the '||' operator.  The file name in 
$fileInTstToRunDir was obtained from the $tstToRunDir directory so you need to 
include the directory name in order to access it:


open FILE_2RUN, $tstToRunDir/$fileInTstToRunDir
or die  Failed to open $tstToRunDir/$fileInTstToRunDir:$!\n ;



 print File is : $fileInTstToRunDir\n ;
 next if($fileInTstToRunDir =~ m/^./ ) ;

 while( FILE_2RUN ){
  print $_ ;
  if ($_ =~ m/Test Description/ ){
   print $_ ;
  }
 } 
 
 print RESULTS_LOG File is : $fileInTstToRunDir\n ;

 #$count++  ;
# close FILE_2RUN ; 
 
}

close RESULTS_LOG ;
close TST2RUN ;



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: printing to a file if line is not there

2007-01-12 Thread FamiLink Admin

John,

Thanks for your help!  Chomp and replacing $_ with $site did it!

Below is the log if you think I can do it without so many splits that would 
be great!


2007.1.12 8:06:43 - 70.22.222.29 http://www.12gpatt.com/Nightlife.html 
*DENIED* Weighted phrase limit of 100 : 341 (live sex...cam+liveshow) GET 
143438


Ryan
- Original Message - 
From: John W. Krahn [EMAIL PROTECTED]

To: Perl Beginners beginners@perl.org
Sent: Thursday, January 11, 2007 3:55 PM
Subject: Re: printing to a file if line is not there



FamiLink Admin wrote:

Hello all.


Hello,


I am trying to read a log file then look for a website and score.  If
that website has a score 100, take it and add it to a Check me list.


It would be easier to help if we could see examples of valid and invalid 
log

file entries.


1. I don't want to recheck so I have a list of checked sites that I
   want to verify with.
2. I don't want duplicates added to the check me list.

Below is what I have.  I have 1 above working but I cannot get the
duplicates from printing to the list.  The sub testfordup at the bottom
will print the proper information but if it is in the log again it will
print it again.

Also, is the a better way of not printing a line if the line exists in
the file?

I am also getting:
Use of uninitialized value in hash element at line 60.
and
Use of uninitialized value in regexp compilation at line 44,
AB_EX_FILE line 14.

I think this is because the text is www.site.com an the . is not being
read as text in the file.

Any help would be greatly appreciated.


#!/usr/bin/perl -w
use strict;
my $ab_file = 'autobannedsitelist1';
my $ab_ex_file = 'autobannedsitelistexceptlst';
my $log_file = 'access.log';

open ( LOG_FILE, -|, tail -n 10 $log_file ) or die No log file
exists.\n $! \n;
{
# Begin parsing of log data
my $pass = 0;
my $dup = 0;
foreach my $logfile_line (LOG_FILE) {


You are using a foreach loop which means that 100,000 lines of the log 
file
are stored in memory.  It would be more efficient to use a while loop 
which

will only store a single line at a time in memory.



   $logfile_line =~ s/GET//;
   if ($logfile_line =~ m/Weighted/ ){
   my @logfile_fields = split /\s+/, $logfile_line, 6;
   my $site = (split /\//, ($logfile_fields[4]))[2]; # get the
domain name
   my $sitemain = (split '\.', $site)[-2]; # get the site name
without .com
   my $sitemain2 = (split '\.', $site)[-3]; # get the site name
without .co.ku
   my $points = (split ' ',(split ':',
(substr($logfile_fields[5],35,10)))[1])[0];


You are using split() a lot.  It may be more efficient to use a single 
regular

expression but it is hard to tell without seeing actual data.



   if ($points  100){
   $pass = abextest($sitemain,$sitemain2);


You shouldn't use the '' sigil with subroutines unless you really have 
to.


perldoc perlsub



   if ( $pass eq 0 ){


You are using a string comparison operator on a number which means that 
perl

has to convert the number to a string:

   if ( $pass == 0 ){



   testfordup($site);
   }
   }
   }
   }
close (LOG_FILE);
}

sub abextest {
my ($sitemain,$sitemain2)[EMAIL PROTECTED];
my $pass = 0;
open ( AB_EX_FILE, , $ab_ex_file ) or die Can't write
AUTOBANNED_EX_FILE: $!;
foreach my $line (AB_EX_FILE){


You are using a foreach loop which means that all of the file
'autobannedsitelistexceptlst' is stored in memory.  It would be more 
efficient
to use a while loop which will only store a single line at a time in 
memory.




if (($line =~ m/$sitemain/i ) || ( $line =~
m/$sitemain2/i ))  {


It looks like this *may* be line 44?  If so then either $sitemain or
$sitemain2 is undefined.



$pass = 1;
}
}
close (AB_EX_FILE);
return $pass;
}

sub testfordup {
my %seen;
open AB_FILE,   $ab_file or die Can't read AUTOBANNED_FILE: $!;
while (AB_FILE) { $seen{$_} = 1 }  # build the hash of seen lines
close AB_FILE;

my ($site) = @_;
open AB_FILE,  $ab_file or die Can't append AUTOBANNED_FILE: 
$!;

print AB_FILE $site\n if not ($seen{$_}++) ;


It looks like this *may* be line 60?  If so then you have not explicitly
stored a value in $_ so it is probably undefined.  Perhaps you meant to 
use
$seen{$site} instead?  But that probably wouldn't work either as you 
didn't

chomp() the data before you added it to %seen.



close (AB_FILE);
return;
}



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: printing to a file if line is not there

2007-01-12 Thread John W. Krahn
FamiLink Admin wrote:
 John,
 
 Thanks for your help!  Chomp and replacing $_ with $site did it!
 
 Below is the log if you think I can do it without so many splits that
 would be great!
 
 2007.1.12 8:06:43 - 70.22.222.29 http://www.12gpatt.com/Nightlife.html
 *DENIED* Weighted phrase limit of 100 : 341 (live sex...cam+liveshow)
 GET 143438

Based upon that single example I came up with this (YMMV):

while ( LOG_FILE ) {
next unless ( my $site ) = m!http://([^/]+)/!
and ( my $points ) = /Weighted.+?:\s*(\d+)/;

next if $points = 100;

# your original code only indicated using the TLDs .com and .co.ku
my ( $sitemain ) = $site =~ /([^.]+)\.co(?:m|\.ku)$/;

testfordup( $site ) unless abextest( $sitemain );
}


It may also be more efficient to use a tied DBM hash instead of a plain text
file for $ab_file:

perldoc AnyDBM_File



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




printing to a file if line is not there

2007-01-11 Thread FamiLink Admin

Hello all.

I am trying to read a log file then look for a website and score.  If that 
website has a score 100, take it and add it to a Check me list.


1. I don't want to recheck so I have a list of checked sites that I want 
to verify with.

2. I don't want duplicates added to the check me list.

Below is what I have.  I have 1 above working but I cannot get the 
duplicates from printing to the list.  The sub testfordup at the bottom will 
print the proper information but if it is in the log again it will print it 
again.


Also, is the a better way of not printing a line if the line exists in the 
file?


I am also getting:
Use of uninitialized value in hash element at line 60.
and
Use of uninitialized value in regexp compilation at line 44, AB_EX_FILE 
line 14.


I think this is because the text is www.site.com an the . is not being 
read as text in the file.


Any help would be greatly appreciated.


#!/usr/bin/perl -w
use strict;
my $ab_file = 'autobannedsitelist1';
my $ab_ex_file = 'autobannedsitelistexceptlst';
my $log_file = 'access.log';

open ( LOG_FILE, -|, tail -n 10 $log_file ) or die No log file 
exists.\n $! \n;

{
# Begin parsing of log data
my $pass = 0;
my $dup = 0;
 foreach my $logfile_line (LOG_FILE) {
   $logfile_line =~ s/GET//;
   if ($logfile_line =~ m/Weighted/ ){
   my @logfile_fields = split /\s+/, $logfile_line, 6;
   my $site = (split /\//, ($logfile_fields[4]))[2]; # get the domain 
name
   my $sitemain = (split '\.', $site)[-2]; # get the site name without 
.com
   my $sitemain2 = (split '\.', $site)[-3]; # get the site name without 
.co.ku
   my $points = (split ' ',(split ':', 
(substr($logfile_fields[5],35,10)))[1])[0];

   if ($points  100){
   $pass = abextest($sitemain,$sitemain2);
   if ( $pass eq 0 ){
   testfordup($site);
   }
   }
   }
 }
close (LOG_FILE);
}

sub abextest {
   my ($sitemain,$sitemain2)[EMAIL PROTECTED];
   my $pass = 0;
   open ( AB_EX_FILE, , $ab_ex_file ) or die Can't write 
AUTOBANNED_EX_FILE: $!;

   foreach my $line (AB_EX_FILE){

   if (($line =~ m/$sitemain/i ) || ( $line =~ 
m/$sitemain2/i ))  {

   $pass = 1;
   }
   }
   close (AB_EX_FILE);
return $pass;
}

sub testfordup {
   my %seen;
   open AB_FILE,   $ab_file or die Can't read AUTOBANNED_FILE: $!;
   while (AB_FILE) { $seen{$_} = 1 }  # build the hash of seen lines
   close AB_FILE;

   my ($site) = @_;
   open AB_FILE,  $ab_file or die Can't append AUTOBANNED_FILE: 
$!;

   print AB_FILE $site\n if not ($seen{$_}++) ;
   close (AB_FILE);
return;
}

Ryan 



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: printing to a file if line is not there

2007-01-11 Thread John W. Krahn
FamiLink Admin wrote:
 Hello all.

Hello,

 I am trying to read a log file then look for a website and score.  If
 that website has a score 100, take it and add it to a Check me list.

It would be easier to help if we could see examples of valid and invalid log
file entries.

 1. I don't want to recheck so I have a list of checked sites that I
want to verify with.
 2. I don't want duplicates added to the check me list.
 
 Below is what I have.  I have 1 above working but I cannot get the
 duplicates from printing to the list.  The sub testfordup at the bottom
 will print the proper information but if it is in the log again it will
 print it again.
 
 Also, is the a better way of not printing a line if the line exists in
 the file?
 
 I am also getting:
 Use of uninitialized value in hash element at line 60.
 and
 Use of uninitialized value in regexp compilation at line 44,
 AB_EX_FILE line 14.
 
 I think this is because the text is www.site.com an the . is not being
 read as text in the file.
 
 Any help would be greatly appreciated.
 
 
 #!/usr/bin/perl -w
 use strict;
 my $ab_file = 'autobannedsitelist1';
 my $ab_ex_file = 'autobannedsitelistexceptlst';
 my $log_file = 'access.log';
 
 open ( LOG_FILE, -|, tail -n 10 $log_file ) or die No log file
 exists.\n $! \n;
 {
 # Begin parsing of log data
 my $pass = 0;
 my $dup = 0;
 foreach my $logfile_line (LOG_FILE) {

You are using a foreach loop which means that 100,000 lines of the log file
are stored in memory.  It would be more efficient to use a while loop which
will only store a single line at a time in memory.


$logfile_line =~ s/GET//;
if ($logfile_line =~ m/Weighted/ ){
my @logfile_fields = split /\s+/, $logfile_line, 6;
my $site = (split /\//, ($logfile_fields[4]))[2]; # get the
 domain name
my $sitemain = (split '\.', $site)[-2]; # get the site name
 without .com
my $sitemain2 = (split '\.', $site)[-3]; # get the site name
 without .co.ku
my $points = (split ' ',(split ':',
 (substr($logfile_fields[5],35,10)))[1])[0];

You are using split() a lot.  It may be more efficient to use a single regular
expression but it is hard to tell without seeing actual data.


if ($points  100){
$pass = abextest($sitemain,$sitemain2);

You shouldn't use the '' sigil with subroutines unless you really have to.

perldoc perlsub


if ( $pass eq 0 ){

You are using a string comparison operator on a number which means that perl
has to convert the number to a string:

if ( $pass == 0 ){


testfordup($site);
}
}
}
}
 close (LOG_FILE);
 }
 
 sub abextest {
 my ($sitemain,$sitemain2)[EMAIL PROTECTED];
 my $pass = 0;
 open ( AB_EX_FILE, , $ab_ex_file ) or die Can't write
 AUTOBANNED_EX_FILE: $!;
 foreach my $line (AB_EX_FILE){

You are using a foreach loop which means that all of the file
'autobannedsitelistexceptlst' is stored in memory.  It would be more efficient
to use a while loop which will only store a single line at a time in memory.


 if (($line =~ m/$sitemain/i ) || ( $line =~
 m/$sitemain2/i ))  {

It looks like this *may* be line 44?  If so then either $sitemain or
$sitemain2 is undefined.


 $pass = 1;
 }
 }
 close (AB_EX_FILE);
 return $pass;
 }
 
 sub testfordup {
 my %seen;
 open AB_FILE,   $ab_file or die Can't read AUTOBANNED_FILE: $!;
 while (AB_FILE) { $seen{$_} = 1 }  # build the hash of seen lines
 close AB_FILE;
 
 my ($site) = @_;
 open AB_FILE,  $ab_file or die Can't append AUTOBANNED_FILE: $!;
 print AB_FILE $site\n if not ($seen{$_}++) ;

It looks like this *may* be line 60?  If so then you have not explicitly
stored a value in $_ so it is probably undefined.  Perhaps you meant to use
$seen{$site} instead?  But that probably wouldn't work either as you didn't
chomp() the data before you added it to %seen.


 close (AB_FILE);
 return;
 }


John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Printing output to file

2004-11-13 Thread Chasecreek Systemhouse
On Sat, 13 Nov 2004 14:48:06 +0800 (CST), Stephen Liu [EMAIL PROTECTED] wrote:
   $ perl AAA.pl  /pathto/name_of_file
  
   is it possible adding print file function to the
   script


Maybe this example would be of benefit?
http://backpan.cpan.org/authors/id/S/SN/SNEEX/accts.sx

Reads a file and outputs a file.  it's pretty old but it gets the job
done and if an example of what you want:  Reading and Writing.

Also, please research:
http://perldoc.com/perl5.8.4/pod/perlfaq.html


HTH  =)

-- 
WC -Sx- Jones
http://insecurity.org/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Printing output to file

2004-11-12 Thread Stephen Liu
Hi folks,

Instead of using following command printing output to
a file;

$ perl AAA.pl  /pathto/name_of_file

is it possible adding print file function to the
script

1) If YES
Please advise how to make it.

2) Can I add following bash syntax to the script
user=$(whoami);
now=$(date +%Y.%m.%d.%R);
File=/tmp/satimis/comparison_${user}_${now}.txt; 

so that the printout file will carry name of creator,
date and time, etc.

If possible please shed me some light to re-edit the
script.

TIA

B.R.
Stephen Liu

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Printing output to file

2004-11-12 Thread Renqilong
On Sat, 13 Nov 2004 11:42:13 +0800 (CST)
Stephen Liu [EMAIL PROTECTED] wrote:

 Hi folks,
 
 Instead of using following command printing output to
 a file;
 
 $ perl AAA.pl  /pathto/name_of_file
 
 is it possible adding print file function to the
 script
 
 1) If YES
 Please advise how to make it.
YES:
open FL,/pathto/name_of_file
print FL WHATEVERYOUWANT
 2) Can I add following bash syntax to the script
 user=$(whoami);
you can use:$user = `whoami`;
 now=$(date +%Y.%m.%d.%R);
 File=/tmp/satimis/comparison_${user}_${now}.txt; 
 
 so that the printout file will carry name of creator,
 date and time, etc.
 
 If possible please shed me some light to re-edit the
 script.
 
 TIA
 
 B.R.
 Stephen Liu
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/ http://learn.perl.org/first-response
 
 
 

PS:maybe you should post your code first
-- 
Whatever you do will be insignificant,but 
the important is you do it!


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Printing output to file

2004-11-12 Thread Stephen Liu
Hi Renqilong,

Tks for your advice.

 --- Renqilong [EMAIL PROTECTED] wrote: 

  Instead of using following command printing output
 to
  a file;
  
  $ perl AAA.pl  /pathto/name_of_file
  
  is it possible adding print file function to the
  script
  
  1) If YES
  Please advise how to make it.
 YES:
 open FL,/pathto/name_of_file
 print FL WHATEVERYOUWANT

Noted with thanks.

  2) Can I add following bash syntax to the script
  user=$(whoami);
 you can use:$user = `whoami`;

Noted.

  now=$(date +%Y.%m.%d.%R);
  File=/tmp/satimis/comparison_${user}_${now}.txt;
 
  
  so that the printout file will carry name of
 creator,
  date and time, etc.
  
  If possible please shed me some light to re-edit
 the
  script.

 PS:maybe you should post your code first

Script:-

#!/usr/bin/perl -w
#use strict;
use warnings;

format   STDOUT_TOP=
  ORGINAL MISTAKE  LINENOWORDNO
.
format   STDOUT_MID=
  @ @  @##   @##
  $org,   $mis,$lno1,$wno
.

$orgfile='doc_a.txt';
open(INFO,$orgfile)|| die 'cannot open $orgfile';
@basedoc=INFO;
close(INFO);
$typfile='doc_b.txt';
open(INFO1,$typfile) || die 'cannot open $typfile';
@typedoc=INFO1;
close(INFO1);

$~=STDOUT_TOP;
write();

$lno=0;
$wno=0;

foreach $i (@typedoc)
{
   $incr=0;
  @typedoc1=split(/ /,$i);

   @basedoc1=split(/ /,$basedoc[$lno]);

  foreach $k (@typedoc1)
  {
$var=$basedoc1[$incr];

 if ( $var ne $k )
  {
 $org=$var;
 $mis=$k;
 $wno=$incr+1;
 $lno1=$lno+1;
 $~=STDOUT_MID;
  write();
  }
  $incr++;
  }
   $lno++;
}
- END of SCRIPT -

B.R.
Stephen

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Printing to a file

2004-10-29 Thread Zeus Odin
I could see this thread coming 2 months ago. Does anyone have any pent-up
frustrations?
:-)

I think this list is the BEST list I have ever read ... not that I have read
many lists. However, any list can be improved. The improvements I would
suggest have to do with editorializing and patience:

First, editorializing--definition here
http://www.m-w.com/cgi-bin/dictionary?book=Dictionaryva=editorialize. If
you find yourself making comments about code--after all, it's all about the
code--that is the purpose of this list. This is and should be supported ad
infinitum and ad nauseum. On the other hand, if you find yourself making
comments about the poster in general, I think it is a sign of trouble. Who
is it for me to say that what one programmer tried in a day is not equal to
what a lesser programmer tried in a week?

Charles Clarkson, Randall Schwartz, and John Krahn (I'm only naming three.
Apologies to those I omitted. Whatever happened to Drieux?) are some of the
most respected lurkers in this forum. I try to read their posts whenever I
can. I am sure that a day of their effort would go a long way. But so what?
Not many can compare their abilities to the highly seasoned.

There are much differing abilities of all those that post to this forum. I
recently rewrote something I first wrote in Perl when I was just starting
out. The code went from 100 lines to only 20.

What I am trying to say is if you find yourself getting ready to post
something that is not talking explicitly about code, chances are it is
better left unsaid (unwritten). I think we should stick to: what did you
try; can I see your code; this is where your problem is; an alternate
solution is These are just my opinions. Yours might vary.

Second, patience. We should all be more patient. Keep in mind that this is a
global group. This forum is in English but English is not the native
language of many here. Many translate directly from their native tongue into
English. Sometimes desired nuances are lost or undesired ones gained. It can
be taken differently and offensively by some.

There have been many posts I have read here that have rubbed me the wrong
way. What did I do? I immediately moved on to the next post or thread.
Otherwise, you spend time editorializing. That's bad.

Everyone here is subject to criticism including me. If you don't like that,
then please do not post anything here. You are still free to read. When in
doubt, discretion is the better part of valor.

Just my 2 cents. I hope I didn't offend any as that was not my intent. Now
let's get back to business. Well, actually this is pleasure for me.

Thanks,
ZO

[EMAIL PROTECTED] wrote in message...
 I agree, Gunnar is mean.  Mean people suck!

 derek



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Printing to a file

2004-10-29 Thread John W. Krahn
Zeus Odin wrote:
Charles Clarkson, Randall Schwartz, and John Krahn (I'm only naming three.
You probably meant Randal Schwartz (one l.)   :-)
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



Re: Printing to a file

2004-10-29 Thread Zeus Odin
my $hero = 'John Krahn';
$hero =~ s/John Krahn/Jenda Krynicky/;

:-)

I can't believe I wrote what I thought was a thoughtful post that would
hopefully make perl.beginners a better place to frequent (good luck!) and
all I get is:

Dude, Randal is spelled with one L not two. Pay attention. I'm John Krahn.

Obviously, I'm kidding. I honestly did know that Randal was spelled with one
L but while typing, sometimes I can't see the forest for the trees.
Hopefully, next time I have tunnel vision, I will remember.

Keep the thoughtful and informative posts coming.
-ZO

John W. Krahn [EMAIL PROTECTED] wrote in message ...

  Charles Clarkson, Randall Schwartz, and John Krahn (I'm only naming
three.

 You probably meant Randal Schwartz (one l.)   :-)



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Printing to a file

2004-10-26 Thread Kevin Old
Hello everyone,

First, this is a basic problem, but I have looked at it for over an
hour and wasted time.  Now, I'm asking for help.

For some reason nothing is being printed to the MY filehandle. Can
someone see what I'm doing wrong?

#!/usr/bin/perl
  
+  
use warnings;
use strict;
use File::Find;

find sub {
return unless -f;
return unless $_ =~ /.\d+$/;
print $_\n;
#print $File::Find::name\n;
  
+  
open(SD, $_) or die can't open $_ $!\n;
#my $fh = IO::File-new( /tmp/savedata/new/$_) or die Coul
+dn't open new for writing: $! \n;
open(MY,  /tmp/savedata/new/$_) or die can't open new $_ $
+!\n;
  
+  
my @sd = SD;
foreach (@sd) {
if ( $_ =~ /40187378|40187233|40187230|4018722
+9|40187234|40187232|40186526/ ) {
#print match: $_\n;
my $line = $_;
print MY $line;
}
}
  
+  
close MY;
close SD;
}, .;


Thanks,
Kevin
-- 
Kevin Old
[EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Printing to a file

2004-10-26 Thread JupiterHost.Net

Kevin Old wrote:
Hello everyone,
First, this is a basic problem, but I have looked at it for over an
hour and wasted time.  Now, I'm asking for help.
For some reason nothing is being printed to the MY filehandle. Can
someone see what I'm doing wrong?
Is
 if ( $_ =~ /40187378|40187233|40187230|4018722
+9|40187234|40187232|40186526/ ) {
ever true?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



Re: Printing to a file

2004-10-26 Thread Brian Gunlogson
Put the MY in braces.

SO
print MY $line;

WOULD BECOME
print {MY} $line;

--- Kevin Old [EMAIL PROTECTED] wrote:

 Hello everyone,
 
 First, this is a basic problem, but I have looked at it for over an
 hour and wasted time.  Now, I'm asking for help.
 
 For some reason nothing is being printed to the MY filehandle. Can
 someone see what I'm doing wrong?
 print MY $line;




__
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
http://promotions.yahoo.com/new_mail 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Printing to a file

2004-10-26 Thread Gunnar Hjalmarsson
Kevin Old wrote:
First, this is a basic problem, but I have looked at it for over an
hour and wasted time.  Now, I'm asking for help.
Just an hour, and you think it's already justified to waste hundreds 
of other person's time?

I disagree. You need to be a lot more patient if you want to learn 
programming.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



Re: Printing to a file

2004-10-26 Thread Kevin Old
On Tue, 26 Oct 2004 10:57:39 -0500, JupiterHost.Net
[EMAIL PROTECTED] wrote:
 
 
 Kevin Old wrote:
 
  Hello everyone,
 
  First, this is a basic problem, but I have looked at it for over an
  hour and wasted time.  Now, I'm asking for help.
 
  For some reason nothing is being printed to the MY filehandle. Can
  someone see what I'm doing wrong?
 
 Is
 
   if ( $_ =~ /40187378|40187233|40187230|4018722
 +9|40187234|40187232|40186526/ ) {
 
 ever true?

Yes, it is true, lots and lots of lines.  I tried changing:

print MY $line;
to 
print {MY} $line;

and I get an error when using strict.

The weird thing is, that when I change the open statement from:

open(MY,  /tmp/savedata/new/$_) or die can't open new $_ $!\n;
to 
open(MY,  /tmp/savedata/new/$_) or die can't open new $_ $!\n;

and append instead of write to the file, everything works.

Any ideas?

Kevin
-- 
Kevin Old
[EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Printing to a file

2004-10-26 Thread Bob Showalter
Brian Gunlogson wrote:
 Put the MY in braces.
 
 SO
 print MY $line;
 
 WOULD BECOME
 print {MY} $line;

Why? That fails under use strict, and is totally unecessary.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Printing to a file

2004-10-26 Thread Kevin Old
On Tue, 26 Oct 2004 19:17:11 +0200, Gunnar Hjalmarsson
[EMAIL PROTECTED] wrote:
 Kevin Old wrote:
  First, this is a basic problem, but I have looked at it for over an
  hour and wasted time.  Now, I'm asking for help.
 
 Just an hour, and you think it's already justified to waste hundreds
 of other person's time?
 
 I disagree. You need to be a lot more patient if you want to learn
 programming.

Wait a minute there.  I'm not learning programming.  I've been a perl
programmer for 7 years and have written hundreds of more complex
programs than this and have written to thousands of files in my time. 
I just needed another pair of eyes to possibly point out what I was
doing wrong.

Also, posting to this list is not wasting hundreds of other people's
time.  It's a discussion list.  If you're looking at my message it
means that you're either seeking to help or seeking help.  Therefore,
you are choosing to devote a few minutes of your time to help or be
helped.

Sorry my post rubbed you the wrong way.

Kevin
-- 
Kevin Old
[EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Printing to a file

2004-10-26 Thread Chap Harrison
Wait a minute there.  I'm not learning programming.  I've been a perl
programmer for 7 years and have written hundreds of more complex
programs than this and have written to thousands of files in my time.
I just needed another pair of eyes to possibly point out what I was
doing wrong.
Also, posting to this list is not wasting hundreds of other people's
time.  It's a discussion list.  If you're looking at my message it
means that you're either seeking to help or seeking help.  Therefore,
you are choosing to devote a few minutes of your time to help or be
helped.
Sorry my post rubbed you the wrong way.
Ah-freakin'-men.  Gunnar, what the *hell*?  I've been reading this list 
for a couple of months now and the majority of your posts seem to be 
little more than chiding people in an extremely unpleasant tone.  Is 
that your job?  Do you think it's consistent with this list's claim to 
be a place where beginners can ask questions in a friendly environment? 
 Come on.  Try being civil, please.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



Re: Printing to a file

2004-10-26 Thread Steve Bertrand
 On Tue, 26 Oct 2004 10:57:39 -0500, JupiterHost.Net
 [EMAIL PROTECTED] wrote:


 Kevin Old wrote:

  Hello everyone,
 
  First, this is a basic problem, but I have looked at it for over
 an
  hour and wasted time.  Now, I'm asking for help.
 
  For some reason nothing is being printed to the MY filehandle. Can
  someone see what I'm doing wrong?

 Is

   if ( $_ =~ /40187378|40187233|40187230|4018722
 +9|40187234|40187232|40186526/ ) {

 ever true?

 Yes, it is true, lots and lots of lines.  I tried changing:

 print MY $line;
 to
 print {MY} $line;

 and I get an error when using strict.

 The weird thing is, that when I change the open statement from:

 open(MY,  /tmp/savedata/new/$_) or die can't open new $_ $!\n;
 to
 open(MY,  /tmp/savedata/new/$_) or die can't open new $_ $!\n;

 and append instead of write to the file, everything works.

 Any ideas?

I may be wrong, but when re-writing (as opposed to appending) is the
file not deleted, then recreated?

What user are you running the program with?

Just a shot in the dark, but does the user running the script have the
appropriate permissions on the file itself?

Steve


 Kevin
 --
 Kevin Old
 [EMAIL PROTECTED]

 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/ http://learn.perl.org/first-response






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Printing to a file

2004-10-26 Thread Gunnar Hjalmarsson
Chap Harrison wrote:
Ah-freakin'-men.  Gunnar, what the *hell*?  I've been reading this list 
for a couple of months now and the majority of your posts seem to be 
little more than chiding people in an extremely unpleasant tone.
If you find a *particular* post extremely unpleasantly worded, please 
say so and explain why, and I'll be happy to give my view.

Do you think it's consistent with this list's claim to 
be a place where beginners can ask questions in a friendly environment?
Even if I'm not sure what it is, let me make clear that I for one 
expect people to give it a *serious* try before asking a group of 
volunteers for help. Doing so is civil, the opposite is rude.

As regards Kevin's initial post in this thread, I still don't think that 
an hour is very long time when it comes to debugging, and talking about 
not wanting to waste time is not an appropriate way IMO to approach a 
forum with a problem. It's at least not an appropriate way to approach 
me. ;-)

Suggested reading: http://www.catb.org/~esr/faqs/smart-questions.html
That document is well applicable to this list. The fact that it's a 
beginner-level list does not make a difference with respect to general 
etiquette, right?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



RE: Printing to a file

2004-10-26 Thread Brian Gunlogson
Oops, my bad. This looked like a problem I had. I stored a filehandle in a hash, and 
it confused
print when I accessed the filehandle as a scalar.

I'd better think before I post again... *blush*

--- Bob Showalter [EMAIL PROTECTED] wrote:

 Brian Gunlogson wrote:
  Put the MY in braces.
  
  SO
  print MY $line;
  
  WOULD BECOME
  print {MY} $line;
 
 Why? That fails under use strict, and is totally unecessary.




__
Do you Yahoo!?
Take Yahoo! Mail with you! Get it on your mobile phone.
http://mobile.yahoo.com/maildemo 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Printing to a file

2004-10-26 Thread DBSMITH
I agree, Gunnar is mean.  Mean people suck!

derek




Chap Harrison [EMAIL PROTECTED]
10/26/2004 01:37 PM

 
To: Gunnar Hjalmarsson [EMAIL PROTECTED]
cc: Perl Lists [EMAIL PROTECTED]
Subject:Re: Printing to a file


 Wait a minute there.  I'm not learning programming.  I've been a perl
 programmer for 7 years and have written hundreds of more complex
 programs than this and have written to thousands of files in my time.
 I just needed another pair of eyes to possibly point out what I was
 doing wrong.

 Also, posting to this list is not wasting hundreds of other people's
 time.  It's a discussion list.  If you're looking at my message it
 means that you're either seeking to help or seeking help.  Therefore,
 you are choosing to devote a few minutes of your time to help or be
 helped.

 Sorry my post rubbed you the wrong way.

Ah-freakin'-men.  Gunnar, what the *hell*?  I've been reading this list 
for a couple of months now and the majority of your posts seem to be 
little more than chiding people in an extremely unpleasant tone.  Is 
that your job?  Do you think it's consistent with this list's claim to 
be a place where beginners can ask questions in a friendly environment? 
  Come on.  Try being civil, please.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response






Re: Printing to a file

2004-10-26 Thread JupiterHost.Net

[EMAIL PROTECTED] wrote:
I agree, Gunnar is mean.  Mean people suck!
Ok, everyone calm down :) Perhaps some folks can be a bit blunt and 
maybe its even wrong *but* the content was good if not the delivery :)

As a support person myself it is extremely annoying when soemone comes 
up and says My Foo doesn't work, Ok, in what way, any errors, what 
steps do you do to see the problem, how can I acess Foo to 
troublehshoot, what have you tried, what the heck is  Foo, etc etc

At the same time we don't want to become the qmail list ;p
So hears the deal, we'll all work on being nicer and we'll all work on 
how we post messages to make sure its easier to assist when we ask 
questions.

Now kiss and make up :)
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



Re: Printing to a file

2004-10-26 Thread Felix Li
print Gunnar does not suffer fools gladly.\nSo?;
print Try not to look like a fool!;

- Original Message - 
From: JupiterHost.Net [EMAIL PROTECTED]
To: Perl Lists [EMAIL PROTECTED]
Sent: Tuesday, October 26, 2004 4:01 PM
Subject: Re: Printing to a file


 
 
 [EMAIL PROTECTED] wrote:
  I agree, Gunnar is mean.  Mean people suck!
 
 Ok, everyone calm down :) Perhaps some folks can be a bit blunt and 
 maybe its even wrong *but* the content was good if not the delivery :)
 
 As a support person myself it is extremely annoying when soemone comes 
 up and says My Foo doesn't work, Ok, in what way, any errors, what 
 steps do you do to see the problem, how can I acess Foo to 
 troublehshoot, what have you tried, what the heck is  Foo, etc etc
 
 At the same time we don't want to become the qmail list ;p
 
 So hears the deal, we'll all work on being nicer and we'll all work on 
 how we post messages to make sure its easier to assist when we ask 
 questions.
 
 Now kiss and make up :)
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/ http://learn.perl.org/first-response
 
 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Printing to a file

2004-10-26 Thread Ed Christian
 #!/usr/bin/perl
 
 +
 use warnings;
 use strict;
 use File::Find;
 
 find sub {
 return unless -f;
 return unless $_ =~ /.\d+$/;
 print $_\n;
 #print $File::Find::name\n;
 
 +
 open(SD, $_) or die can't open $_ $!\n;
 #my $fh = IO::File-new( /tmp/savedata/new/$_) or die Coul
 +dn't open new for writing: $! \n;
 open(MY,  /tmp/savedata/new/$_) or die can't open new $_ $
 +!\n;

Why are you repeatedly opening and closing the file within the find
subroutine? You're overwriting the file every time by opening with 
and not . Either place your open outside the find, or use append
mode.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Printing to a file

2004-10-26 Thread DBSMITH
yes I do agree, some people need to try harder at their code and be more 
descriptive in their problems.  Gunnar, nothing personal! : ) 

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
614-566-4145





JupiterHost.Net [EMAIL PROTECTED]
10/26/2004 04:01 PM

 
To: Perl Lists [EMAIL PROTECTED]
cc: 
Subject:Re: Printing to a file




[EMAIL PROTECTED] wrote:
 I agree, Gunnar is mean.  Mean people suck!

Ok, everyone calm down :) Perhaps some folks can be a bit blunt and 
maybe its even wrong *but* the content was good if not the delivery :)

As a support person myself it is extremely annoying when soemone comes 
up and says My Foo doesn't work, Ok, in what way, any errors, what 
steps do you do to see the problem, how can I acess Foo to 
troublehshoot, what have you tried, what the heck is  Foo, etc etc

At the same time we don't want to become the qmail list ;p

So hears the deal, we'll all work on being nicer and we'll all work on 
how we post messages to make sure its easier to assist when we ask 
questions.

Now kiss and make up :)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response






RE: Printing to a file

2004-10-26 Thread Ed Christian
Ed Christian wrote:
 #!/usr/bin/perl
 
 +
 use warnings;
 use strict;
 use File::Find;
 
 find sub {
 return unless -f;
 return unless $_ =~ /.\d+$/;
 print $_\n;
 #print $File::Find::name\n;
 
 +
 open(SD, $_) or die can't open $_ $!\n;
 #my $fh = IO::File-new( /tmp/savedata/new/$_) or die
 Coul +dn't open new for writing: $! \n;
 open(MY,  /tmp/savedata/new/$_) or die can't open new $_
 $ +!\n;
 
 Why are you repeatedly opening and closing the file within the find
 subroutine? You're overwriting the file every time by opening with
  and not . Either place your open outside the find, or use
 append mode.   

Er, that is, if your find subroutine recurses through directories and
finds the same filename nested deeper within the search path. Ignore the
open outside the find comment as just plain silly. :)

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Printing a text file

2001-12-11 Thread Ben Crane

Hi list,

Does anyone know how to print out a text file to a
printer? i would like the option of chosing a printer
to send the document to, in addition, will I need to
reformat the text, or will it print out the text file
as is?

Thanx
Ben

__
Do You Yahoo!?
Check out Yahoo! Shopping and Yahoo! Auctions for all of
your unique holiday gifts! Buy at http://shopping.yahoo.com
or bid at http://auctions.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Printing a text file

2001-12-11 Thread Andrea Holstein

Ben Crane wrote:
 
 Hi list,
 
 Does anyone know how to print out a text file to a
 printer? i would like the option of chosing a printer
 to send the document to, in addition, will I need to
 reformat the text, or will it print out the text file
 as is?
 
Have searched in search.cpan.org for print ?

I did last week and found a module Printer in it.

Best Wishes,
Andrea

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Searching and printing a text file

2001-06-19 Thread SAWMaster

Hi group!

Ok, I'm trying to open all files in the directory containing my script, and
search each one for lines containing my search string.  This is my code so
far:

$i = 0;
opendir(logdir, '.') or die Can't open directory.;
print Enter string to search for: ;
$searchstring = ;
print \n;
#print join(\n, readdir(logdir));
print \n;
@filelist = readdir(logdir);
while($i  ($#filelist))
{
open(logfile, @filelist[$i]);
 $i++;
 while(logfile)
{
  #if(#line I'm currently on is m/$searchstring/i)
  {
   print;
  }
 }
}
print Press enter to continue...;
;
$i = 0;
while($i  ($#filelist))
{
 close(logfile, @filelist[$i]);
 $i++;
}
closedir $logdir;

Now the problem is, how do I specify The line I'm currently working on?

Right now my program prints every line from every file in my directory, but
once I figure this out I think it should limit the output to only lines
containing my search string.


Can someone help me out?



Re: Searching and printing a text file

2001-06-19 Thread Me

  while(logfile)
 {
   #if(#line I'm currently on is m/$searchstring/i)
   {
print;
   }

 Now the problem is, how do I specify The line I'm currently working
on?

That's easy -- you don't.

When you said:

print;

you didn't specify what to print. So print printed $_,
the default variable. Lots of functions either set or
get the default variable if you don't tell them to do
otherwise.

In the same vein:

while () {

(with or without a handle name in the s)
sets $_.

And:

if (/foo/) {

compares foo to $_.

Easy, huh?

Btw, the current line no is in $.

hth.




Re: Searching and printing a text file

2001-06-19 Thread SAWMaster

 Ok, I think I understand you, but things still are not working right.

Never mind, I forgot to chop my search string.  Works great now :)

TDH (That DID help) lol.