Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subroutine 2

Status
Not open for further replies.
Jun 16, 2005
52
0
0
US
Hello,

I know this is probably a dumb question to all of you, but I am horrible at programming and need someone to help me with this. How do I get this bit of code (below) into a subroutine so that I can repeat this through out my script. Thank you so much in advance.


my $log_file = pop @logs;

open (FILE, "$dir$log_file");
while(<FILE>){
chomp;
$sn = (split(/\t/,$_))[1];
$sn =~ s/^\s+//;
$sn =~ s/\s+$//;
if ($sn =~ /^[0-9]+$/) {
push(@list, $sn);
}
}
close(FILE);

my $log_file2 = pop @logs;

open (FILE2, "$dir$log_file2");
while(<FILE2>){
chomp;
$sn = (split(/\t/,$_))[1];
$sn =~ s/^\s+//;
$sn =~ s/\s+$//;
if ($sn =~ /^[0-9]+$/) {
push(@list, $sn);
}
}
close(FILE2);
/
 
surround it with
Code:
 sub SUBNAME {
}

to call it use &SUBNAME;



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I should have rephrased my original question..sorry

The code below opens log files that are read and parsed later in the script. I have it read about 20 log files at a time. I currently cut and paste the below code changing the highlighted areas with numbers in consecutive order.( $log_file2, FILE2, <FILE2>, (FILE2). I want to be able to call on one subroutine to chew through 20 log files with out the long drawn out code that I have now. Its a mess. ANy help appreciated. Thank you 1DMF




CODE:


my $log_file22 = pop @logs;

open (FILE2, "$dir$log_file");
while(<FILE2>){
chomp;
$sn = (split(/\t/,$_))[1];
$sn =~ s/^\s+//;
$sn =~ s/\s+$//;
if ($sn =~ /^[0-9]+$/) {
push(@list, $sn);
}
}
close(FILE2);




Actual Script sample:

my $log_file = pop @logs;

open (FILE, "$dir$log_file");
while(<FILE>){
chomp;
$sn = (split(/\t/,$_))[1];
$sn =~ s/^\s+//;
$sn =~ s/\s+$//;
if ($sn =~ /^[0-9]+$/) {
push(@list, $sn);
}
}
close(FILE);

my $log_file2 = pop @logs;

open (FILE2, "$dir$log_file2");
while(<FILE2>){
chomp;
$sn = (split(/\t/,$_))[1];
$sn =~ s/^\s+//;
$sn =~ s/\s+$//;
if ($sn =~ /^[0-9]+$/) {
push(@list, $sn);
}
}
close(FILE2);

my $log_file3 = pop @logs;

open (FILE3, "$dir$log_file3");
while(<FILE3>){
chomp;
$sn = (split(/\t/,$_))[1];
$sn =~ s/^\s+//;
$sn =~ s/\s+$//;
if ($sn =~ /^[0-9]+$/) {
push(@list, $sn);
}
}
close(FILE3);

my $log_file3 = pop @logs;

open (FILE3, "$dir$log_file4");
while(<FILE3>){
chomp;
$sn = (split(/\t/,$_))[1];
$sn =~ s/^\s+//;
$sn =~ s/\s+$//;
if ($sn =~ /^[0-9]+$/) {
push(@list, $sn);
}
}
close(FILE3);

my $log_file4 = pop @logs;

open (FILE4, "$dir$log_file5");
while(<FILE4>){
chomp;
$sn = (split(/\t/,$_))[1];
$sn =~ s/^\s+//;
$sn =~ s/\s+$//;
if ($sn =~ /^[0-9]+$/) {
push(@list, $sn);
}
}
close(FILE4);


etc.....
 
Something like this?:
Code:
my @list;
my $dir = 'something';
my @logs = qw/one two three four/;

for my $logfile( @logs ) {
   add_to_list( "$dir$logfile", \@list );
}

sub add_to_list {
   my $logfile = shift;
   my $array = shift;

   open (FILE, "$logfile");
   while(<FILE>){
     chomp;
     my $sn = (split(/\t/,$_))[1];
     $sn =~ s/^\s+//;
     $sn =~ s/\s+$//;
     if ($sn =~ /^[0-9]+$/) {
          push(@$array, $sn);
     }
   }
   close(FILE);
}
 
qw automatically quotes single string values for you. Just a lazy way of populating a hash.

Why this:

my @logs = ("one", "two", "three", "four");


When you can do it much faster and easier like this:

my @logs = qw/one two three four/;

 
raklet - what if the item you want in an array is two words, you'll have no choice but to put quotes round it won't you?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Yes. The qw// construct only useful in certain situations.
 
Hello,


This post is a couple weeks old but I was wondering If I could get some furhter help. Here is my question:

How can I get this bit of code (Thanks Ishnid) to only go back through the last 6 days or 144 hours of log files? The problem with the code below is that it goes through all files in a directory that have the name " File List F-#######.txt " (After the dash there just numbers for each file ex. " File List F-00123.txt " ). I only want it to go through the past 6 days or 144 hours of those files. Can anyone help?


opendir(DIR, "$dir");
my @filelist = readdir(DIR);
closedir(DIR);


foreach my $file (@filelist)
{
if (-f "$dir$file")
{
if ($file =~ /^File List F-0/i)
{
push @logs, $file;
}
}
}

my @list;



for my $logfile( @logs ) {
add_to_list( "$dir$logfile", \@list );
}

sub add_to_list {
my $logfile = shift;
my $array = shift;

open (FILE, "$logfile");
while(<FILE>){
chomp;
my $sn = (split(/\t/,$_))[1];
$sn =~ s/^\s+//;
$sn =~ s/\s+$//;
if ($sn =~ /^[0-9]+$/) {
push(@$array, $sn);
}
}
close(FILE);
}
 
Hi,

The function you need to look at is the stat() function which collects info about files.

So, get the current time in a variable and then for each filename you get - call stat() and get the mtime for that file - compare that against current time minus whatever and accept or reject the file based on that comparison.

Documentation at
Mike

The options are: fast, cheap and right - pick any two. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top