I believe you meant to send this message to the Perl Beginner's List.

On Monday, October 6, 2003, at 01:51 AM, vaishali wrote:

I am having my text file called ldap1.txt in the following format

uid                     : department                         : branch
vaishali.chitale   : Corporate Entertainment   : Mumbai
rajesh.chandran  : Planet M - Powai (Retail) : pune


I have written a code so that I can take department and branch according to uid in
my program but this code is not working .

Let's see if we can fix it up.


# open ldap1 text file and chunk the elements into a hash

open(F,'<'.$file_name) or die "Can't open $file_name: $!\n";

Good start.


my @file=<F>;

Why store the whole file in an array so you can later walk it line by line? Let's just do all our processing right here, instead of the line above. Here's how I would do it:


my %lp1;
while (<F>) {
        my($uid, $department, $branch) = split /\s*:\s*/, $_;
        $lp1{$uid} = { department => $department, branch => $branch };
}

close(F);

Nothing below this line is needed, but I'll go through it with comments anyway


my %lp1;
for my $file (@file) {
my @lp1=(split(/\s*:\s*/,$file))[0];

This is the program's main problem. You're splitting the fields and then throwing away all but the first one with your indexed list assignment. That's why you're seeing uninitialized warnings, $lp1[1] and $lp1[2] never receive a value. You could fix this by removing the ( ... )[0] around your split.


Hope that helps.

James

$lp1{$lp1[0]}{'department'}=$lp1[1];
$lp1{$lp1[0]}{'branch'}=$lp1[2];

}
undef @file;


In above code I am not able to print department and branch
it is giving me error for the following

$lp1{$lp1[0]}{'department'}=$lp1[1];
$lp1{$lp1[0]}{'branch'}=$lp1[2];

Use of uninitialized value

what is the problem ?







--- NOTICE----------------------------------------------------------------- ---------------------------------
This E-mail (including the attachment/(s) if any ) has been scanned for viruses and dangerous content. For more information mail to [EMAIL PROTECTED]
----------------------------------------------------------------------- ------------------------------------
--- DISCLAIMER------------------------------------------------------------- ---------------------------------
The contents of this E-mail (including the contents of the enclosure/(s) or attachment/(s) if any) are privileged and confidential material of Bennett, Coleman & Co. Ltd. (BCCL)and should not be disclosed to, used by or copied in any manner by anyone other than the intended addressee/(s). If this E-mail (including the enclosure/(s) or attachment/(s) if any ) has been received in error, please advise the sender immediately and delete it from your system. The views expressed in this E-mail message (including the enclosure/(s) or attachment/(s) if any) are those of the individual sender.
----------------------------------------------------------------------- ------------------------------------




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to