Need help about Ruby with text file

Hi, everybody

This is my first time get into this forum.
I would like to know that is it a forum for us to discuss about
everythings in Ruby, please?
If so,
1)I would like to know that how can I delete or remove a line in a text
file, please?
2)How can I read some letter in a line, please?
e.g. a text file like this:-

aabbcc
ddeeff
gghhii
jjkkll

1)I want to delete ddeeff become like this:-
aabbcc
gghhii
jjkkll

2)I get the result like :-
hh

Thanks
Regards
Oscar

···

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

Oscar Lok wrote:

···

Hi, everybody

This is my first time get into this forum.
I would like to know that is it a forum for us to discuss about
everythings in Ruby, please?
If so,
1)I would like to know that how can I delete or remove a line in a text
file, please?
2)How can I read some letter in a line, please?
e.g. a text file like this:-

aabbcc
ddeeff
gghhii
jjkkll

1)I want to delete ddeeff become like this:-
aabbcc
gghhii
jjkkll

--------------------------------------------------

#!/usr/bin/ruby

data=<<EOF
aabbcc
ddeeff
gghhii
jjkkll
EOF

puts "Before:",data

data.sub!(/ddeeff\n/,"")

puts "\nAfter:",data

--------------------------------------------------

Output:

Before:
aabbcc
ddeeff
gghhii
jjkkll

After:
aabbcc
gghhii
jjkkll

2)I get the result like :-
hh

Please explain the circumstances under which you want to get this result.

--
Paul Lutus
http://www.arachnoid.com

Dont do his homework for him :slight_smile:

Paul Lutus wrote:

Oscar Lok wrote:

Hi, everybody

This is my first time get into this forum.
I would like to know that is it a forum for us to discuss about
everythings in Ruby, please?
If so,
1)I would like to know that how can I delete or remove a line in a text
file, please?
2)How can I read some letter in a line, please?
e.g. a text file like this:-

aabbcc
ddeeff
gghhii
jjkkll

1)I want to delete ddeeff become like this:-
aabbcc
gghhii
jjkkll

--------------------------------------------------

#!/usr/bin/ruby

data=<<EOF
aabbcc
ddeeff
gghhii
jjkkll
EOF

puts "Before:",data

data.sub!(/ddeeff\n/,"")

puts "\nAfter:",data

--------------------------------------------------

Output:

Before:
aabbcc
ddeeff
gghhii
jjkkll

After:
aabbcc
gghhii
jjkkll

2)I get the result like :-
hh

Please explain the circumstances under which you want to get this result.

He said the text is "in a file"
Cheers

Mike Durham wrote:

/ ...

2)I get the result like :-
hh

Please explain the circumstances under which you want to get this result.

He said the text is "in a file"

I generally don't require newbies to create and then read a file for
demonstration purposes. I prefer my examples to run exactly as posted. This
avoids the otherwise inevitable "I tried it and it didn't work!"" replies.

What this means is the OP needs to explain the circumstances under which he
wants two "h" character in a row, clearly and unambiguously. Before, after,
and in between.

And now, on re-reading his first post, I can't tell whether he got that
result and didn't want it, or didn't get it and wanted it. Or ... got it
and wanted it, and thought he would mention it in passing.

···

--
Paul Lutus
http://www.arachnoid.com

Paul Lutus wrote:

Mike Durham wrote:

/ ...

2)I get the result like :-
hh

Please explain the circumstances under which you want to get this result.

He said the text is "in a file"

I generally don't require newbies to create and then read a file for
demonstration purposes. I prefer my examples to run exactly as posted.
This
avoids the otherwise inevitable "I tried it and it didn't work!""
replies.

What this means is the OP needs to explain the circumstances under which
he
wants two "h" character in a row, clearly and unambiguously. Before,
after,
and in between.

And now, on re-reading his first post, I can't tell whether he got that
result and didn't want it, or didn't get it and wanted it. Or ... got it
and wanted it, and thought he would mention it in passing.

Hi,Paul Lutus
Thanks for your reply!

aabbcc
ddeeff
gghhii
jjkkll

2)I get the result like :-
hh

* What I would like to do is :-
1) go through each line from the begin
2) compare "hh" each letter on each line
3) found the line number of "gghhii"
4) extract "hh" of it.

Thanks!
Oscar

···

--
Posted via http://www.ruby-forum.com/\.

Oscar Lok wrote:

/ ...

And now, on re-reading his first post, I can't tell whether he got that
result and didn't want it, or didn't get it and wanted it. Or ... got it
and wanted it, and thought he would mention it in passing.

Hi,Paul Lutus
Thanks for your reply!

aabbcc
ddeeff
gghhii
jjkkll

2)I get the result like :-
hh

