473,473 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Printing only two lines from a file

Hi all!

I have a file (file01, i.e) like this:

foo
foo
foo
bar
foo
bar
foo

....

Where foo and bar repeats n times, but only foo and bar. What i want to
print is just:
foo
bar
I have tried this:

$myfile=file("file01");

$current_line = reset($myfile);
$first_line = $current_line;

print("First line is: $first_line");

while ($current_line=next($myfile))
{
if ($current_line != $first_line)
{
print("The other line is: $current_line");
break;
}
}
and it works as stand alone, but in the real application, i get:

Fatal error: Cannot break/continue 1 level in
/var/www/html/web/include/functionShow.inc on line 172
Does anyone knows how to do it in another way?

Thanks a lot.

Jul 17 '05 #1
2 2184
Fernando Alvarez-Uria wrote:
Hi all!

I have a file (file01, i.e) like this:

foo
foo
foo
bar
...

Where foo and bar repeats n times, but only foo and bar. What i want to
print is just:

foo
bar .... Does anyone knows how to do it in another way?


Try this:

<?php
unset($x);
$fh = fopen('file01', 'r');
while (!feof($fh)) { // read the file line-by-line
$line = trim(fgets($fh));
if ($line) $x[$line]++; // and increment a count for each line
}
fclose($fh);
echo "lines in file (hopefully by order of appearance):\n------------\n";
foreach ($x as $line=>$count) {
echo "[$line] shows up $count times\n"; // display the line and count
// echo "$line\n"; // display just the line
}
echo "------------\n";
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
"Fernando Alvarez-Uria" a écrit le 12/12/2003 :
Hi all! Hi
I have a file (file01, i.e) like this:
foo
foo
foo
bar
foo
bar
foo
...

Where foo and bar repeats n times, but only foo and bar. What i want to print

I have tried this: [...] Does anyone knows how to do it in another way?


Try to use array_unique on your $myfile var :
$resultarray = array_unique(file("file01"));
Then parse $resultarray :
foreach ($resultarray AS $key => $val) {
echo "Distinct line number $key = $val<br>\n";
}

See the doc :
http://fr.php.net/manual/en/function.array-unique.php

--
Have you read the manual?
http://www.php.net/manual/en/

Jul 17 '05 #3

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

Similar topics

10
by: Andrew | last post by:
I am making a web site and I am using ASP because of its templating capabilities. My footer is an include file, for obvious reasons. I can't use an include file for the header, because, while the...
5
by: Patrick De Ridder | last post by:
How can I turn what I want to print 90 degrees using the logic below? Please tell me the code with which to make the modification. Many thanks, Patrick. using System.ComponentModel; using...
10
by: Jeff B. | last post by:
Has anyone come across a decent algorithm for implementing word wrap features in .net printing? I have a small component that uses basic printing techniques (i.e. e.Graphics.DrawString in a...
4
by: Rob T | last post by:
I have a small VB program that has a printing module...very simple....and works great. However, If I try to print to a generic printer, I get the following error: "The data area passed to a...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
12
by: Alex Clark | last post by:
Greetings, (.NET 2.0, WinXP Pro/Server 2003, IE6 with latest service packs). I've decided to take advantage of the layout characteristics of HTML documents to simplify my printing tasks, but...
8
by: eric | last post by:
I have a text file that I would like to print. I load the contents of this text file into a string, call it strData. I then pass strData into my print method, which creates it as a StreamReader. ...
3
by: cj | last post by:
I always have liked to print my code and look at it on paper. Used to be I could print my code on nice tractor feed green bar paper. Now I have to tape pages together. Oh well. What really...
0
by: Iain Wilson | last post by:
I am pulling my hair out trying to print various objects from a .net web page My apologies for cross posting but I need an answer and my previous post has attracted no interest. ASP.Net 2.0...
7
by: Iain Wilson | last post by:
I am pulling my hair out trying to print various objects from a .net web page My apologies for cross posting but I need an answer and my previous post has attracted no interest. ASP.Net 2.0...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.