venkat@perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am new to perl facing this problem.Please give me the solution.THANKS

my @files_list = glob "$inpath/filename*.txt"; foreach my $var (files_list){ my @array[i]=$var; i=i+1; } my @sorted_list = reverse sort @myarray; #@myarray contains the files foreach my $file (@sorted_list){ my $single_file = glob "$path/$file*.txt"; some stuff on files...................... }
when I run this for first file it's working fine but for second file I an not able to get the second file

Replies are listed 'Best First'.
Re: glob is working only first time in foreach loop
by Corion (Patriarch) on Jan 27, 2016 at 15:16 UTC
    my @array[i]=$var;

    The syntax errors aside, this line is your error. You are (re)declaring the array every time through the loop. Most likely you wanted to have the array outside of your loop. Also see push for appending elements to an array.

    Also, you already have the elements in @files_list. Why are you copying them over into @array?

Re: glob is working only first time in foreach loop
by toolic (Bishop) on Jan 27, 2016 at 15:10 UTC
    • Read Writeup Formatting Tips and update your post to use "code" tags for your code samples.
    • Post the code you're are actually using. What you posted has syntax errors.
    • Be specific about what you mean by "I an not able to get the second file". What do you get? What do you expect to get?
    • Basic debugging checklist
Re: glob is working only first time in foreach loop
by LanX (Saint) on Jan 27, 2016 at 19:44 UTC
    this is no valid code, variables like files_list or i need sigils in Perl, i.e. @ and $

    Please start always using

    use strict; use warnings;
    for your code.

    Posting code which isn't checked this way won't help you here.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!