Solved

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

Posted on 1999-01-18
12
139 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
  • 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

932 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now