473,383 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Hi guys! Got stuck with this..

"2nd post as i noone could do it"

Hi guys! Got stuck with this..
I have a chunks of text separated with blanc line. All I need is to
take every last 3 lines of that text before the blanc line and put it
in to the new text file: example:

1some text
2some text
3some text
4some text
5some text
6some text
7some text
8some text
9some text
10some text
11some text
12some text
13some text
14some text
15some text
End result mast be...
3some text
4some text
5some text
8some text
9some text
10some text
13some text
14some text
15some text
Any idea?

Sep 5 '06 #1
2 1158
kulet wrote:
Hi guys! Got stuck with this..
I have a chunks of text separated with blanc line. All I need is to
take every last 3 lines of that text before the blanc line and put it
in to the new text file: example:

You could keep a rolling buffer of the previous 3 lines as variables
(or an array, which I would personally use). Each time you get a new
line, move each previous entry down the buffer getting rid of the
oldest. Then, when you encounter a blank line, dump your buffer to the
file.

Something like (untested):
$currLine = //get line data somehow
if (strlen($currLine)==0){
//output array contents to file
unset($buffer);
} else {
$buffer[] = $currLine;
if (count($buffer)>3){
array_shift($buffer);
}
}

Sep 5 '06 #2
Messy but effective:-

Read the whole of the text into a variable.

Reverse the string. I dont know if there is a function to do this or
whether you would have to write one.

copy the first three lines after each paragraph to a new variable.

Reverse this string to get it back into the correct direction.

OR:

more effective.

Copy the whole text to a variable
Split it at CRLF into an array.
The blank lines should create an 'empty' array element
For each array element, check the next three elements and if any of
them are empty (not sure if it will be null or just length zero) then
add it to a variable.

return the variable.

easy.

Aaron
mo*******************@yahoo.com wrote:
kulet wrote:
Hi guys! Got stuck with this..
I have a chunks of text separated with blanc line. All I need is to
take every last 3 lines of that text before the blanc line and put it
in to the new text file: example:


You could keep a rolling buffer of the previous 3 lines as variables
(or an array, which I would personally use). Each time you get a new
line, move each previous entry down the buffer getting rid of the
oldest. Then, when you encounter a blank line, dump your buffer to the
file.

Something like (untested):
$currLine = //get line data somehow
if (strlen($currLine)==0){
//output array contents to file
unset($buffer);
} else {
$buffer[] = $currLine;
if (count($buffer)>3){
array_shift($buffer);
}
}
Sep 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: echo01 | last post by:
Hey when do you guys think 5.0 will be stable or considered stable. i think the are up to 5.0.2?
7
by: Snake | last post by:
Hi guys, I have question about classes. when u create class called Test. and you define variable Test c; so does this act like( a variable c of type Test pointing to an abject )? The thing that...
10
by: Tom Dauria | last post by:
My situation is that I am developing and supporting databases for my company. I have been using paper folders one for each project both new and support. And support means I am still developing...
92
by: Jeffrey P via AccessMonster.com | last post by:
Our IT guys are on a vendetta against MS Access (and Lotus Notes but they've won that fight). What I can't understand is, what's the problem? Why does IT hate MS Access so much. I have tried...
1
by: skygenn | last post by:
hi guys im new at c++ program and im 1st year taking up I.T our prof in c++ gave us an assignments but she didnt even explain us how to begin doing it? honestly i have no idea how to begin it...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.