CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
My Variable is not valid in while loop

 



Alux
New User

Oct 6, 2011, 10:36 AM

Post #1 of 4 (1424 views)
My Variable is not valid in while loop Can't Post

Dear all

i wrote and perl script that try to extract some information in log files

here is a part of my code

open(FILE2, "SyslogCatchAll2010-$ARGV[0]-$ARGV[1].txt") or
die("Could not open log file. $!\n");

my @date = split(':', $q22[1]);

foreach $line4 (<FILE2>){
chomp($line4);

# print "$line4 \n";
while ($tmp > 0){
some code....

print "$line4 \n"; ( ***)
my $Time= "$date[0]:$date[1]:$date[2]";

if ($line4 =~ m/$Time/){

if ($line4 =~ m/$q3/){

print "$line4\n";}

}#for if
}#for while 2
my $tmp=$q4;
}#for while time

Code
 
i confused because when i try to print my $line4 that sign with (***)
nothing in the $line4 , just an empty variable !

but when i print $line4 outside of while ($tmp > 0){ loop
its works !!!

i also try to initiate my line4 variable in beginning of my script but it dos'nt works

any idea?


FishMonger
Veteran / Moderator

Oct 6, 2011, 5:46 PM

Post #2 of 4 (1417 views)
Re: [Alux] My Variable is not valid in while loop [In reply to] Can't Post

Do you have the strict and warnings pragmas loaded? If not, start by loading them and fix the problems that they point out.

Since you've chopped up and posted an incomplete script and didn't provide the data file you're parsing, it's hard if not impossible for us to point out why your script didn't work.

Please provide a compete but short script that demonstrates the problem and include a sample of the data file you're parsing.


Alux
New User

Oct 6, 2011, 11:41 PM

Post #3 of 4 (1373 views)
Re: [FishMonger] My Variable is not valid in while loop [In reply to] Can't Post

yes , i loaded them before and its not working....

here is my complete script


Code
 
#!/usr/bin/perl -w


use strict;
use warnings;

my $file = '1.csv';

my $line4 = '';

open(FILE, "<$file") or
die("Could not open log file. $!\n");

while(<FILE>) {

my($line) = $_;

chomp($line);

my ($q1,$q2,$q3,$q4) = split(/,/,$line);


my $tmp=$q4;
open(FILE2, "SyslogCatchAll2010-$ARGV[0]-$ARGV[1].txt") or
die("Could not open log file. $!\n");


if($line =~ /2010\/$ARGV[0]\/$ARGV[1]/){

my @q22 = split(' ', $q2); # extracting time

my @date = split(':', $q22[1]); #date of IP !

foreach $line4 (<FILE2>){
chomp($line4);

# print "$line4 \n";
while ($tmp > 0){

Some code...

print "$line4 \n";
my $Time= "$date[0]:$date[1]:$date[2]";

if ($line4 =~ m/$Time/){

if ($line4 =~ m/$q3/){

print "$line4\n";}

}#for if
}#for while 2
my $tmp=$q4;
}#for while time


}
}
close(FILE);



FishMonger
Veteran / Moderator

Oct 7, 2011, 8:42 AM

Post #4 of 4 (1338 views)
Re: [Alux] My Variable is not valid in while loop [In reply to] Can't Post

First, in the future post questions like this in the beginners section.

You need to post a sample of the 2 files so that I can run a test to duplicate the issue.

1) Remove the -w switch. It is not needed.

2) Use a lexical var for the filehandle and the 3 arg form of open.

3) The die statements should say which file couldn't be opened.

4) Var names should indicate what type of data they hold. Using single letter var names in most cases should not be done. Some exceptions are made for array indexes and numerical loop iterators.

5) Initialize the $Line var when initializing the while loop instead of coping $_ after the initialization.

6) Instead of using an @date array, use separate vars (i.e., $yr, $mon, $day) for the date.

7) Don't keep opening the second file inside the while loop. Open that file prior to the while loop and if needed, use the seek function to reset the file pointer.

8) Fix the indentation, it's all over the map. Indentation should be at least 3 characters but never more than 8 at each level. My preference is 4 characters. What ever you end up using be consistent.

9) Your $tmp var never changes inside the while loop, so that becomes an infinite loop.

There are a number of other things I can point out, but that's all I have time for.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives