Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

how to save result in multiple files using for loop in perl

by zakishah (Novice)
on Aug 27, 2012 at 22:04 UTC ( #990067=perlquestion: print w/replies, xml ) Need Help??
zakishah has asked for the wisdom of the Perl Monks concerning the following question:

Hi, monks i want to save result of traceroute in different files, and i am using for loop, how can i do that , my code seems to be not working. Any help will be appreciated. my code is ....

#use strict; #use warnings; open FILE, '<', "dns.txt" or die $!; my @dude; while (my $line = <FILE>) { my ($ip) = $line =~ /(\d+\.\d+\.\d+\.\d+)/; push(@dude,$ip); } my $num = @dude + 1; for(my $o = 0; $o<$num; $o++){ system ("ssh a.AS1 traceroute @dude[$0] > file"+"$0.txt "); }

Replies are listed 'Best First'.
Re: how to save result in multiple files using for loop in perl
by davido (Archbishop) on Aug 27, 2012 at 22:31 UTC

    You didn't mention in what way(s) it's not working. That's important info for anyone trying to help you debug.

    One issue I noticed is that you're going to run passed the end of @dude. The scalar value returned by @dude is the number of elements it contains. @dude+1 is one more than the number of elements.

    Another issue is here: file"+"$0.txt ". Perl doesn't concatenate strings using '+'. It uses the dot operator for that. But it's not even necessary. You could write it as file$0.txt".

    Here's a reworked version. I won't be debugging your system call, and don't know if it's even an issue:

    use strict; use warnings; open FILE, '<', "dns.txt" or die $!; while (my $line = <FILE>) { my ($ip) = $line =~ /(\d+\.\d+\.\d+\.\d+)/; system("ssh a.AS1 traceroute $ip > file$ip.txt"); }


Re: how to save result in multiple files using for loop in perl
by linuxer (Curate) on Aug 27, 2012 at 22:23 UTC

    Check your for-loop at the end of your code. You declared $o (letter o) as loop variable and then you use $0 (number 0 (zero)) inside the system call to create the filename...

Re: how to save result in multiple files using for loop in perl
by jethro (Monsignor) on Aug 27, 2012 at 22:33 UTC

    Never ever use a variable named $o (dollar-ow) !!!! As you can see (or not see), you seem to be using $0 (dollar-zero) in your system call instead

    You probably will find a file named file<your scriptname>.txt in your directory, because the name of your script is usually stored in $0 (dollar-zero).

    There is another bug in your script: Since you initialize $num with @dude+1, your for loop will always try to access an undefined value after the end of your array. You might like to use the following instead, much less error-prone

    my $i=0; for my $ip (@dude) { system ("ssh a.AS1 traceroute $ip > file${i}.txt "); $i++; }
Re: how to save result in multiple files using for loop in perl
by ww (Archbishop) on Aug 27, 2012 at 23:48 UTC
    strict and warnings won't help much when commented out, as in your post. They could have:
    023: for(my $o = 0; $o<$num; $o++){
    024: system ("ssh a.AS1 traceroute @dude[$0] > file"+"$0.txt ");
    Had you not disabled them, you would have learned right away that $0 (twice, in Ln 24) is something other than you intended; specifically, it's the full path-name of the file being executed; and not the same as the $o instantiated in Ln 23.

    Granted, the error messages and warnings would have been numerous and perhaps confusing, but the appearance of the filename might have sent you looking in the right direction.

Re: how to save result in multiple files using for loop in perl
by Rudolf (Pilgrim) on Aug 27, 2012 at 22:28 UTC

    for looping through the array, you dont really need the index unless your doing something special with the data, I am assuming what you want to do but for your loop at the end here is what I suggest:

    close FILE;#dont forget to close your file for(my $i=0;$i<scalar(@dude);$i++){ system("ssh a.AS1 tracerout $dude[$i] > file $i.txt"); }

    not sure why you used the + sign, maybe your looking for the . that connects strings ? and your use of $0 is also confusing because it is the path to the perl script itself.. this could not be used as an index. possibly you ment to say $dude[$o] which would make a lot more sense. but your problem is that the correct way to index an array is by putting it into scalar context ( $dude[$i] ) because the bit of information your taking from the array is flat data you begin it with a dollar sign instead of the @.

    FIXED: length(@dude) to scalar(@dude) thanks jethro

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://990067]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-05-22 18:04 GMT
Find Nodes?
    Voting Booth?