in reply to Re^6: I need help with opening a directory and reading files in that directory.
in thread I need help with opening a directory and reading files in that directory.
Brandon,
Welcome to the wonderful world of programming - don't be discouraged, this happens to the best of us
--
OK, from what you have posted - you're main problem is a file path problem. My advice?
start freshbut leave your current script as-is so you can see where you're coming from. Have a look at the snippet below which shows how I have used chdir() to handle the file path issue:
#! perl -slw use strict; use Data::Dumper; my ($album_id,$track_id,$filepath,%music); $filepath = 'Y:/perlscripts2/software/perl2/music' opendir DH, $filepath or die $!; # magic one-liner to "fix" your path problems chdir($filepath); while(readdir(DH)){ # don't evaluate special dirs '.' & '..' # or anything that doesn't end in .txt next if $_ eq "." or $_ eq ".." or $_ !~ /\.txt$/; # explicitly test this item to see if it's a # regular file and if it's readable (notice the # use of the default filehandle test var '_') next unless -f $_ and -r _; # arrival here signifies we have a readable, # regular file. Extract its contents into a hash open FH, '<', $_ or die "could not open file: $_\n"; # use the actual filename to pull the album & artist my ($name, $artist) = split /\-|\.txt/; $album_id++; $track_id = 0; $music{$album_id}{ALBUM} = $name; $music{$album_id}{ARTIST} = $artist; while (my $line = <FH>) { # the lines of this file are of the format # <track>:<minutes>:<seconds>:<genre> my ($track, $minutes, $seconds, $genre) = split /\:/, $line; $track_id++; $music{$album_id}{$track_id}{TRACK}{TITLE} = $track; $music{$album_id}{$track_id}{TRACK}{DURATION} = [$minutes,$seconds +]; $music{$album_id}{$track_id}{TRACK}{GENRE} = $genre; } close FH; } print Dumper \%music; __END__
You're almost there mate. Keep at it.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: I need help with opening a directory and reading files in that directory.
by brawal128 (Novice) on Sep 10, 2015 at 21:20 UTC | |
by shadowsong (Pilgrim) on Sep 10, 2015 at 21:42 UTC |