Brandon,

Welcome to the Monastery. Let's see if we can get your script working as expected - let us break it down one step at a time and solve each problem in turn.

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

We do not use <> when opening a directory path. Here is a small script to open a directory, read the contents of that directory and list/print the files therein

#! perl -slw use strict; opendir DH, "." or die $!; # open current directory while($_ = readdir(DH)){ # don't evaluate special dirs '.' & '..' next if $_ eq "." or $_ eq ".."; print "$_"; # print this file/directory's name } __END__

One can also use a construct known as a glob to evaluate the contents of a directory

#! perl -slw use strict; my @files = glob("*"); print "Files matched via glob pattern *: @files\n"; __END__
and then use the <> to read the files and do something with the information.

Reading files and doing something with the information is relatively straightforward (but beware this can get hairy real fast based on what your particular environment is like - let's keep it simple for now. It's enough that you've been made aware that what we're doing here is scratching the surface).

#! perl -slw use strict; opendir DH, "." or die $!; # open current directory while($_ = readdir(DH)){ # don't evaluate special dirs '.' & '..' next if $_ eq "." or $_ eq ".."; 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; } close FH; } } __END__

Let us know if you have any issues w/ the syntax for using a hash.

p.s. Careful when using RegExps; certain characters carry special meaning and need to be "escaped". So instead of having

if ($filename =~ /.txt/)

You might want to have:

if ($filename =~ /\.txt/)

In reply to Re: I need help with opening a directory and reading files in that directory. by shadowsong
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":



  • 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.