maheshkumar has asked for the wisdom of the Perl Monks concerning the following question:
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 }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Reading Multiple files
by nemesdani (Friar) on Aug 07, 2012 at 11:47 UTC | |
by bimleshsharma (Beadle) on Aug 07, 2012 at 12:48 UTC | |
by maheshkumar (Sexton) on Aug 07, 2012 at 16:50 UTC | |
by roboticus (Chancellor) on Aug 07, 2012 at 19:52 UTC | |
by maheshkumar (Sexton) on Aug 07, 2012 at 11:49 UTC | |
by aitap (Curate) on Aug 07, 2012 at 12:20 UTC | |
Re: Reading Multiple files
by 2teez (Vicar) on Aug 07, 2012 at 13:06 UTC | |
by maheshkumar (Sexton) on Aug 07, 2012 at 16:21 UTC | |
by 2teez (Vicar) on Aug 07, 2012 at 18:13 UTC | |
by maheshkumar (Sexton) on Aug 08, 2012 at 15:36 UTC | |
by 2teez (Vicar) on Aug 08, 2012 at 16:39 UTC |