Solved

Read blocks of text from a file?????

Posted on 1999-01-18
12
145 Views
Last Modified: 2013-12-25
HI....
I have a perl.cgi that writes form data to a disk file in the folowing format.....

NAME=Fred%2Bloggs
ADDRESS=6223%02Ocean%02Avenue
MORE_VARS=HERE
AGE=33
DATE=12%02December%02
MAYBE=Another%02var%02here%02
=
Lots of text appears as a single
block here until there is a = sign on its own line.
=
The number of such text boxes that appear here
varies. A block will be appended to this file periodically.
=
And will finish with an = sign on its own.
The number of text block will grow
=

I can read in the single lines OK. but I'm stuck when it comes to reading in the blocks of text towards the end of the e block.

To get the single lines I'm doing.....
($var1, $var2) =  split (/=/, $_);

To the question......

How can I read each block into its own variable so that I can use each one in its own table cell?

Does anyone have any ideas????

regards

0
Comment
Question by:rickyr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 1

Expert Comment

by:slinky
ID: 1832314
Have a start token (=) and and end token(=).
Detect the start token, the concatenate each line into your variable until the end token.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1832315
while( <FILE> ){
  ($var1, $var2) = split/=/;
  last unless $var1;
}
{local $/="\n=\n"; @textblocks=<FILE>; }
0
 
LVL 1

Expert Comment

by:ptruman
ID: 1832316
open(MAIN,"filename);
@lines=<MAIN>;
$stringident = "=\n";
foreach $line (@lines) {
     if ($line !~ /$stringident/) {
         # your code
     } else {
         # found a block marker
     }
}
0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
LVL 3

Author Comment

by:rickyr
ID: 1832317
ptruman.....
Thats ok for working out which lines have the "=" and which lines do not.
What is the best way to create the arrays from the results of your "if" construct,
#your code
} else {
#found a block marker
are the lines that are missing from your solution.
I need to be able to pull the values from each of these blocks by referencing the variables
created inside your if. forinstance.......

 $array_of_blocks[2]

should hold.....
And will finish with an = sign on its own.
    The number of text block will grow

Incidently... don't forget about the single lines which also have to be treated in a similar way, except that I need to pull these values out with something like......
$vitals{"NAME"}.

No I know I am mixing arrays with hashes herem but the solution is up to you guys, you could suggest different ways of formating the input text files if you think theres a better way.
regards
0
 
LVL 1

Expert Comment

by:ptruman
ID: 1832318
If the line is *in* a block...

($variable,$value) = split(/[=]/,$line);

then do what you want with $variable & $value


0
 
LVL 3

Author Comment

by:rickyr
ID: 1832319
Ok.....
So in your solution what variable holds the values for the 3rd block of text that contains these 2 lines.....

And will finish with an = sign on its own.
          The number of text block will grow.

0
 
LVL 1

Expert Comment

by:ptruman
ID: 1832320
It is done line by line.
If you want to store them in blocks you need to keep
track of how many = blocks you've encountered :

$BlockCount++;
after each block,
and store the values in an array :

($variable,$value) = split(/[=]/,$line);
$OUTPUT[$variable] = $value;

Then $OUTPUT[relevant block] holds the value...

0
 
LVL 84

Expert Comment

by:ozo
ID: 1832321
In my solution, that would be contained in $textblocks[2]
0
 
LVL 3

Author Comment

by:rickyr
ID: 1832322
ptruman, sorry to reject again, but ozo's solution is exactly what I want.
regards
0
 
LVL 3

Author Comment

by:rickyr
ID: 1832323
ozo...
Did you forget about this one?
0
 
LVL 84

Accepted Solution

by:
ozo earned 110 total points
ID: 1832324
Yes. Sorry.
0
 
LVL 3

Author Comment

by:rickyr
ID: 1832325
Thanx again.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question