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`

In reply to Re^5: Extracting password from array by ikegami
in thread Extracting password from array by robotball

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.