Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

reading in a text file

by bzenker (Initiate)
on Jan 03, 2005 at 03:04 UTC ( [id://418863]=perlquestion: print w/replies, xml ) Need Help??

bzenker has asked for the wisdom of the Perl Monks concerning the following question:

I need to read in a .txt file with a list of part numbers into a Hash. How do I do that and then I will need to use that list to go through a bunch of directories and subdirectories find the files and move them into a different directory.. Can someone quickly write me some sample code for me to modify and try. Thanks for the help...bzenker

Replies are listed 'Best First'.
Re: reading in a text file
by chanio (Priest) on Jan 03, 2005 at 03:28 UTC
    It is easier to help if you provide your code and let us point you what you need to change.

    • First, write some code that would open,read one line and close
    • Then, write your sample line on a sheet of paper and try to split it to extract your valuable parts: code number for the hash key and directory path for finding the files.
    • Finally, write your code and do a sub that would receive the file's path , open it and read it and write it where you want it to be.
    • Then, add iteration to the line reading, call that sub with every value of the hash and that's all.
    It is easier if you start coding your first lines, than explaining it all with words.

Re: reading in a text file
by csuhockey3 (Curate) on Jan 03, 2005 at 05:30 UTC
    I had a similar task the other day (although it was dbm, it could just as well have been a text file). I had a bunch of information in these files that I needed to strip out and place in a hash -- namely county codes and their corresponding email aliases. Sounds like you would just need to modify this by using opendir / readdir etc as pointed out by chanio and prasadbabu to avoid hard coding the file name(s) and allow traversing the directory.

    Post some of your code and we can help you more, til then here is a little snippet that can give you something to start with:
    #!/usr/bin/perl -wT dbmopen(%ISO_Country, "ISO_Country", 0777) or die "Cannot create ISO_Country: $1\n\n"; dbmopen(%ISO_Email, "ISO_Email", 0777) or die "Cannot create ISO_Email: $1\n\n"; open FNAME, $ARGV[0] or die "Cannot locate file $ARGV[0]: $1"; while ($line = <FNAME>){ chop($line); @data = split /\t/, $line; $ISO_Country{$data[0]} = $data[1]; $ISO_Email{$data[0]} = $data[2]; } &displayData; dbmclose(%ISO_Country); dbmclose(%ISO_Email); sub displayData{ while (($key, $value) = each %ISO_Country){ print "ISO($key), Country($value)\n"; } while (($key, $value) = each %ISO_Email){ print "ISO($key), Email($value)\n"; } } ...
Re: reading in a text file
by prasadbabu (Prior) on Jan 03, 2005 at 04:40 UTC

    As chanio said, it will be easy for us to help you if you provide the code which you have tried.

    You go through the perl documentation part for open, close,opendir,readdir,split,hash etc.

    Also there are number of modules to help in reading files from and to a directory

    Prasad

Re: reading in a text file
by mkirank (Chaplain) on Jan 03, 2005 at 06:47 UTC
    my $path='/tmp/myfile.txt'; open(F,"$path") or die "Cannot open $!"; while(<F>) {## read each line ## you have the read line in the variable $_ ## you can store it in your hash } close(F); ## iterate over your hash ## check for the existence of the files if (-e $filename) { print "$filename exists!\n"; } ## move the files to the directories ## Check for the return value for errors

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://418863]
Approved by johnnywang
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found