Perlaccess : Tutorials : Variables
Arrays (3.2)
by Jacob A. Wheeler
http://www.bigresources.com

If you plan on working with flat-file databases you will want to learn to take advantage of arrays.

  1. @names (whole array)
  2. @names = ('Me','Myself','I','You')
  3. $names[1] #Would equal: Myself

The following code will demonstrate howto split an array:

  1. open(FILE,"file.txt");
  2. @file = <FILE>;
  3. foreach $a (@file) { ($first,$last,$email) = split(/[|]/,$a); }
  4. close(FILE);

Click here to view the contents of file.txt

Once you split each line of the array using the above code, you can then print it out, run tests on it, or do anything else you can think of with it! Checkout a few examples below:

  1. open(FILE,"file.txt");
  2. @file = <FILE>;
  3. foreach $a (@file) { ($first,$last,$email) = split(/[|]/,$a);
  4. print "Hi $first $last, How are you?\n";
  5. print "Mind if I email you at $email?\n";
  6. exit;
  7. }
  8. close(FILE);

The $first, $last, and $email variables will all be filled in with the data from that current line in the array.