I'm not sure your
substr's are doing what you think they're doing.
substr($string,1,-2) will remove the first character and the last two characters from a string. e.g.
perl -e 'print substr("string",1,-2)' prints
tri. Is this what you want to do? Or do you just want to remove the last two characters, in which case you could do
substr($string,0,-2).
Also, I'd be more inclined to remove newlines from file- and path-names with chomp while populating the array. This way you don't have to worry about accidentally removing characters you want to keep with substr. For instance (not sure if this is the way you're going about this), if I had a file produced by doing ls -AR directory_name > filelist in the shell I would put it into an array like so:
use strict;
use warnings;
use autodie;
my $file = shift @ARGV;
open my $fh, '<', $file;
my @files;
while ( my $line = <$fh> ) {
chomp $line;
push @files, $line unless $line =~ /^\s*$/;
}
.. and I'd use
File::Spec to assemble the pathnames so I didn't need to worry about concatenating pathnames and filenames and slashes and whether there was already a trailing slash in the direcory name, etc:
use File::Spec;
my $dirname;
for my $name ( @files ) {
if ( $name =~ s/:$// ) { # use trailing : to id dirname
$dirname = $name;
}
else {
my $fullpath = File::Spec->catfile( $dirname, $name );
# slurp or do whatever with file at $fullpath
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.