Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re^2: I need help with opening a directory and reading files in that directory. by brawal128
in thread I need help with opening a directory and reading files in that directory. by brawal128

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-28 15:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found