Forum Moderators: coopster

Message Too Old, No Replies

How to number 42 lines 1-14

         

shanecavanaugh

11:58 pm on Sep 3, 2005 (gmt 0)

10+ Year Member



I have 3 sonnets (14-line poems, fyi) in a file without blank lines and I want to tack line numbers onto the end of each line so that the first 14 lines will be numbered 1-14, as will the second 14 lines and the third. I've exploded the file by "\n" so that I have each line as an array value. I've been using various loop fuinctions (for, foreach, while) to try to put individual numbers at the end of each corresponding line, but something either loops too much or not at all.

This is just an example. I know I could number 42 lines by hand, but I'd like an automated system to do it for much more line numbers.

grandpa

1:44 am on Sep 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you want consecutive numbers, then just create a working variable. Let's call it $x. Initialize $x to 1.

Then in a foreach or while loop, increment $x as needed.

$x = 1;
foreach ($array as $key=>$val) {
echo "<br>$val $x";
$x ++;
}

If you need $x to start at #1 for the second sonnet, then you can substitute $x ++; with this:

if ($x == '14') $x = 1;
else $x ++;

A dreary day for love and lovers - 1
this cold and wet Valentines Day; - 2
Sitting alone, glancing at each other, - 3
both hoping the other will not go away. -4
The banners and signs all bright and pink, - 5
they're supposed to make us feel better; -6
As I sit here alone, I wonder, I think, -7
just how much of this really matters. - 8
When I looked up, looked over at you, - 9
you looked quickly the other way; - 10
To all the lovers, there must be so few, - 11
I wish a Happy Valentine's Day. - 12

Sorry, temptation won over

figment88

2:40 am on Sep 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



also, you don't need to explode by "\n" to get each line in array. The file() command does this automatically.

[us3.php.net...]

bennymack

2:53 am on Sep 4, 2005 (gmt 0)

10+ Year Member



That PHP stuff looks like a lot of work..

perl -Minteger -ne 'chomp;$b=($a++%14)+1;print "$_ - $b\n";' test.txt

dmorison

7:35 am on Sep 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That Perl stuff looks like a lot of work..

php -R 'echo($argn." - ".($i++%14+1)."\n");' < test.txt

Your move ;)



Shane -

The above PHP command line hack is PHP5. However, even within the code based examples above the modulus operator (%) is something you can use whenever you want to generate a repetative count from x to y.

Modulus means "the remainder following division", so by incrementing a counter "MOD 14" you get values in the range 0-13 (which is why you see the +1 in the above hacks). Your problem could be solved something like this (assuming $infile as the source text file opened for reading, and $outfile as your output, opened for writing)...


while(!feof($infile))
{
$line = fgets($infile,1024);
fputs($outfile,$line." - ".($i++%14+1)."\n");
}

bedlam

8:12 am on Sep 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're outputting to html...

  1. Why not
  2. Use
  3. An ordered List?

echo '<ol>';
foreach ($array as $key=>$val) { echo "<li>$val</li>"; }
echo '</ol>';

-B

bennymack

1:25 pm on Sep 4, 2005 (gmt 0)

10+ Year Member



perl -i.bak -ne 'chomp;printf "$_ - %s\n", $a++%14+1;' test.txt

Will edit the file test.txt in place AND create a backup called test.txt.bak. Ooooh. Ok I'm done.

shanecavanaugh

6:57 pm on Sep 4, 2005 (gmt 0)

10+ Year Member



Thanks, everyone! It's working now, and I think I might have learned a bit more about PHP's syntax.