Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Array problem......PLEASE HELP!

by green_lakers (Novice)
on Jan 05, 2009 at 20:02 UTC ( #734265=perlquestion: print w/replies, xml ) Need Help??

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

My home directory contains these files:- 08-1.txt 08-2.txt 09-1.txt 09-2.txt
i have an array @files = ("08","09");
foreach $file (@files) { $file =~ s/\s*$//; @command = `ls -l|grep $file`; print @command; system("/usr/bin/perl $file"); }
Content of #!/usr/local/bin/perl my %data; my $header = <>; # first line while(<>) { my($key) = split /\s+/; $data{$key} = $_; } print $header; foreach my $key (sort keys %data) { print $data{$key}; }
value of @command array after the first iteration of @files is $command[0]=08-1.txt $command[1]=08-2.txt but for the second iteration of @files value of @command array :- $command[0]=09-1.txt 09-2.txt $command[1]=empty. why this is happening? i want the value to be $command[0]=09-1.txt $command[1]=09-2.txt can anyone please help?
I need to use the @files array. can anyone suggest what might cause the @command array to act like this? Is this because is used in the script?

Replies are listed 'Best First'.
Re: Array problem......PLEASE HELP!
by moritz (Cardinal) on Jan 05, 2009 at 20:06 UTC
    Don't make it so complicated, use glob instead: my @files = glob "09-*.txt";
Re: Array problem......PLEASE HELP!
by zentara (Archbishop) on Jan 05, 2009 at 20:32 UTC
    First thing is put
    use warnings; use strict;
    at the top of your script, then see what happens when you get rid of the globals.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Array problem......PLEASE HELP!
by kennethk (Abbot) on Jan 05, 2009 at 20:37 UTC

    The code:

    use strict; my @files = ("08","09"); for my $file (@files) { my @command = `ls -l|grep $file`; print "$file $#command\n"; }

    works as expected on my box. Your problem is probably in the " of the script...."

    Update:Please create responses, rather than modifying your question. As it stands, your posted code will not work because the $file you pass to your system statement is not a file name. If you swap it for a command element, it still won't work because your ls -l returns more than just file names.

      I have updated the question.can you check if that helps to understand the problem. Thanks in advance.
        You have not updated your question since I updated my response.
Re: Array problem......PLEASE HELP!
by swampyankee (Parson) on Jan 05, 2009 at 22:06 UTC

    Why must you use the array @files? This strikes me as a rather pointless constraint. What are you trying to do?

    Dealing with your attempt to read the files in your directory: as said by moritz, use glob. It's what it's made for. Your first step would look something like this:

    my $files = join(" ", (glob("0[89]-*.txt")));#I suffer from parentheti +caphilia. sorry
    @files will now contain a blank-separated list of files which start with 08 or 09, followed by a hyphen and ending with '.txt'. You could then pass this to your ls -l command (the one in the backticks) and do the post-processing from your routine.

    Alternatively, you could use opendir, readdir, and grep, resulting in something like this:

    opendir(my $DH, "."); # opens pwd @files = grep {/^0[89]-.+\.txt$} readdir($DH); closedir($DH);

    But it would be a really good idea to tell the Monks–many of whom actually like explaining the nuances of Perl if the question is couched appropriately–what your goal is.

    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

      Hi there, I dont want to join 08 and 09 files together. At the first loop i need the 08 files where i will process only files and gererate a report. At the second loop i require the 09 files and output a report.
        Try this:
        #!/usr/bin/perl use warnings; use strict; use Data::Dumper; opendir(DIR, '.'); my @files = grep !/^\.{1,2}$/, readdir DIR; my @files8 = grep /08/, @files; my @files9 = grep /09/, @files; closedir DIR; print "@files\n"; my %commands; #maybe use a hash print "08 files\n"; foreach my $file (@files8){ $commands{'08'}{$file} = `ls -l|grep $file` } print "09 files\n"; foreach my $file (@files9){ $commands{'09'}{$file} = `ls -l|grep $file` } print Dumper([\%commands]);

        I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Array problem......PLEASE HELP!
by zentara (Archbishop) on Jan 05, 2009 at 21:01 UTC
    Maybe this will shed some light:
    #!/usr/bin/perl use warnings; use strict; opendir(DIR, '.'); my @files = grep !/^\.{1,2}$/, readdir DIR; #no . or .. @files = grep /08|09/, @files; #only 08 09 files closedir DIR; print "@files\n"; my @commands; foreach my $file (@files){ my $string = `ls -l|grep $file`; push @commands, [$file, $string]; # push arrayref } print join "\n", @commands,"\n"; foreach my $arrayref( @commands){ print $arrayref->[0]. "\t=\t". $arrayref->[1],"\n"; }

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      Hi there, I have updated some things in my question? can you please help?
        First of all, I have no idea what you are trying to do. If you are running a second Perl script, thru system, you are going about it wrong. Why not combine all operations into one script?

        The problem we are all having, is that your question dosn't seem to make sense; and it's hard for experienced programmers to warp their minds into fixing some sort of Rube Goldberg code contraption.

        What you should do, is show what is in those files, even if just a few sample lines. Then say what you want to do with the files and what output do you expect?

        Once we have a clear idea of what you need to do to the files, and what they contain, we can give you a good answer. Otherwise, we are just trying to figure WHY you are doing it your way? Finally, use warnings, and strict. Many problems, like yours are caused by global variables acting unexpectedly, and it shows good faith on your part, to use warnings and strict.

        I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Array problem......PLEASE HELP!
by gasho (Beadle) on Jan 05, 2009 at 20:48 UTC
    $StartDirectory='\cr1_qc\Harness\MyTextFiles'; @files = `ls -l $StartDirectory\\0*.txt` ; foreach $file (@files) { print "$file\n"; }
    (: Life is short enjoy it :)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://734265]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2023-06-01 17:00 GMT
Find Nodes?
    Voting Booth?

    No recent polls found