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

Really stuck this time. I have a flat text file database ( reff.data ). The left side will always be different numbers while the right side may be repetitive. The simple part is I inject to the script a number From the left side and ask for the corresponding number on the right side. I inject the number as $line3 submitted from a form.
$data_file="reff.data"; open(DAT, $data_file) || die("Could not open file!"); @raw_data=<DAT>; @results = grep( /^$line[3]/, @raw_data); foreach( @results ) { $record = $_; # strip the beginning key $record =~ s/^\d+//; print "$record"; } close(I);
Great this returns $record which will print out the number corresponding on the right. Now the difficult part. I now need to search the left side for that number and be given again the corresponding number on the right. I have tried the same search for the variable $record thus.
$data_file="reff.data"; open(DAT, $data_file) || die("Could not open file!"); @raw_data=<DAT>; @results = grep( /^$record/, @raw_data); foreach( @results ) { $record2 = $_; # strip the beginning key $record2 =~ s/^\d+//; print "$record2"; } close(I);
But that printed out the whole database I can inject a number from the left and be given the Corresponding right. But then I need the script to go on and find that corresponding number in the left, and give again the corresponding right and so on and so on. I am not a module man but I have tried many things Some with surprising results However after being bogged down with this problem For about three months now, I really need to get past this and Soldier on . God bless Ye All…Nasa.

edited: Sat Feb 15 20:53:30 2003 by jeffa - title change (was: Bewildered.)

Replies are listed 'Best First'.
Re: Problem with (recursively?) searching flat file database
by Joost (Canon) on Feb 14, 2003 at 16:32 UTC
    I guess you didn't post the complete program in here:
    @results = grep( /^$line[3]/, @raw_data);
    Where is the @line array coming from in this code?

    Anyway, if the data file contains lines like:

    1235 some text data 784783 some other data
    You can do something like:
    my $search_for = "text"; my $re = qr/^(\d+)\s+\Q$search_for\E/; open D,"data.txt" or die "Cant open file: $!"; while (<D>) { /$re/ or next; print "$1\n"; } close D;
    to print all the numbers that correspond to the text "text". (and will not print records that have a number that happens to match your search query).

    See also perlop and perlre.

    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: Problem with (recursively?) searching flat file database
by jasonk (Parson) on Feb 14, 2003 at 16:21 UTC

    If your second example code is returning the whole database, I would guess that $record doesn't contain what you think it contains (you would get the whole file returned if it was an empty string). Other than that try clarifying the problem (maybe by posting some sample data showing what the input file looks like and what the output you are trying to get looks like), as it isn't very clear what you are attempting to do.

    p.s. you will get better responses if you pick better titles for your questions, 'Bewildered' doesn't describe the problem.

Re: Problem with (recursively?) searching flat file database
by dws (Chancellor) on Feb 14, 2003 at 19:18 UTC
    There's a discrepancy between your narrative and your code fragments. Do you have form input in $line3 or $line[3]? They're not the same thing.
    @results = grep( /^$line[3]/, @raw_data);
    will match everything if $line[3] is not defined.

    And even if $line[3] is defined, it's not going to do what you want. Let's say it holds "1". The regex will then match strings that start with "1", "12", "123", etc.

    I suspect that you really want to do something like

    @results = grep( (split(@_))[0] eq $line[3], @raw_data);