read the config file and put the values into variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amma
    New Member
    • May 2007
    • 5

    read the config file and put the values into variables

    Hi Everyone,

    As I am having liitle bit perl knowledge,I was trying to read the config file contents and put the values into some variables.

    Config File Contents:
    Code:
    #Header:Putting Header on Excel Sheet
    Header_Text=Sample Report Sheet
    Header_Color=red
    Header_Merge_Info=A:D
    #Subheader1:Putting Subheader1
    SubHeader1_Text=Sample Subtitle1 Sheet
    SubHeader1_Color=red
    SubHeader1_Merge_Info=A:B
    #Subheader2:Putting Subheader2
    SubHeader1_Text=Sample Subtitle2 Sheet
    SubHeader1_Color=red
    SubHeader1_Merge_Info=C:D
    Needed Output:
    [CODE=perl]
    $p1=Sample Report Sheet
    $p2=red
    $p3=A
    $p4=D
    $p5=Sample Subtitle1 Sheet
    $p6=red
    $p7=A
    $p8=B
    $p9=Sample Subtitle2 Sheet
    $p10=red
    $p11=C
    $p12=D
    [/CODE]

    Pls do help me on this at the earliest.
    Last edited by miller; Jun 4 '07, 05:29 PM. Reason: Code Tag and ReFormatting
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Greetings Amma,

    What have you tried so far?

    - Miller

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Originally posted by miller
      Greetings Amma,

      What have you tried so far?

      - Miller
      Same reply by me on the perlguru forum.

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        Originally posted by KevinADC
        Same reply by me on the perlguru forum.
        Ahh ... yes. I learn quickly master. ;)

        - M

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Originally posted by miller
          Ahh ... yes. I learn quickly master. ;)

          - M

          It's time for you to leave grasshopper. ;)

          Comment

          • Amma
            New Member
            • May 2007
            • 5

            #6
            Dear Kevin,

            Thanks on this Reply !

            I have been developed the following code.

            [CODE=perl]
            #!usr/bin/env perl -w

            use strict;
            use warnings;

            open(FILE, "<Config.txt")o r die "Couln't open file1.txt: $!";

            while (<FILE>) {
            chomp;

            my @Fld = split /=/, $_;
            foreach my $token (@Fld) {
            print "\n$token";
            }
            }

            close(FILE);

            ##### End code #####
            exit;
            [/CODE]

            But the thing is ,it's not giving corrected form.

            I am looking to leave the lines start with # and once i found '=' i need to take the value of the same.
            pls help me on this !
            Last edited by miller; Jun 5 '07, 04:46 PM. Reason: Code Tag and ReFormatting

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              As posted on the other forum (based on the code you PM'd me):

              One possibility:


              [CODE=perl]#!usr/bin/env perl -w

              use strict;
              use warnings;
              my @p = ();
              open(FILE, "<Config.txt")o r die "Couln't open Config.txt: $!";
              while(<FILE>){
              next if (/^#/);
              chomp;
              my $var = (split(/=/))[1];
              if ($var =~ /^(.*?):(.*)/) {
              push @p,$1,$2;
              }
              else {
              push @p ,$var;
              }
              }
              close(FILE);
              print "$_\n" for @p;
              ##### End code #####
              exit;

              [/CODE]

              Comment

              • Amma
                New Member
                • May 2007
                • 5

                #8
                Dear Kevin,

                First of all,I would be thankful to you!.
                Could you pls send me standard tutorial url's so that i can start working on the same.

                Best Regards !
                Amma.




                Originally posted by KevinADC
                As posted on the other forum (based on the code you PM'd me):

                One possibility:


                [CODE=perl]#!usr/bin/env perl -w

                use strict;
                use warnings;
                my @p = ();
                open(FILE, "<Config.txt")o r die "Couln't open Config.txt: $!";
                while(<FILE>){
                next if (/^#/);
                chomp;
                my $var = (split(/=/))[1];
                if ($var =~ /^(.*?):(.*)/) {
                push @p,$1,$2;
                }
                else {
                push @p ,$var;
                }
                }
                close(FILE);
                print "$_\n" for @p;
                ##### End code #####
                exit;

                [/CODE]

                Comment

                Working...