Home  |  Linux  | Mysql  | PHP  | XML
From:jbl Date:Tue Mar 16 15:47:24 2010
Subject:2 off in array count and index
I am reading a directory and creating an array of .png file names. I
know for a fact that there are 1025 png files in the folder
With $count = @files I get 1027
With $last = $#files I get 1026
$files [0] = .
$files[1] = ..
I have heard of being one off by not taking into account the
difference in number of elements and the index of the last element but
why am I 2 off?? As far as the dots in elements 0 & 1, I am sure that
that is something to do with the path, but what??

#!/usr/bin/perl -w
use strict;

opendir(FILENAMES, 'F:/_D_drive_IDE/TIGER2009_working_folder/
perl_scripts_copy')
||die "Cannot open directory $1";
my @files = grep !/^\.png/, readdir FILENAMES;
closedir FILENAMES;

$last = $#files;
$count = @files;

print 'Number of elements =>',$count,"\n";
print 'Index of last element =>',$last,"\n";

print 'array @files index[0] =>'.$files[0],"<=\n";
print 'array @files index[1] =>'.$files[1],"<=\n";
print 'array @files index[2] =>'.$files[2],"<=\n\n";
print $files[$lastIndex];
# actual files
= 1025
#the output is:
#Number of elements =>1027
#Index of last element =>1026
#array @files index[0] =>.<=
#array @files index[1] =>..<=
#array @files index[2] =>31052_50068.png<= This is the first
actual file name

Navigate in group perl.beginners.cgi at sever nntp.perl.org
Previous Next


Your recent visits
RE: content type headers
Getting forward slashes and hyphins past the taint check
Problem uploading large files (broken storing)
RE: how to add support of Msql and CGI in Apache
RE: Global scope



  
© No Copyright
You are free to use Anything, but please consult your advocate before doing so as this website
also list content from other sources which may be copyrighted.
Site Maintained by Zareef Ahmed
Powered By PHP Consultants