in reply to Read the values from a file

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

Replies are listed 'Best First'.
Re^2: Read the values from a file
by Anonymous Monk on Aug 23, 2004 at 17:12 UTC
    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
Re^2: Read the values from a file
by Anonymous Monk on Aug 27, 2004 at 02:54 UTC
    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