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

I'm having trouble figuring this one out here... Basically, like my previous post, I'm having perl search a text database for a specific name. Only, what I need it to do is to search the database for a specific name, then the next 3 lines will be assigned to an array. So... if say "users.txt" looks like: Joe 2 9 8 and they typed in joe, there would be an array called @joe, and values 0-2 would be 2, 9 and 8 respectively. I for the life of me can't figure out how to do something like this. Any help would be GREATLY appreciated, I've been trying to figure out how to do this for the past week and don't even know where to start. tanks

Replies are listed 'Best First'.
Re: Text Databases
by athomason (Curate) on Jun 02, 2000 at 10:24 UTC
    I would recommend against naming variables based on dynamic input; that can get you into all sorts of troubles. A better idea (besides using DB files or an RDBMS, of course) would be to have a hash whose values are anonymous array references containing the data you need. As for separating the file data, you need to split the line on spaces. As far as I understand the question, I think this will work for you:
    use strict; my %users; open USERS, "<users.txt"; while (<USERS>) { chomp; my @line = split / /; $users{shift @line} = \@line; } close USERS;
    To get back at the data, you need to dereference the array, i.e. @{$users{$name}}. For example, to get a dump of the users,print join ' ', ($_, map($_, @{$users{$_}}), "\n") for (keys %users);Of course, you'd be better off using a DB file if you can; flat files require you to reinvent the wheel in a lot of ways. Provided tied hashes properly handle array references (which I'm not sure of; I stick to RDBMSs), this should extend to that pretty easily.
Re: Text Databases
by Snowdog (Sexton) on Jun 02, 2000 at 11:13 UTC
    Start with the library. The intro to data structures is excellent. It would be easy to make it a list of lists just through your file into an array and then search by position. @C = <INFILE>; $start = 0; foreach $foo(@C) { until ($start > $#@C) if($foo$start=~/joe || bob/) {push (@NewUser,$foo$start); $start++; push (@NewUser,$foo$start); $start++; push (@NewUser,$foo$start); $start++; push (@NewUser,$foo$start); $start++;} $start++;} {push (@AllUsers, @NewUser;} you get @AllUsers = ( 'joe','2','9','8', 'bob','4','u','2', ); or a hash where Joe is the key and the value is a list of the next three lines. Just my little thoughts. Gandalf tea Wednesday.
      need to work on that formattin. Gandalf tea Wednesday.
      @C=<<>INFILE<>> lost in the formatting. Sorry.
        ok I really need to work on the formattin. one last try for the night. @C=<INFILE>
      See the Site How To for info on formatting stuff. The easiest way is to embed code blocks in <CODE></CODE> tags; everything inside them will be spaced as intended and it won't strangely interpret any of your characters. Also, rather than posting 4 follow-ups, if you go to the actual node of your original reply, you can modify it in most cases.