Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1999-01-18
12
Medium Priority
?
152 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 85

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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 85

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 85

Accepted Solution

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

Author Comment

by:rickyr
ID: 1832325
Thanx again.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

963 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