my file contains multiple ipconfig outputs.

Ah, that changes things a bit. Is there a distinct and consistent string pattern that separates one ipconfig output from another, such as a blank line, or a line of dashes?

If so, you can assign that string to "$/" (rather than leaving it with the default value of CRLF, or setting it to undef, as suggested by BrowserUK). This way, you read one whole ipconfig output set in a single iteration of while (<>). For example, if the successive records are separated by a single blank line (and blank lines never occur within a single ipconfig output record), then you could do like this (untested):

my %hostdata; { $/ = "\x0d\x0a\x0d\x0a"; # record separator is now a blank line while (<>) { # $_ now contains one whole ipconfig output next unless ( /Host Name.*?:\s*(.*)/ ); my $host = $1; @{$hostdata{$host}{dns}} = ( /\s(\d+\.\d+\.\d+\.\d+)\s/g ); ( $hostdata{$host}{typ} ) = ( /Node Type.*?:\s*(.*)/ ); } } # Now, keys %hostdata gives the list of host names from # all the ipconfig runs, and $hostdata{"hostname"} contains # the dns and node type info for each host. for ( sort keys %hostdata ) { print join "\n", $_, @{$hostdata{$_}{dns}}, $hostdata{$_}{typ}, "\n +"; }
(update: removed a spurious line of code)

If the separation between ipconfig runs is not distinct or consistent, then you'll want to fall back on BrowserUK's less-favored method that reads one line at a time and uses a status flag to keep track of what it's supposed to look for (and what it's supposed to do) as it works through each multi-line record.


In reply to Re: Re: Re: Re: Re: reading lines by graff
in thread reading lines by spill

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.