Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^7: I need help with opening a directory and reading files in that directory.

by shadowsong (Monk)
on Sep 09, 2015 at 22:51 UTC ( #1141480=note: print w/replies, xml ) Need Help??


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 fresh
but 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

    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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1141480]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2019-05-26 05:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (153 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!