in reply to Using a lookup table(?)

Assuming I understand you correctly, and you are using a file of the format:

server1 Webserver server2 DMZ Server server4 Oracle Server
you could do the following:
use strict; use Tie::File; | mumble. | sub get_comments { my $host=shift; my @ry=(); tie @ry,"Tie::File","myfile.txt" or die "myfile.txt: $!"; my @lines = grep $host,@ry; my $line = shift @lines; # should only be one. my @f=split(/[\s\n\t]+/,$line); shift @f; untie @ry; return join(" ",@f); }
Keep in mind I haven't tested this and this is off the top of my head. There's some error detection logic missing in that code (for instance, nothing for the value $host in the input file) and stuff like that.

Hope this is a help...


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Replies are listed 'Best First'.
Re^2: Using a lookup table(?)
by ikegami (Patriarch) on Feb 07, 2007 at 18:16 UTC

    Your ignored the fact that the search was case-insensitive.

    my $host = lc(shift(@_));

    Your grep is wrong.

    my @lines = grep { lc($_) eq $host } @ry;

    Why read the whole file everytime?

    use List::Util qw( first ); my $line = first { lc($_) eq $host } @ry;

    Why use Tie::File if you're not going to take advantage of it?

    use List::Util qw( first ); my $line = first { lc($_) eq $host } <$fh>;

    Why load up the entire file into memory if you look at it one line at a time?

    sub get_comments { my $host = lc(shift(@_)); my $file_name = "myfile.txt"; open(my $fh, '<', $file_name) or die("Unable to open server info file \"$file_name\": $!\n"); while (<$fh>) { return $1 if /^$host:\s*(.*)/; } return; }

    Of course, that's still very expensive if get_comments is called repeatedly. If the list is really that long, some sort of index (or database) would be very useful. A sorted file with fixed length records would also be fine (allowing for a binary search).

          Why load up the entire file into memory if you look at it one line at a time?

      Last I checked... Tie::File does not load the entire file into memory...


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

        That comment refered to the intermediary step towards the optimization that used <$fh> in list context.

        Tie::File does not load the entire file in memory (although it stores in memory the index of every line encountered in the file, unrelated and beyond any limit you have placed on its cache size).