* What I would like to do is :-
1) go through each line from the begin
2) compare "hh" each letter on each line
3) found the line number of "gghhii"
4) extract "hh" of it.

Please provide an example. I think you don't literally mean you want 'hh'
specifically, but these are meant to stand in for the actual characters you
want. Yes? Otherwise this would work:

···

-------------------------------------------

#!/usr/bin/ruby

data = File.read("example.txt")

n = 0

data.each do |line|
   test = line.sub(/..(..)../,"\\1")
   print "#{n}: #{test}\n" if test =~ /hh/
   n += 1
end

-------------------------------------------

Example file contains:

aabbcc
ddeeff
gghhii
jjkkll

Output:

2: hh

Conclusion? You simply must explain what you want, clearly enough that it
can be turned into code.

--
Paul Lutus
http://www.arachnoid.com

Example file contains:

aabbcc
ddeeff
gghhii
jjkkll

Output:

2: hh

Thanks a lots!
Ruby script really is a great script!
By the way, may I know that is there any function like e.g. left(line,
3), right(line, 2) and mid(line, 1, 4) to extract letter from a line,
please?
e.g.
mid(gghhii,3,2) return hh
left(gghhii,3) return ggh
right(gghhii,3) return hii

Thanks!
Oscar

···

--
Posted via http://www.ruby-forum.com/\.

ri String# gives (for ruby-1.8.5):

-------------------------------------------------------------- String#
     str[fixnum] => fixnum or nil
     str[fixnum, fixnum] => new_str or nil
     str[range] => new_str or nil
     str[regexp] => new_str or nil
     str[regexp, fixnum] => new_str or nil
     str[other_str] => new_str or nil
     str.slice(fixnum) => fixnum or nil
     str.slice(fixnum, fixnum) => new_str or nil
     str.slice(range) => new_str or nil
     str.slice(regexp) => new_str or nil
     str.slice(regexp, fixnum) => new_str or nil
     str.slice(other_str) => new_str or nil

···

On Sat, 16 Sep 2006, Oscar Lok wrote:

By the way, may I know that is there any function like e.g. left(line,
3), right(line, 2) and mid(line, 1, 4) to extract letter from a line,
please?
e.g.
mid(gghhii,3,2) return hh
left(gghhii,3) return ggh
right(gghhii,3) return hii

------------------------------------------------------------------------
     Element Reference---If passed a single +Fixnum+, returns the code
     of the character at that position. If passed two +Fixnum+ objects,
     returns a substring starting at the offset given by the first, and
     a length given by the second. If given a range, a substring
     containing characters at offsets given by the range is returned. In
     all three cases, if an offset is negative, it is counted from the
     end of _str_. Returns +nil+ if the initial offset falls outside the
     string, the length is negative, or the beginning of the range is
     greater than the end.

     If a +Regexp+ is supplied, the matching portion of _str_ is
     returned. If a numeric parameter follows the regular expression,
     that component of the +MatchData+ is returned instead. If a
     +String+ is given, that string is returned if it occurs in _str_.
     In both cases, +nil+ is returned if there is no match.

        a = "hello there"
        a[1] #=> 101
        a[1,3] #=> "ell"
        a[1..3] #=> "ell"
        a[-3,2] #=> "er"
        a[-4..-2] #=> "her"
        a[12..-1] #=> nil
        a[-2..-4] #=> ""
        a[/[aeiou](.)\1/] #=> "ell"
        a[/[aeiou](.)\1/, 0] #=> "ell"
        a[/[aeiou](.)\1/, 1] #=> "l"
        a[/[aeiou](.)\1/, 2] #=> nil
        a["lo"] #=> "lo"
        a["bye"] #=> nil

Take the time to explore ri, which you should have on your system.
It is very good.

Thanks!
Oscar

        Hugh

Oscar Lok wrote:

/ ...

Thanks a lots!
Ruby script really is a great script!
By the way, may I know that is there any function like e.g. left(line,
3), right(line, 2) and mid(line, 1, 4) to extract letter from a line,
please?
e.g.
mid(gghhii,3,2) return hh
left(gghhii,3) return ggh
right(gghhii,3) return hii

#!/usr/bin/ruby

s = "gghhii"

puts s
puts s[0,2]
puts s[2,2]
puts s[-2,2]

Output:

gghhii
gg
hh
ii

···

--
Paul Lutus
http://www.arachnoid.com

Hai,
It is neat and simple(compare to others programing)! Very easy to
understand!

Thanks!

Regards
Oscar

···

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

Oscar Lok wrote:

Hai,
It is neat and simple(compare to others programing)! Very easy to
understand!

Thanks!

You are welcome. I sincerely hope you will also read Hugh Sasse's post. It
offers more general guidance. IOW I gave you a fish, but he tries to teach
you how to fish.

···

--
Paul Lutus
http://www.arachnoid.com