I need some advice/help on getting this to work. I'm taking an on-line Perl Training Course and I am stumped on getting my code to work. Please be kind in your responses as I'm new to Perl. I am trying to open a path to a folder on one of my drives and then read the files to get the information and print a statement out from those files. I was instructed to use the <> diamond operator to read the directory and to also read the files. I have setup hashes to set the key and values up or at least that's what I thought I was doing. I did write a script that used opendir to get to the files and print them out. The opendir has been the only thing that worked so far. However, I'm having a hard time creating/reading the files to do something with and create the $hash{$key} = $values. But, is there a way to use <> to open the directory path and then use the <> to read the files and do something with the information. Any help on this would greatly appreciated, as I know there are some Perl Guru's on here.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $filename; my $FH; my %length; my %songs; opendir DH, "./software/Perl2/Music" or die "Could not open directory! +"; #open(my $file1, "<", "The Bravery-Believe.txt") or die "Could not ope +n file $!"; while($filename = readdir(DH)){ if ($filename =~ /.txt/){ chomp $filename; my ($artist, $song_title) = split '-', $filename, 3; $length{$artist}{$artist} = $artist; $length{$artist}{$song_title} = $song_title; #print $artist,"\n"; #print $song_title,"\n"; } #elsif ($filename =~ /.txt/){ #open FH, "<", $filename or die "Could not open file '$filename' $ +!"; } print Dumper %songs; print Dumper %length; foreach my $artist ( sort keys %length ) { print "$artist is the name of the Artist\n"; } foreach my $album ( sort keys %length ) { print "$album is this now\n"; }

Thanks, Brandon


In reply to 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.