Hi, I am trying to avoid ugly code. I have a working solution below and what I wish I could make my doce look like commented out. I am including a sample input file and the expected output. For the application I am writing there will be up to thousands if these types of flags and I am looking far a better way to parse the data. Any help is much appreciated.
INPUT FILE = # comment # comment ON Mon, Tues, Fri, Sat # comment other stuff EXPECTED OUTPUT = $on = Mon,Tues,Fri,Sat CODE = #!/usr/bin/perl -w $infile = "test.txt"; $linecounter = 0; $inon = 0; $hold_on = ""; open(INFILE, $infile) || die "Can't open $infile : $!\n"; MAIN:while(<INFILE>) $linecounter++; #printf("File Line %s = %s",$linecounter, $_); if ( /^\*/ || /^\#/ ) # Get rid of useless comments in file. { next MAIN; } chop; if ( /^on/i || $inon eq "1" ) { undef($tmpon); undef(@junk); if ( $inon eq "0" ) { @junk = split(' ', $_, 2); $tmpon = $junk[1]; $tmpon =~ s/\s//g; } else { $tmpon = $_; } $len = length($tmpon) - 1; $last_char = substr($tmpon, $len, 1); #printf("Last Char = %s\n", $last_char); if ( $last_char eq "," ) { $hold_on = "$hold_on$tmpon"; $inon = 1; next MAIN; } else { $on = "$hold_on$tmpon"; $inon = 0; $hold_on = ""; next MAIN; } } #other code printf("\$on = %s\n", $on); WISHFUL CODE = #$infile = "test.txt"; #$linecounter = 0; #open(INFILE, $infile) || die "Can't open $infile : $!\n"; #MAIN:while(<INFILE>) # $linecounter++; # printf("File Line %s = %s",$linecounter, $_); # if ( /^\*/ || /^\#/ ) # Get rid of useless comments in file. # { # next MAIN; # } # chop; # if ( /^on/i ) # { # hold_on = ""; # undef(@junk); # @junk = split(' ', $_, 2); # $hold_on = $junk[1]; # $len = length($hold_on) - 1; # $last_char = substr($hold_on, $len, 1); # while ( $last_char eq "," ) # { # $read_line = readln(<INFILE>); # $read_line =~ chop($read_line); # $hold_on = "$hold_on$read_line"; # $len = length($hold_on) - 1; # $last_char = substr($hold_on, $len, 1); # } # $hold_on =~ s/\s//g; # } # #other code #printf("\$on = %s\n", $on);

In reply to How do you read the next line in a file while in a loop? by GroundZero

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.