Solved

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

Posted on 1999-01-18
12
136 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
Comment Utility
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
Comment Utility
while( <FILE> ){
  ($var1, $var2) = split/=/;
  last unless $var1;
}
{local $/="\n=\n"; @textblocks=<FILE>; }
0
 
LVL 1

Expert Comment

by:ptruman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Expert Comment

by:ptruman
Comment Utility
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
Comment Utility
In my solution, that would be contained in $textblocks[2]
0
 
LVL 3

Author Comment

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

Author Comment

by:rickyr
Comment Utility
ozo...
Did you forget about this one?
0
 
LVL 84

Accepted Solution

by:
ozo earned 110 total points
Comment Utility
Yes. Sorry.
0
 
LVL 3

Author Comment

by:rickyr
Comment Utility
Thanx again.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

771 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

14 Experts available now in Live!

Get 1:1 Help Now