Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read STDIN chunk until string match

Status
Not open for further replies.

thedaver

IS-IT--Management
Jul 12, 2001
2,741
US
I am reading text via STDIN within a &quot;while (<>) { ... }&quot; loop.

I need to read and store a variable number of strings within a &quot;section&quot; of incoming content until I reach a specific string in STDIN or a blank line...

--- BEGIN Example Data:

JOBS:
Butcher
Baker
Candlestick Maker

LOCATIONS:
Houston
Toledo
Moscow
Detroit
Vienna

--- END Example Data:

I feel stupid, but I cannot figure out how to read multiple &quot;sections&quot; of variable numbers of strings into arrays. I'm guessing there's a really slick perl module to do this, but I'm at a loss. Help Appreciated in advance.



&quot;Surfinbox Shares&quot; - A fundraising program that builds revenue from dialup Internet users.
 
You could do something like this:

while (<stdin>) {
@array = $_ unless $_ = &quot;Your criteria for termination&quot;;
if (&quot;Your criteria for termination&quot;) {
exit;
}
}
 
ok, great start, but my (mental) problem is trying to keep parsing the STDIN for multiple &quot;sections&quot; of data. Just one section is easy enough, the issue is when I need to parse 500 lines of STDIN to find the sections

&quot;Surfinbox Shares&quot; - A fundraising program that builds revenue from dialup Internet users.
 
I guess I am not quite clear on what you want to do. You want to terminate at a found string and then skip to the next section?

Like this:?

JOBS:
Digger <- print
Farmer <- print
Baker <- skip
Butcher <- skip

LOCATIONS:
Toledo <- print
Moscow <- print
Ohio <- print
Florida <- skip

Is that the gist?

If so, you can do something like this:

$pr = 0;
while (<stdin>) {
if (/something that identifies a new section/) {
$pr = 1;
}
elsif (&quot;Your criteria for termination&quot;) {
$pr = 0;
}
@array = $_ if ($pr);
}



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top