brawal128 has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|