Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I want to get the LOGIN and PASSWORD using Brandcode. I am reading a config file.
If i choose brand code CM, i should get the the following values.LOGIN = H006897 PASSWORD = $"?Y??
Each Brand code contains LOGIN and PASSWORD.
So how can i get the values according to my chosen Brand?.
Following details are stored in my config file.
[NEWBRAND] #LOGIN FOR THAT PARTICULAR BRAND #Get the LOgin and Password according to Brand [AN] LOGIN = H006698 PASSWORD = $"?Y?? [AZ] LOGIN = H006699 PASSWORD = ???S? [CL] LOGIN = H007985 PASSWORD = 8?|?E?0 [CM] LOGIN = H006897 PASSWORD = $"?Y?? [CU] LOGIN = H006701 PASSWORD = $"?Y?? [GM] LOGIN = H006703 PASSWORD = 8?|?E?0

Pls provide a solution
Regards, PS

Replies are listed 'Best First'.
Re: Read the values from a file
by Happy-the-monk (Canon) on Aug 23, 2004 at 15:25 UTC

    This looks suspiciously like an INI-style file.
    Config::Simple and a number of other modules for this purpose do the trick:

    use Config::Simple;
    my $cf = Config::Simple->new( syntax => "ini", filename => "your_password_file.cfg")
        or die qq(not an INI style file\n- $!);
    my $login    = $cf->param("cm.login");
    my $password = $cf->param("cm.password");

    Cheers, Sören

    Edit: added missing "my"s and boldfaced the params.

Re: Read the values from a file
by davorg (Chancellor) on Aug 23, 2004 at 15:33 UTC

    Obviously you should use an external module if you can. But having just spent the last few minutes writing the code, I'm going to post it here anyway :)

    #!/usr/bin/perl use strict; use warnings; my $brand = shift || die "Must give a brand\n"; my %data; while (<DATA>) { chomp; next unless /^\[$brand\]$/ .. !/\S/; next if /^\[/; next unless /\S/; my ($k, $v) = split /\s*=\s*/, $_, 2; $data{$k} = $v; } foreach (keys %data) { print "$_ => $data{$_}\n"; } __END__ [NEWBRAND] #LOGIN FOR THAT PARTICULAR BRAND #Get the LOgin and Password according to Brand [AN] LOGIN = H006698 PASSWORD = $"?Y?? [AZ] LOGIN = H006699 PASSWORD = ???S? [CL] LOGIN = H007985 PASSWORD = 8?|?E?0 [CM] LOGIN = H006897 PASSWORD = $"?Y?? [CU] LOGIN = H006701 PASSWORD = $"?Y?? [GM] LOGIN = H006703 PASSWORD = 8?|?E?0
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Hi,
      Thanks a lot for all your help.
      I am a new comer to this area.
      I tried to write a program without any module.
      In future i would like to edit the file according the brand.
      I tried to write a code as follow...
      $inputfiletoread = "/config/apollo_logins.cfg"; &Read_Config_File($inputfiletoread); sub Read_Config_File { my($inputfiletoread, ) = @_; chomp($inputfiletoread); if ( -e "$inputfiletoread") { open ( ReadFile , $inputfiletoread); flock( ReadFile, LOCK_EX); @readlines = <ReadFile>; foreach $readentirefile(@readlines) { #If Line Begins Pound Symbol(#) Skip To The Next Line; next if $readentirefile =~ s/^#//; #Check For Line Beginning And Ends With Square Brackets; if ($readentirefile =~ m/^\[/) { if ($readentirefile =~ m/\]+$/) { #Storing the string within [ ] in to string storegroupname; $storegroupname = $readentirefile; $storegroupname =~ s/^\[//; $storegroupname =~ s/\]+$//; } } #If Line is Blank Skip To Next Line; next if $readentirefile =~ s/^$//; #Splitting The Lines INTO Fields using Delimiter; next if $readentirefile =~ m/^\[/; my($optionname, $optionvalue) = split(/=/, $readentirefile); #print "HERE Key=$optionname Val=$optionvalue\n"; } flock (ReadFile, LOCK_UN); close ReadFile; } else { #Error: To Be Written To Log Manager #print " The Path - Name Doesnt Exists \n"; exit(); } }

      Thanks a lot... i will follow your suggestion and hope i can ask doubt regarding this..
      Regards,
      Suresh
      Hi,
      Thanks for your code.
      I am planing to use Config::Simple module to solve my solution.
      Your code working fine.
      Can you please explain the following code (3 Lines)
      next unless /^\[$brand\]$/ .. !/\S/; next if /^\[/; next unless /\S/;
      Thanks & Regards,
        Can you please explain the following code (3 Lines)
        next unless /^\[$brand\]$/ .. !/\S/;

        This uses the "flip-flop" operator (i.e. '..' in a scalar context) to only process the lines that we are interested in. The flip-flop operator returns false until its left operand is true. It then continues to return true until its right hand operator is true. At that point it returns false and continues to do so until its left hand operand is true again. And so on.

        So here we us "next" to skip to the next loop iteration unless we are between the [XXX] line that starts our required section and the blank line that ends it.

        next if /^\[/;

        We don't actually need to process the [XXX], so this code skips to the next loop iteration.

        next unless /\S/;

        And similarly for the blank line that ends the section.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Read the values from a file
by gellyfish (Monsignor) on Aug 23, 2004 at 15:22 UTC

    THat looks sufficiently like a windows .ini style file that you should be able to use the module Config::IniFiles - give it a go.

    /J\

Re: Read the values from a file
by eric256 (Parson) on Aug 23, 2004 at 15:24 UTC

    Do you have any code you have tried?


    ___________
    Eric Hodges