Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

No such file or directory at line 19.

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

Dear Monks , i am trying to open a txt file( Junk.txt) which contains list of other txt files. My code is not working and it displays error: No such file or directory at line 19. i tried many ways but failed. I will be thankful for any help. My file contents and code is below

my text file ( junk.txt ) is here

file0.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt file10.txt

my code is here

use strict; sub R_loop2(){ my $file = "junk.txt"; open (FH, "< $file") or die "Can't open $file for read: $!"; my @lines; while (<FH>) { push (@lines, $_); } close FH or die "Cannot close $file: $!"; #print @lines; my $count= @lines + 1; my @dude; for ( my $k=0; $k<$count; $k++){ open FILE, '<', "@lines[$k]" or die $!; #my @dude; while (my $line = <FILE>) { my ($ip) = $line =~ /(\d+\.\d+\.\d+\.\d+)/; #print $ip; push(@dude,$ip); #print "$hh"; } system("clear"); print "The packet traverse through this path :\n"; my $num = @dude + 1; for(my $o = 0; $o<$num; $o++){ print " @dude[$o]\n"; } } my %seen; foreach my $string(@dude) { next unless $seen{$string}++; print "Routing Loop Detected \n"; return 1; } }1;

Replies are listed 'Best First'.
Re: No such file or directory at line 19.
by NetWallah (Canon) on Aug 28, 2012 at 00:34 UTC
    Why don't you replace the line as suggested below, to improve the error message :
    ##OLD LINE# open FILE, '<', "@lines[$k]" or die $!; open (FILE, '<', $lines[$k]) or die "Cannot open file $k='$lines[$k]' +:$!";
    Although I would replace 'FILE' my a localized handle : 'my $filehandle' .

                 I hope life isn't a big joke, because I don't get it.

Re: No such file or directory at line 19.
by 2teez (Vicar) on Aug 28, 2012 at 01:25 UTC

    If I may give you a head up. The following script will get names of file from junk.txt, saved in an array variable. Then using a foreach loop, open function and a while loop to print out content of each of the file listed in the text file named junk.txt like so:

    use warnings; use strict; use Cwd qw(abs_path); my $file = "junk.txt"; my @lines; open my $fh, '<', $file or die "can't open file: $!"; while (<$fh>) { chomp; push( @lines, $_ ); } close $fh or die "Cannot close file: $!"; foreach my $my_file (@lines) { $my_file = abs_path($my_file); # get the absolute path of each +file open my $fh2, '<', $my_file or die "can't open file: $!"; while (<$fh2>) { chomp; print $_, $/; } close $fh2 or die "Cannot close file: $!"; }
    Re-write this to do what you want.
    Please, also note that subroutine are not written in Perl like so: sub subname(){...} (expect for maybe prototype),but like so: sub subname{...}
    Please, check perlsub for more information.

Re: No such file or directory at line 19.
by ig (Vicar) on Aug 28, 2012 at 06:26 UTC

    It may be that open fails when you try to access an array element that doesn't exist.

    You initialize @lines with the contents of your file, then you iterate over the array, but you don't stop when you reach the end of the array: you keep going for two more iterations.

    In scalar context (like when adding 1 and assigning to $count), @lines evaluates to the number of elements in the array. In your example: 11. Then you add 1, so $count is set to 12. You iterate while $k varies from 0 through 11 and stop when it reaches 12. But the indexes for your array are 0 through 10 - there is no element with index 11.

    You could set count with $count = @lines - 1 or, even better $count = $#lines, but you don't use $k except to get elements from $lines, so you would be better to use a loop like:

    foreach my $file (@lines) { open(my $fh, '<', $file) or die "$file: $!"; ... }

    Another possibile cause of your fault is that you have line termination characters in your file names but you probably don't have files with names that end with line termination characters. You can use chomp to remove these characters.

    my $file = "junk.txt"; open (my $fh, '<', $file) or die "Can't open $file for read: $!"; my @lines; while (<$fh>) { chomp; push (@lines, $_); } close($fh) or die "Cannot close $file: $!";
Re: No such file or directory at line 19.
by Kenosis (Priest) on Aug 28, 2012 at 07:17 UTC

    I will be thankful for any help.

    Well, am not sure whether the following will be any help, but...

    Looks like you're gathering and displaying IPs from a set of files, then printing 'Routing Loop Detected' at the end if an IP's been duplicated in that set.

    An alternative to manually opening files is to use File::Slurp qw/read_file/;, as it returns a file's lines in a list context. Give this, and the excellent feedback already provided, consider the following option:

    use Modern::Perl; use File::Slurp qw/read_file/; use Regexp::Common qw/net/; sub R_loop2 { my ( $file, $loop, %seen ) = 'junk.txt'; for my $fileName ( read_file $file ) { chomp $fileName; say 'The packet traversed this path:'; for my $fileLine ( read_file $fileName ) { $fileLine =~ /($RE{net}{IPv4})/ or next; $loop++ if ++$seen{$1} > 1; say $1; } } if ($loop) { say 'Routing Loop Detected'; return 1; } }

    Each line in the files whose names are contained in junk.txt is processed using Regexp::Common qw/net/ to match/capture IP addresses. Duplicate IPs are tracked, just before printing the IP address, by incrementing $loop, and 'Routing Loop Detected' will print at the end if $loop is set.

    Hope this helps!

      Thank you very much you really understood my problem and i really want to perform the task you just said. But problem is that i am working on platform where i have no access to download Modern::Perl module so i want to ask if there is any other way to perform this job. Thanks for your help

        I notice you've been advised (multiple times) to just remove the offending line. I suspect, though, that you may get similar messages with the other two modules.

        When there's a task like yours, it's good to first see if there's a module for it (or some components of it), as modules can help avoid 'reinventing the wheel,' are often mature and well-tested, and can reduce development time and programmatic errors. Some, however, may have to work with the Perl environment as is, with no option to install modules.

        Given the above, see the modified script below (that's not a subroutine):

        use strict; use warnings; my ( $file, $loop, %seen ) = 'junk.txt'; open my $fh1, '<', $file or die "Unable to open $file: $!"; while ( my $fileName = <$fh1> ) { chomp $fileName; print "The packet traversed this path in $fileName:\n"; open my $fh2, '<', $fileName or die "Unable to open $fileName: $!" +; while ( my $fileLine = <$fh2> ) { $fileLine =~ /(\d+\.\d+\.\d+\.\d+)/ or next; $loop++ if ++$seen{$1} > 1; print "$1\n"; } close $fh2; print "\n"; } close $fh1; print "** Routing Loop Detected **\n" if $loop;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2018-04-21 12:38 GMT
Find Nodes?
    Voting Booth?