[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

simple code

angel.of.north@googlemail.com

10/19/2007 1:45:00 PM

hi

#!/usr/bin/ruby -w
re=Regexp.new('\s+[A-Z]')
puts " {| class=\"wikitable\"
|-
Table -- put your title here
|-"
while line = gets
md=re.match(line)
if md =~ nil
else
puts md
end
end

I input the text as


COM - Commercial
EDU - Educational
GOV - Povernment
NET - Network
MIL - Military
ORG - Non-profit organisations



and I get the result as

{| class="wikitable"
|-
Table -- put your title here
|-
nil
C
E
G
N
M
O
nil


How do I prevent the nil appearing?
How do I access each matchdata with the postmatchdata on each line?

DavidR

4 Answers

Ben Giddings

10/19/2007 1:51:00 PM

0

On Oct 19, 2007, at 09:45, angel.of.north@googlemail.com wrote:
> while line = gets
> md=re.match(line)
> if md =~ nil

Try "if md.nil?" instead.

> else
> puts md

puts md, md.post_match

> end
> end


7stud --

10/19/2007 6:14:00 PM

0

Ben Giddings wrote:
> On Oct 19, 2007, at 09:45, angel.of.north@googlemail.com wrote:
>> while line = gets
>> md=re.match(line)
>> if md =~ nil
>
> Try "if md.nil?" instead.
>

if md
#do stuff


>> else
>> puts md
>
> puts md, md.post_match
>

puts line




Try this:

re=Regexp.new('^\s+[A-Z]') #match start of line only
...
...
while line = gets
md=re.match(line)

if md
puts line
end

end



--
Posted via http://www.ruby-....

William James

10/20/2007 1:19:00 AM

0

On Oct 19, 8:44 am, "angel.of.no...@googlemail.com"
<angel.of.no...@googlemail.com> wrote:
> hi
>
> #!/usr/bin/ruby -w
> re=Regexp.new('\s+[A-Z]')
> puts " {| class=\"wikitable\"
> |-
> Table -- put your title here
> |-"
> while line = gets
> md=re.match(line)
> if md =~ nil
> else
> puts md
> end
> end
>
> I input the text as
>
> COM - Commercial
> EDU - Educational
> GOV - Povernment
> NET - Network
> MIL - Military
> ORG - Non-profit organisations
>
> and I get the result as
>
> {| class="wikitable"
> |-
> Table -- put your title here
> |-
> nil
> C
> E
> G
> N
> M
> O
> nil
>
> How do I prevent the nil appearing?
> How do I access each matchdata with the postmatchdata on each line?
>
> DavidR

re = /\s+[A-Z]/

puts ' {| class="wikitable"
|-
Table -- put your title here
|-'

while line = gets
puts line if line =~ re
end

William James

10/20/2007 1:29:00 AM

0

On Oct 19, 8:44 am, "angel.of.no...@googlemail.com"
<angel.of.no...@googlemail.com> wrote:
> hi
>
> #!/usr/bin/ruby -w
> re=Regexp.new('\s+[A-Z]')
> puts " {| class=\"wikitable\"
> |-
> Table -- put your title here
> |-"
> while line = gets
> md=re.match(line)
> if md =~ nil
> else
> puts md
> end
> end
>
> I input the text as
>
> COM - Commercial
> EDU - Educational
> GOV - Povernment
> NET - Network
> MIL - Military
> ORG - Non-profit organisations
>
> and I get the result as
>
> {| class="wikitable"
> |-
> Table -- put your title here
> |-
> nil
> C
> E
> G
> N
> M
> O
> nil
>
> How do I prevent the nil appearing?
> How do I access each matchdata with the postmatchdata on each line?
>
> DavidR

#!awk -f

BEGIN {
print " {| class=\"wikitable\"|-Table -- put your title here|-" }

/[ \t]+[A-Z]/