Hi, well if you did a google search on "perl open", you would come to http://perldoc.perl.org/functions/open.html as the first link.

You need something like this:

open OUT , '>', "outfilename" or die "unable to open out $!";
Then instead of just  print "whatever" you just put in print OUT "whatever" . The print goes to the OUT file instead of to the terminal.

The kind of problem that you are having where the file contains something weird that you find during debug happens all the time when using ad hoc methods to parse something for which a complete spec is not known in advance.

I am happy to see that you are making an effort to understand the code. My efforts to help are pointless unless actual knowledge is being transferred.

The generic problem is that you can't output the new lines until you are sure that you've got all of the referencedesignators. The typical solution is to delay the printout until you have seen the next line with a valid $netname. This introduces a couple of complications.

First, how do we tell if this is a new "record" or a continuation of the previous line? That depends upon what $netnames look like. If all netnames have an underscore in them and the other continuation tokens do not, then something like this would work:

my $test = '10GBE_ADDR1'; #decide if first part of line has ABC901X_ ... print "new record\n" if $test =~ m/^[A-Z0-9a-z]+_/;
The above code decides that "10GBE_" is a match. If that regex (regular expression) is not adequate, then some other "rule" is needed.

Second, since we don't have a simple: read line, process line, print line(s), some "memory" is needed. ie. we have "read line, print lines based upon previous $netname and previous @singlereference if a new record is starting, process line".

Third, you will find that the last record is problematic. The while loop will end when there are no more lines, but that last record will not have been output yet. So you need some "cleanup" code to do that.

sub print_record { return unless @singlereference; #no work to do @singlereference = sort {$a <=> $b} @singlereference; foreach my $col (@singlereference) { print "$netname $col\n"; } @singlereference =(); #reset array to empty return; }
Now you can call this subroutine inside or after the while loop ends provided that you give @singlereference and $netname greater scope by declaring them before the while loop starts.

Hope these tips helps. Try some code and ask if you are having problems. The code doesn't need to some "masterpiece", it just has to be logically correct and work.


In reply to Re^5: Perl Formatting Text by Marshall
in thread Perl Formatting Text by oopl1999

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.