G'day bshah,

"Open user.txt and iterate through each user and check each line on ldap.txt in dn: line."

That would mean you'd be reading your multi-gigabyte ldap.txt multiple times (i.e. once for every user in user.txt). Instead, read user.txt once and create a regex with a capture group containing an alternation of all the users. If that's unfamiliar to you, see "Perl regular expressions tutorial".

"If matches, then store the value of all the lines matching maillocaladdress to the varialbe , I assume in hash key/value pari but here the values are more than one."

What you want here is a "hash of arrays".

Putting all that together (based on your sample data):

#!/usr/bin/env perl use strict; use warnings; use autodie; my ($user_file, $ldap_file) = qw{pm_1079590_user.txt pm_1079590_ldap.t +xt}; open my $user_fh, '<', $user_file; my @users; while (<$user_fh>) { chomp; push @users, $_; } close $user_fh; my $user_re = 'uid=(' . join('|', @users) . '),'; my %user_ldap_data; open my $ldap_fh, '<', $ldap_file; my $user = ''; while (<$ldap_fh>) { if (/^dn:/) { $user = /$user_re/ ? $1 : ''; next; } next unless $user; push @{$user_ldap_data{$user}}, $1 if /^maillocaladdress:\s+(\S+)/ +; } close $ldap_fh; use Data::Dump; dd \%user_ldap_data;

Output:

{ game => [ "game\@example.com", "game.test\@example.com", "game-test\@example.com", ], test1 => [ "test1\@example.com", "test.team\@example.com", "test11\@example.com", ], }

-- Ken


In reply to Re: File Iteration and looking for specific matching data by kcott
in thread File Iteration and looking for specific matching data by bshah

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.