jmrhide-p...@yahoo.com wrote:
Hello and thanks for volunteering your time!

Hello.  (And it's Perl, not PERL.  :-)


I'm returning to PERL after about a year and am struggling to remaster  some
syntax:


#!/usr/local/bin/perl
use warnings;
use strict;
use diagnostics;

# Converts current directory to a list of links

my @dir;
my $name;
my $i = 0;

opendir DH, "." or die "\nCouldn't open current directory: $!\n";

while ($_ = readdir(DH)) {
next if $_ eq "." or $_ eq ".." or -d $_ or $_ eq "zlinks.pl" or $_ eq
"zout.txt";

The entries "." and ".." are directories so you could simplify that to:

next if -d $_ || $_ eq "zlinks.pl" || $_ eq "zout.txt";


$dir[$i++] = $_;

That is usually written as:

push @dir, $_;


}

open FH, ">  zout.txt" or die $!;

foreach $i (@dir) {

That is usually written as:

foreach my $i (@dir) {

What you are doing here is iterating through the contents of @dir and putting each element, in turn, into the $i variable. So for example if @dir contains ( "dir1", "dir2", "dir3" ) then the first time through the loop $i will contain "dir1" and the second time through the loop $i will contain "dir2", etc.


my @title = split /\./, $dir[$i];

Here (and below) you are using the text value (name of the directory) as a numerical index into the array @dir, and a text value in numerical context will be 0 so you are always accessing $dir[0], the first element of the array. What you want is:

my @title = split /\./, $i;


$name = $title[0];

Or simpy:

my $name = ( split /\./, $i )[ 0 ];


print FH "<P ALIGN=CENTER><A HREF=\"$dir[$i]\"
TARGET=\"_blank\">$name</A></P>\n";

}

I get an error for the line<<my @title = split /\./, $dir[$i];>>
Argument "filename.ext" isn't numeric in array element

Well, I wasn't expecting it to be, but apparently I implied it and can't  figure
out how.


Interestingly, the program executes with the error but only considers  the first
file it finds in the directory. If there are N files, it will  print N links,
but all using the first file name.



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to