in reply to Re^3: Extracting password from array
in thread Extracting password from array

Shell solution would be PASSWD=`cat password_file | grep $database | grep $user | cut -d: -f2`

Replies are listed 'Best First'.
Re^5: Extracting password from array
by ikegami (Patriarch) on Mar 29, 2010 at 20:38 UTC

    And you're complaining about the code I gave? I can't wait for the day you create a user name, password, database name or server name that's a subset of a user name, password, database name or server name (which doesn't seem unlikely).

    The direct equivalent of:

    database=... user=... PASSWD=`cat password_file | grep $database | grep $user | cut -d: -f2`
    is
    my $database = '...'; my $user = '...'; my $PASSWD = ''; open(my $fh, '<', 'password_file') or die("open: $!\n"); while (<$fh>) { chomp; $PASSWD = (split /:/)[2] if /$database/ && /$user/; }

    Not bad. Replacing 4 entire programs with 5 statements. (Although cat is totally useless in your code.)

    If you want an equivalent drop-in replacement, you can use

    PASSWD=`perl -e"chomp; print((split /:/)[2]) if /$database/ && /$user/ +" < password_file`

    If you want a better drop-in replacement, you can use

    PASSWD=`perl -F: -lane'print $F[1] if $F[2] eq $ARGV[0] && $F[0] eq $A +RGV[1]' $database $user < password_file`