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

It is a simple data base consisting of first elements all 10 diget numbers followed by anouther three elements in each line. I open my data base and extract the contents thus.
"$data_file="learn.data"; open(DAT, $data_file) || die("Could not open file!"); @raw_data=<DAT>;
This gives me the contents of my database as an array. I also have a ten digit number, I now need to search the array and ask if this number is in the array. If so give me the other elements in that line. Simple for some hard for me...Please help.

update (broquaint): title change (was simple for some)

Replies are listed 'Best First'.
Re: simple for some
by derby (Abbot) on Nov 22, 2002 at 18:48 UTC
    grep

    -derby

    update: Okay how about

    @results = grep( /^$key/, @raw_data );
    where key is the number your looking for. Sure you could do more fancy things like munge the array into a hash or DBFile but grep should suffice. All the matching records will be in results, then just roll through those to output the data you want:

    foreach( @results ) { $record = $_; # strip the beginning key $record =~ s/^\d+//; print $record; }
      Brilliant Derby It dose everything I asked for. There is the called string right there in front of me. Frustrating I can see the elements but just can’t get hold of them. I didn’t really need to be rid of the first element so I tried
      foreach( @results ) { $record = $_; print $record; } @test = $record; print @test[0];
      Then it prints the entire string again. Then
      foreach( @results ) { $record = $_; print $record; } @test = $record print @test[1];
      Then it prints nothing in the second print. Then
      foreach( @results ) { $record = $_; print $record; } @test = $record $scalar = $array[0]; print $scaler;
      but nothing. I just need to get hold of the elements individually so I can do something with the little critters. May god bless you guys.
Re: simple for some
by Sihal (Pilgrim) on Nov 22, 2002 at 20:32 UTC
    late guess :
    my $number_to_guess = X ; map { $_ =~ /^(\d{10})/ ; $number_to_guess == $1 and die $_ } @row ;
    OK. maybe thats not the way to do it.
      Or maybe :
      my $number_to_guess = X ; my %stuff ; map { $_ =~ /^\d{10}(.*)/ ; $stuff{$_} = $1 ; } @row ; } @rows; print $stuff{$number_to_guess} ;
      Or maybe not