Lori713 has asked for the wisdom of the Perl Monks concerning the following question:
#! c:\Perl\bin\perl5_8 #Read lines from a text file; create RE's that will find certain stuff #use strict; take this out for now open (MYFILE, "< Lori.txt") || die("Can't open Lori.txt"); #find lines starting with a c and end with a d print "lines starting with a c and end with a d:\n"; while (chomp($line =<MYFILE>)) { if ($line =~ /^[cC].*d$/o) { print "$line\n"; } } #find blank lines seek MYFILE, 0,0; print "\nblank lines:\n"; while (chomp($line =<MYFILE>)) { if ($line =~ /^$/o) { print "$line\n"; print "blank line above\n"; } } #find lines with only spaces seek MYFILE, 0,0; print "\nlines with only spaces:\n"; while (chomp($line =<MYFILE>)) { if ($line =~ /^ *$/o) { print "$line\n"; print "spaced out line above\n"; } } #find lines that contain the same number twice seek MYFILE, 0,0; print "\nlines that contain the same number twice:\n"; while (chomp($line =<MYFILE>)) { if ($line =~ /(\d+)[^\d]*\1[^\d]/o) { print "$line\n"; } } #find lines that contain a negative number with a decimal point seek MYFILE, 0,0; print "\nlines that contain a - number with a decimal point:\n"; while (chomp($line =<MYFILE>)) { if ($line =~ /-\d*\.\d*/o) { print "$line\n"; } } close MYFILE;
And my test text file Lori.txt:
Catdog isn't a horsebird
The next line is a blank line.
The next line is a line full of spaces.
fred1 fred2 fred3 fred3 fred2
lucy1 lucy2 lucy3 lucy4 lucy5
negative number with a decimal: -15.00
negative number with a decimal: -.15
positive number with a decimal: .15
negative number with a decimal: -7.
negative sign with a decimal: -.
Thanks for any insights you can provide. Please feel free to suggest ways of improving my code.    :-)
Lori
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex negative number question
by dragonchild (Archbishop) on Oct 06, 2003 at 14:45 UTC | |
by CombatSquirrel (Hermit) on Oct 06, 2003 at 15:05 UTC | |
by Abigail-II (Bishop) on Oct 06, 2003 at 22:14 UTC | |
by zby (Vicar) on Oct 06, 2003 at 15:12 UTC | |
|
Re: Regex negative number question
by delirium (Chaplain) on Oct 06, 2003 at 14:47 UTC | |
|
Re: Regex negative number question
by dragonchild (Archbishop) on Oct 06, 2003 at 15:32 UTC | |
|
Re: Regex negative number question
by fletcher_the_dog (Friar) on Oct 06, 2003 at 17:38 UTC | |
by Not_a_Number (Prior) on Oct 06, 2003 at 20:02 UTC | |
|
Re: Regex negative number question
by zby (Vicar) on Oct 06, 2003 at 15:18 UTC | |
|
Re: Regex negative number question
by Cody Pendant (Prior) on Oct 06, 2003 at 21:09 UTC | |
|
Re: Regex negative number question
by Lori713 (Pilgrim) on Oct 07, 2003 at 19:12 UTC |