Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 1999-01-18
12
Medium Priority
?
149 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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 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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 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