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

Dear Monks, I'm trying to list an array which contains a list of files in a vertical way. I use "\n" and instead it list the files from right to left. so they don't get listed for example : abc.ac cdf.dg ert.cf sdf.er How could I accomplish the listing above..
#! perl -w use strict; my $line = 'C:\08\00004DC4.013'; opendir MYDIR, $line or die $! ; my @allfiles = grep { $_ ne '.' and $_ ne '..' } readdir MYDIR ; my @files = grep { !-d } @allfiles ; my @dirs = grep { -d } @allfiles ; print "Current directory contains " . @files . " files and " . @dirs . " directories.\n" ; print "@allfiles\n";

Replies are listed 'Best First'.
Re: list array
by tachyon (Chancellor) on Feb 19, 2003 at 01:50 UTC

    TIMTOWDI

    @ary = 0 .. 10; print "$_\n" for @ary; print map { $_.$/ } @ary; print join $/, @ary; { local $, = "\n"; print @ary; } { local $" = "\n"; print "@ary"; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      You missed one:)

      C:\test>perl -le"@a=1..10; print for @a"

      I find the number of times I don't want a newline after a print to be very much smaller than the number of times I do, so most of my scripts have the -l switch on the shebang line. I find it easier to disable it on the rarer occasions when I don't want it.

      { local $\; print for @a; }

      Examine what is said, not who speaks.

      The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: list array
by Cabrion (Friar) on Feb 19, 2003 at 01:09 UTC
      Thanks fro the info. Question if I want to only list *.rtf files out of the list. should I be using hash or could it be done another way...
        print join( $/, grep( /\.rtf$/, @allfiles ));
        as well as all the variations indicated by tachyon, just using the result of grep() in place of the original array.
Re: list array
by Aristotle (Chancellor) on Feb 19, 2003 at 01:39 UTC
    print map "$_\n", @allfiles;

    Makeshifts last the longest.

      Thanks for the info. Question if I want to only list *.rtf files out of the list. should I be using hash or could it be done another way... In my code I'm listing all the files but I only want to list "*.rtf" do you have any suggestions.
        Yes, see grep.
        print map "$_\n", grep /[.]rtf\z/i, @allfiles;

        Makeshifts last the longest.

Re: list array
by djantzen (Priest) on Feb 19, 2003 at 01:44 UTC
    Simpler:
    print "$_\n" for @allfiles;

    "The dead do not recognize context" -- Kai, Lexx
      Inhowfar is that simpler? It loops over @allfiles just like the other solutions, but it calls print repeatedly, once per array element. The other solutions do not.

      Makeshifts last the longest.

        Conceptually simpler. A newbie probably shouldn't be using map to do this sort of thing.
        "The dead do not recognize context" -- Kai, Lexx