Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Simple file to array

by FoncÚ (Scribe)
on Jul 08, 2002 at 19:19 UTC ( [id://180280]=perlquestion: print w/replies, xml ) Need Help??

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

I want to have a simple text file with a list of email addresses in it (and one for real names as well), one per line, nothing fancy, and I want to read that from a Perl script so that it places it directly into an array. I.e., I want to read the file addys.txt and place each line into it's respective spot in the array @email_addys. I actually want to know how this works, too, not just accomplish something at work, so if you could really explain it well, that'd be great. I understand filehandles a *little* (they're still very confusing to me). Thanks!

Replies are listed 'Best First'.
Re: Simple file to array
by DamnDirtyApe (Curate) on Jul 08, 2002 at 19:25 UTC
    open( FH, 'addys.txt' ) or die "Couldn't open file: $!" ; my @email_addys = <FH> ; close( FH ) ;

    Pretty simple, probably doesn't really need explaining (but let me know if it does.)

    D a m n D i r t y A p e
    Home Node | Email
      Sure, this could use some explaining. But not too much. :)

      When using a filehandle in list context (such as assigning it to an array), each line is treated as a list element and assigned to the array just like if you were doing my @email_addys = ('', '', ... ).

      One of the problems here is that this doesn't strip the linefeed character from the end of each line, so that what we end up with is an array like ("\n", "\n") when reading this from a file. We probably want to clean up the array, which we can do quickly with for( @email_addys ){ chomp }; at some point after the assignment.

        We probably want to clean up the array, which we can do quickly with for( @email_addys ){ chomp }; at some point after the assignment.

        `perldoc -f chomp` tells us an easier way: chomp @email_addys.

      FoncÚ asked specifically for an explaination so I will do a little expaining of DamnDirtyApe's code. I'm sure any mistakes I make will be corrected Immediately.
      open( FH, 'addys.txt' ) or die "Couldn't open file: $!" ;
      This line opens the file "addys.txt" to filehandle FH. The function open will return a true value if the file open's sucessfully and a false value if it fails. If the file opens sucessfully the or will "short circuit" meaning since it already has a true value there is no need to evaluate the rest of the statement. If the open fails the die will be executed and the program will quit giving an error message. This is a pretty common way of testing that an open succeeded. You should always check the return value of an open it will usually save you a lot of debug time.
      my @email_addys = <FH> ;
      This line does the bulk of what you ask for. The my @email_addys creates a lexical array variable to hold the email addresses. The diamond operator ( <> ) on a filehandle reads the next line from the file. If it is being used in what is called a list context it will slurp up the whole file and return a list where each line of the file will be one of the elements of the list.
      List context means that whatever it is feeding is expecting a list of items. Since we are assigning to an array variable this is a list context. If we were asigning to a scalar variable the diamond operator would only return a scalar containing the next line of the file (or undef if we were past the end of the file). Understanding context is one of the most important parts of understanding perl.
      close( FH ) ;
      Close the filehandle.

      Notes: Each element of the @email_addys array will have a trailing newline "\n" (except maybe the last one depending on whether the last line of the file had a newline at the end.)



        A nice explination...this and everyone else's has helped me a lot...thanks! As I understand lexical variables, they would be the same as not passing a variable back into the programme from a subroutine in, say, BASIC, right? It just remains inside whatever sub or block and isn't global? But in this case, to allow it to be global, I simply leave of the my? Just making sure I have things straight. Also...the way I had it chomp above doesn't seem to work...Can someone explain why not?
      Oh, um...would having the @ in the email address cause a problem, or does it assume single quotes? Or, will I have to put single quotes around the email addresses in the outside file?
        One more thing--would this snippet work, or will it not chomp like that?:
        open( FH, 'addys.txt' ) or die "Couldn't open file: $!"; chomp (my @email_addys = <FH>); close( FH );
Re: Simple file to array
by particle (Vicar) on Jul 08, 2002 at 19:49 UTC
    require 5.006; # needs recent perl for three-argument open my $infile = 'addys.txt'; my @email_addys; { # enter new scope # create a filehandle visible in the current scope local *FH; # open the file for reading, die on errors # three-argument open is safer than two-arg -- # it safely handles filenames that begin with '<', '>', etc. open( FH, '<', $infile ) or die "Couldn't open $infile: $!" ; # process the file, line by line # <> returns undef at EOF, and the loop ends while( defined(my $line = <FH>) ) { chomp $line; # append the current line from the file to an array push @email_addys, $line; } # close the filehandle, processing is complete close( FH ); } # exit scope # FH is not visible here.
    while damndirtyape's example may be more idiomatic, this one uses less behind-the-scenes magic-- i think it's a bit easier for a newbie to understand (and it's safer.)

    see open, die, perlop, and perlvar for more details.

    good luck!

    ~Particle *accelerates*

Re: Simple file to array
by waxmop (Beadle) on Jul 08, 2002 at 20:30 UTC

    write the file with addresses like so:

    then to load this into 2 arrays ( 1 for names, 1 for email addresses) do something like this:

    open (IN, "filename.txt") or die "can't open filename.txt!"; while ( $line = <IN> ) { @parts = split /___/, $line; push @names, $parts[0]; push @addresses, $parts[1]; } close IN;

    and now you've got 2 arrays: @names has the names, and @addresses has the addresses. They are indexed by the same number, so joey's name is stored at


    and his address is stored at $addresses[0]

    another exciting way to do this would be to use a hash.

    open (IN, "filename.txt") or die "can't open filename.txt!"; while ( $line = <IN> ) { @parts = split /___/, $line; $name = $pts[0]; $address = $pts[1]; $hash{$name} = $address; } close IN;

    Now, to get joey's address, just do this:

    print "joey's address is ", $hash{"joey"}, ".\n";

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-07-25 20:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.