zakishah has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks Thanks for your time to time help, once again i need your assistance, i am tring to open a file that contains names of other text files, which i need to open to display their contents, i am struggling hard but countering various errors. i need you help to tell me how to open file.txt which is in other text file e.g ( in my code ) junk.txt Thanks again!

my $file = "junk.txt"; open FH, '<', "$file"; my @lines; while (<FH>) { push (@lines, $_); } close FH or die "Cannot close $file: $!"; my @dude; my $count= @lines + 1; for(my $k = 0; $k<$count; $k++){ open FILE, '<', "@lines[k]" or die $!; while (my $line = <FILE>) { my ($ip) = $line =~ /(\d+\.\d+\.\d+\.\d+)/; push(@dude,$ip); } }

Replies are listed 'Best First'.
Re: No Such File or Directory Error
by 2teez (Vicar) on Aug 27, 2012 at 23:57 UTC
    Hi,

    Please use warnings and strict in your script. It will help you identify several errors in your code.
    Also, Use 3 - arugment open function like so:

    ope my $fh,'<',$file or die "can't open file: $!";
    Use foreach loop, not a C language type of for loop, though it work in Perl. Write like so:
    ... use Cwd qw(abs_path); ## module to get absolute pathname ... foreach my $my_file(@lines){ $my_file = abs_path($my_file); # get the absolute pathname of ea +ch file open my $fh2,'<',$my_file or die "can't open file: $!"; while(<$fh2>){ ... } } ...

Re: No Such File or Directory Error
by morgon (Priest) on Aug 28, 2012 at 00:17 UTC
    The lines read via the angle-operator still contain a newline that you need to get rid of if you want to use it as a file name.

    So you need to replace

    while (<FH>) { push (@lines, $_); }
    with
    while (<FH>) { chomp; # removes the newline from $_ push (@lines, $_); }
Re: No Such File or Directory Error
by Rudolf (Pilgrim) on Aug 28, 2012 at 00:17 UTC
    "@lines[k]"

    will NEVER work. you need to tell perl k is a variable by putting a dollar sign in front of it and as I explained in your previous post, you need to access an array element as a scalar. that is:

    $lines[$k]