-----Original Message-----
From: Eric Preece [mailto:[EMAIL PROTECTED]]
Sent: Friday, July 26, 2002 4:09 PM
To: [EMAIL PROTECTED]
Subject: A basic question about arrays


Hello,

Yet another newbie question. I have a file, each line is a pipe delimited
list (user name and password). I want to open the file, search through each
line using the first entry as a key (so this may turn into a question about
hashes, but I am not sure). If I get a positive result I want to execute
some logic, if not then bail out. My confusion is my inability to properly
search through each line. If I open the file and read it into an array I get
each line as an element of the array, not what I wanted. ex:

use strict;
use warnings;
use CGI qw/:standard/;
my $path = "D:/crap/names.txt";

open(FILE, $path) or die ("Couldn't open file - !$\n");
@array = <FILE>;
close (FILE) or die ("Couldn't close file - !$\n");
print $array[0];

This prints the first element of the array - name|value. What I want is to
be able to search for a name then if I have a name match perform some
operations (comparing the value with a var). I need to be able to search
through the name array elements for a sub-set of the array elements. Make
sense?

I suspect I will need to put the data into a hash but I do not know how to
convert an array where elements = name|value into a hash = key|value.

I am certain that there is an easy solution to this but my n00biness is
preventing me from seeing it. Suggestions? Thanks in advance!

Eric

-----My Response-----

If you only want to go through the file once, it may not make sense to go to
the trouble of creating a hash.  Bottom line is that you probably want
something like this:

# loop through the file line by line
while (<FILE>) {
# strip the trailing newline from the current line
        chomp;
# split the line into name and password
        ($name,$pass)=split /\|/;
}

Inside the while loop you will either want to do your check or you will want
to create your hash.

                                /\/\ark


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to