in reply to Re: 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, this is what I came up with. The directory is being read and information is being stored in memory, but I'm have having trouble opening the .txt files to read the data in those files. I thought I was doing it the right way by using hashes to keying a value. But, it is still not reading the data in the .txt file. Am I doing something wrong or am I still not reading what is in the file?
#!/usr/bin/perl -slw use strict; use warnings; use Data::Dumper; my %length; my %songs; opendir DH, "Y://perlscripts2/software/perl2/music" or die $!; # open +current directory while($_ = readdir(DH)){ # don't evaluate special dirs '.' & '..' next if $_ eq "." or $_ eq ".."; if ($_ =~ /\.txt/){ chomp $_; my ($artist, $song_title) = split '-', $_, 3; $length{$artist}{$artist} = $artist; $length{$artist}{$song_title} = $song_title; print "$_"; # print this file/directory's name } # as a by-product of readdir; use the file's stat info # to check whether this is indeed a regular file we can # open for reading/further processing if (-f $_) { open FH, "<$_" or die $!; print "output for file: $_\n"; while (my $line = <FH>) { print "$line\n"; if ($line =~ /\.txt/){ my($album, $minutes, $seconds, $genre) = split ':', $l +ine, 4; $songs{$album} = $album; $songs{$minutes} = $minutes; $songs{$seconds} = $seconds; $songs{$genre} = $genre; print $album, "\n"; } close FH; } } }closedir DH; foreach my $artist ( sort keys %length ) { print "$artist is the name of the Artist\n"; } foreach my $album ( sort keys %songs ) { print "$album is this now\n"; }
Thanks, Brandon
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: I need help with opening a directory and reading files in that directory.
by shadowsong (Pilgrim) on Sep 08, 2015 at 22:15 UTC | |
by brawal128 (Novice) on Sep 09, 2015 at 04:12 UTC | |
by shadowsong (Pilgrim) on Sep 09, 2015 at 09:37 UTC | |
by brawal128 (Novice) on Sep 09, 2015 at 14:46 UTC | |
by shadowsong (Pilgrim) on Sep 09, 2015 at 22:51 UTC | |
| |
|
Re^3: I need help with opening a directory and reading files in that directory.
by Anonymous Monk on Sep 08, 2015 at 23:03 UTC |