Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Creating an array from a text file

by aufrank (Pilgrim)
on Aug 13, 2002 at 15:59 UTC ( #189841=note: print w/replies, xml ) Need Help??

in reply to Creating an array from a text file


Greetings and welcome to the Monastery! Your first node may very well be an interesting question, but it's hard to tell because it is a bit vague. Statements like "[use] each element of the array to perform some action" are hard for readers to respond to because they don't give enough details about exactly what you're trying to do. One way to help your fellow monks help you is to post code along with your question. You say that you have the actions working-- if we could see that code it would greatly help us to understand the nature of your question.

That said, there are a few things that might help you. The first is reading some of the docs available at this site (use the search box), at, or included with your local perl distribution (type perldoc perldoc for meta help. look into the -f and -q options, specifically).

It seems like what you're looking for is very easily accomplished. perldoc -f open would be a good place to learn how to access a file. As for getting the contents of a file into an array, consider this block of code:

sub words_from_file { my $file_path = shift; my @word_list; open WORD_FILE, "< $file_path" or die "Could not open $file_path: $!\n"; @word_list = map { split /,\s*|\n/ } <WORD_FILE>; close WORD_FILE or die "Could not close $file_path: $!\n"; return \@word_list; }

You might also want to read the node that inspired that code, which can be found here. Also, if you are trying to perform some operation on every element of the array once you have created it, you should definitely get comfortable with map-- one of the most useful tools in perl, IMO :) Check out this excellent tutorial by jeffa for help with map.

If you have any questions about what that code is doing, please feel free to respond and I'll be more than happy to explain.

good luck!

UPDATE: I should have explained that this code will make each separate word an element of the array-- to use whole lines rather than individual words, definitely use the far simpler code in Ovid's response. If you do go line by line, be sure to check out chomp. I also added a close statement to my code, 'cause it should have been there in the first place.

Replies are listed 'Best First'.
Re: Re: Creating an array from a text file
by Ovid (Cardinal) on Aug 13, 2002 at 16:21 UTC

    Well, heck, if we're going to go down the road of confusing the person, we can make each individual line an array ref of elements split on predetermined separators :)

    #!/usr/bin/perl -w use strict; use Data::Dumper; $_ = [ grep {$_} split /[\s,:]/ ] for my @data = <DATA>; print Dumper \@data; __DATA__ foo bar, baz this that: those

    Note: that was tongue-in-cheek, but I can see that point that Abigail-II made.


    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Re: Creating an array from a text file
by mikevanhoff (Acolyte) on Aug 13, 2002 at 16:33 UTC
    aufrank and all responders; Thank you for the explaination and suggestions on asking questions. All of these responses will be of some help, even the oneliner. Although I understand the concepts of opening and reading from file, understanding how the array is created, and actually substuting the elements into my code is vague to me. I have included my code so that my question is more explicit. I am trying to delete the files in the reports directory that are x days old. I have 67 directories under /opt/web that all have reports directories. I would like to place all of these paths into a text file to be read into the array, and have the following actions performed.
    # open the directory for reading chdir "/opt/web/hr83tst/reports"; opendir(REP, "/opt/web/hr83tst/reports") || die "Cannot open the dire +ctory /opt/web/hr83/reports $! "; open(remlog,">/Scripts/dir-removed.log"); # open a file to keep as a +log of directories removed. print remlog "Directories removed this date : ",`date`, "\n"; # list the contents of the directory. These should be directories. while ($name = readdir(REP)) { if (-M $name >= 10) { print "$name\n" unless($name eq "images"); print remlog "$name\n" unless($name eq "images"); `rm -r $name` unless($name eq "images"); # THIS WILL REMOVE THE D +IRECTORY AND ALL OF ITS subdirectories and Files. } } close remlog; closedir(REP)
      You can also use the File::Find module as follows to populate your array:
      use File::Find; find sub { push @reportdirs, $File::Find::name, if -d && /^reports$/ } +, "/opt/web";
      That way, you won't have to modify your file every time your directory structure changes. You then can readdir each directory in the array to begin processing the files it contains.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://189841]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-12-03 08:46 GMT
Find Nodes?
    Voting Booth?

    No recent polls found