[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Howto Delete 3 Leftmost Characters

Skeets

7/30/2006 9:30:00 PM

i'm sure this is easy, but i've gone through Pickaxe's string methods,
searched the web and searched the groups, and i can't figure out how to
do this in Ruby.

i grep a file and it returns the following string:

#ip 127.0.0.1

i now want to get rid of "#ip" so i can then strip the remaining string
to get rid of spaces.

however, i can't find out how to delete the 3 leftmost characters - in
this case "#ip".

thanks for any tips to get this done - i would think it is a matter of
just knowing the correct method.

17 Answers

William James

7/30/2006 9:37:00 PM

0


Skeets wrote:
> i'm sure this is easy, but i've gone through Pickaxe's string methods,
> searched the web and searched the groups, and i can't figure out how to
> do this in Ruby.
>
> i grep a file and it returns the following string:
>
> #ip 127.0.0.1
>
> i now want to get rid of "#ip" so i can then strip the remaining string
> to get rid of spaces.
>
> however, i can't find out how to delete the 3 leftmost characters - in
> this case "#ip".
>
> thanks for any tips to get this done - i would think it is a matter of
> just knowing the correct method.

irb(main):001:0> s="#ip 127.0.0.1"
=> "#ip 127.0.0.1"
irb(main):002:0> s.slice!(0,3)
=> "#ip"
irb(main):003:0> s
=> " 127.0.0.1"

Robert Klemme

7/30/2006 9:44:00 PM

0

Skeets wrote:
> i'm sure this is easy, but i've gone through Pickaxe's string methods,
> searched the web and searched the groups, and i can't figure out how to
> do this in Ruby.
>
> i grep a file and it returns the following string:
>
> #ip 127.0.0.1
>
> i now want to get rid of "#ip" so i can then strip the remaining string
> to get rid of spaces.
>
> however, i can't find out how to delete the 3 leftmost characters - in
> this case "#ip".
>
> thanks for any tips to get this done - i would think it is a matter of
> just knowing the correct method.
>

If you do the grepping in Ruby you can do something like this:

ip = nil

File.foreach("foo.txt") do |line|
ip = $1 if /^#ip\s+(\d+(?:\.\d+){3})/ =~ line
end

HTH

robert

Esteban Manchado Velázquez

7/30/2006 9:52:00 PM

0

On Mon, Jul 31, 2006 at 06:30:13AM +0900, Skeets wrote:
> i'm sure this is easy, but i've gone through Pickaxe's string methods,
> searched the web and searched the groups, and i can't figure out how to
> do this in Ruby.
>
> i grep a file and it returns the following string:
>
> #ip 127.0.0.1
>
> i now want to get rid of "#ip" so i can then strip the remaining string
> to get rid of spaces.
>
> however, i can't find out how to delete the 3 leftmost characters - in
> this case "#ip".

"#ip 127.0.0.1"[3..-1] # => " 127.0.0.1"
"#ip 127.0.0.1"[4..-1] # => "127.0.0.1"

But you're probably better off using regular expressions instead of fixed
indices:

"#ip 127.0.0.1".sub(/^#ip\s+/, '') # => "127.0.0.1"

That is, "remove, from the beginning of the line, '#ip' followed by one or
more space characters (be them spaces, tabs or whatever)". If you don't know
regular expressions, _and_ you're into text processing, I recommend you to go
and read some book about regular expressions and practice a little (under
Linux there are a couple of handy utilities for that; I'm sure there must be
also for other platforms).

Regards,

--
Esteban Manchado Velázquez <zoso@foton.es> - http://ww...
EuropeSwPatentFree - http://EuropeSwPatentFree.his...

Skeets

7/30/2006 9:59:00 PM

0

William James wrote:
> Skeets wrote:
> > i'm sure this is easy, but i've gone through Pickaxe's string methods,
> > searched the web and searched the groups, and i can't figure out how to
> > do this in Ruby.
> >
> > i grep a file and it returns the following string:
> >
> > #ip 127.0.0.1
> >
> > i now want to get rid of "#ip" so i can then strip the remaining string
> > to get rid of spaces.
> >
> > however, i can't find out how to delete the 3 leftmost characters - in
> > this case "#ip".
> >
> > thanks for any tips to get this done - i would think it is a matter of
> > just knowing the correct method.
>
> irb(main):001:0> s="#ip 127.0.0.1"
> => "#ip 127.0.0.1"
> irb(main):002:0> s.slice!(0,3)
> => "#ip"
> irb(main):003:0> s
> => " 127.0.0.1"

William, thanks. when i follow your approach, i get your result via
irb. however, i get a different result when i run code in a file. i'm
doing something different, but i don't know what.

here is the code:

#!/usr/bin/env ruby

if File.exist?( "ip.txt" )

f = File.open("ip.txt').grep(/#ip/)
(f.to_s).slice!(0,3)
# f = f.strip

end

puts f # this outputs "#ip 127.0.0.1" - i was expecting it to output
"127.0.0.1"

if i have

f = (f.to_s).slice!(0,3)

instead fo

(f.to_s).slice!(0,3)

then "puts f" prints "#ip"

tia...

dblack

7/30/2006 10:04:00 PM

0

Chad Perrin

7/30/2006 10:08:00 PM

0

On Mon, Jul 31, 2006 at 06:51:35AM +0900, Esteban Manchado Velázquez wrote:
>
> But you're probably better off using regular expressions instead of fixed
> indices:
>
> "#ip 127.0.0.1".sub(/^#ip\s+/, '') # => "127.0.0.1"
>
> That is, "remove, from the beginning of the line, '#ip' followed by one or
> more space characters (be them spaces, tabs or whatever)". If you don't know
> regular expressions, _and_ you're into text processing, I recommend you to go
> and read some book about regular expressions and practice a little (under
> Linux there are a couple of handy utilities for that; I'm sure there must be
> also for other platforms).

Actually, the best text I've ever had the pleasure to read for learning
regex syntax from scratch was Learning Perl. The downside is that you
kinda have to know some Perl for it to make much sense (or go through
the entire book and learn regular expressions as a part of learning
Perl) -- which is only a downside if you don't want to learn Perl, of
course. It's a reasonably easy book to work through, though, and great
at what it does. All else being equal, it's better to know more
languages anyway.

If there are some really good materials for regexen that are specific to
Ruby, of course, that may be more what you need -- but I don't know of
any off the top of my head. Obviously, several of the available e-books
and tutorials you can find online address regex usage, but as far as
I've seen they don't tend to do as good a job of it as Learning Perl.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"There comes a time in the history of any project when it becomes necessary
to shoot the engineers and begin production." - MacUser, November 1990

Skeets

7/30/2006 10:14:00 PM

0

Esteban Manchado Velázquez wrote:
> On Mon, Jul 31, 2006 at 06:30:13AM +0900, Skeets wrote:
> > i'm sure this is easy, but i've gone through Pickaxe's string methods,
> > searched the web and searched the groups, and i can't figure out how to
> > do this in Ruby.
> >
> > i grep a file and it returns the following string:
> >
> > #ip 127.0.0.1
> >
> > i now want to get rid of "#ip" so i can then strip the remaining string
> > to get rid of spaces.
> >
> > however, i can't find out how to delete the 3 leftmost characters - in
> > this case "#ip".
>
> "#ip 127.0.0.1"[3..-1] # => " 127.0.0.1"
> "#ip 127.0.0.1"[4..-1] # => "127.0.0.1"
>
> But you're probably better off using regular expressions instead of fixed
> indices:
>
> "#ip 127.0.0.1".sub(/^#ip\s+/, '') # => "127.0.0.1"
>
> That is, "remove, from the beginning of the line, '#ip' followed by one or
> more space characters (be them spaces, tabs or whatever)". If you don't know
> regular expressions, _and_ you're into text processing, I recommend you to go
> and read some book about regular expressions and practice a little (under
> Linux there are a couple of handy utilities for that; I'm sure there must be
> also for other platforms).
>
> Regards,
>
> --
> Esteban Manchado Velázquez <zoso@foton.es> - http://ww...

Esteban, and all - thank you.

this did the trick...

if File.exist?( 'current_ip.txt' )

f = File.open('current_ip.txt').grep(/#ip/)
f = f[0].sub(/^#ip\s+/, '')

end

puts f

the File.open grep line returned an array. i had to sort that out
first. i know this file will always have only one instance of #ip - is
there any way to force grep to return as a variable instead of an
array?

also, i believe the regex Esteban gave will get rid of all white spaces
- both to the left and right of the ip address.

thanks to everyone for the help.

Skeets

7/30/2006 10:23:00 PM

0

> also, i believe the regex Esteban gave will get rid of all white spaces
> - both to the left and right of the ip address.

actually, this isn't true so i still have to use the strip method.

i have some regex experience - but not too much.

i pretty much take some pain killers and then try and figure out what i
need as the need arises. ;-)

i'll bet this regex can be update to remove all spaces, however, i will
have to look into that next week.

thanks to everyone for getting me back on track.

i'm working on my first ruby program - and i'm impressed with ruby.

lots to learn, though.

William James

7/30/2006 10:35:00 PM

0


Skeets wrote:
> Esteban Manchado Velázquez wrote:
> > On Mon, Jul 31, 2006 at 06:30:13AM +0900, Skeets wrote:
> > > i'm sure this is easy, but i've gone through Pickaxe's string methods,
> > > searched the web and searched the groups, and i can't figure out how to
> > > do this in Ruby.
> > >
> > > i grep a file and it returns the following string:
> > >
> > > #ip 127.0.0.1
> > >
> > > i now want to get rid of "#ip" so i can then strip the remaining string
> > > to get rid of spaces.
> > >
> > > however, i can't find out how to delete the 3 leftmost characters - in
> > > this case "#ip".
> >
> > "#ip 127.0.0.1"[3..-1] # => " 127.0.0.1"
> > "#ip 127.0.0.1"[4..-1] # => "127.0.0.1"
> >
> > But you're probably better off using regular expressions instead of fixed
> > indices:
> >
> > "#ip 127.0.0.1".sub(/^#ip\s+/, '') # => "127.0.0.1"
> >
> > That is, "remove, from the beginning of the line, '#ip' followed by one or
> > more space characters (be them spaces, tabs or whatever)". If you don't know
> > regular expressions, _and_ you're into text processing, I recommend you to go
> > and read some book about regular expressions and practice a little (under
> > Linux there are a couple of handy utilities for that; I'm sure there must be
> > also for other platforms).
> >
> > Regards,
> >
> > --
> > Esteban Manchado Velázquez <zoso@foton.es> - http://ww...
>
> Esteban, and all - thank you.
>
> this did the trick...
>
> if File.exist?( 'current_ip.txt' )
>
> f = File.open('current_ip.txt').grep(/#ip/)
> f = f[0].sub(/^#ip\s+/, '')
>
> end
>
> puts f
>
> the File.open grep line returned an array. i had to sort that out
> first. i know this file will always have only one instance of #ip - is
> there any way to force grep to return as a variable instead of an
> array?
>
> also, i believe the regex Esteban gave will get rid of all white spaces
> - both to the left and right of the ip address.
>
> thanks to everyone for the help.

file_name = 'current_ip.txt'
if File.exist?( file_name )
ip = File.read( file_name )[ /^#ip\s+([\d.]+)/, 1 ]
end
p ip

Daniel Schüle

7/30/2006 10:57:00 PM

0

Skeets schrieb:
> i'm sure this is easy, but i've gone through Pickaxe's string methods,
> searched the web and searched the groups, and i can't figure out how to
> do this in Ruby.
>
> i grep a file and it returns the following string:
>
> #ip 127.0.0.1
>
> i now want to get rid of "#ip" so i can then strip the remaining string
> to get rid of spaces.
>
> however, i can't find out how to delete the 3 leftmost characters - in
> this case "#ip".
>
> thanks for any tips to get this done - i would think it is a matter of
> just knowing the correct method.
>

irb(main):022:0> "abcdeg".sub(/.../, "")
=> "deg"
irb(main):023:0> "abcdeg"[3..-1]
=> "deg"
irb(main):024:0>

hth, Daniel