I need some help with this, I have written a code which takes in input of multiple files and then it is supposed to process each file and save its output in a seperate file for each, but what is happening is that everytime i run the program it takes the new file created as the input as well. And for example my variable $ip is in the program its defined it just shows up that uninitialised any help on this?
#!/usr/bin/perl -w
use Regexp::Common qw/net/;
use warnings;
#use strict;
use Regexp::Common qw/net/;
use strict;
use Cwd;
my $mypath=getcwd();
my $mypath=$mypath."/sd";
my $ip;
chdir $mypath;
my @files = <*>;
my $var=0;
my $i = 0;
my $traceroute;
my $line;
foreach (@files) {
if (-f $_) {
process ($_);
}
}
sub process {
my $file2 = $_[0];
open my $in, '<', $file2 or die $!;
open my $out, '>', "$file2.log" or die $!;
while (my $line = <$in>){
if($line =~ /^Traceroute: .* (\S+)/) {
$traceroute = $1;
#print "$traceroute\n";
$var++;
#print "$traceroute\n";
}
my ($ip) = $line =~ /(?: \d+ \s \s+) ($RE{net}{IPv4}) /msx
+;
print "$ip\n";
if($traceroute eq $ip){
print {$out} $ip if defined ;
if($ip ne undef){
{$i++;}
}
}
else {
}
}
print {$out} "Number of traceroutes - $var\n";
print {$out} "Number of traceroutes reached destination - $i\n";
my $subs = $var-$i;
print {$out} "Number of traceroutes that did not reaach destination ($
+subs)\n";
my $perc = ($i/$var)*100;
print {$out} "Percentage of sucessful traceroutes ($perc%)\n";
# ... your remain code here
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|