in reply to Re^7: 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.

Shadowsong, I appreciate all of the help that you have given me. The below code is what I was able to get to work and print out successfully. I did use some of the items that presented in your previous post. My issues were around setting up the hashes and keys. But, we will see what the instructor says about what I came up with. It is nice to know that there are folks out there willing help a newbie such as myself.

#!usr/bin/perl -l use strict; use warnings; use Data::Dumper; my ($artist, $song_title, $filepath, %length,); $filepath = 'Y:/perlscripts2/software/perl2/music'; opendir DH, $filepath or die $!; chdir($filepath); while ($_ = readdir(DH)) { next if $_ eq "." or $_ eq ".." or $_ !~ m/\.txt$/; next unless -f $_ and -r $_; open FH, '<', $_ or die "could not open file: $_\n"; my ($artist, $song_title) = split /\-|\.txt/; $length{$artist}{artist} = $artist; $length{$artist}{song_title} = $song_title; while (my $line = <FH>){ my ($album, $minutes, $seconds, $genre) = split ':', $line, 4; $length{$artist}{album} = $album; $length{$artist}{minutes} = $minutes; $length{$artist}{seconds} = $seconds; $length{$artist}{genre} = $genre; } close FH; } foreach my $artist ( sort keys %length ) { print "$length{$artist}{artist} has a total $length{$artist}{m +inutes}:$length{$artist}{seconds} of music.\n"; }

Thanks, Brandon

  • Comment on Re^8: I need help with opening a directory and reading files in that directory.
  • Download Code

Replies are listed 'Best First'.
Re^9: I need help with opening a directory and reading files in that directory.
by shadowsong (Pilgrim) on Sep 10, 2015 at 21:42 UTC

    Brandon,

    I'm glad you got it working in the end (and didn't quit, like so many others do).

    Any questions/issues/thoughts feel free to ask us Monks.

    Best Regards,
    Shadowsong