Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

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

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":



  • 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 sharing their wisdom with the Monastery: (6)
As of 2024-04-25 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found