Robert Klemme
9/28/2008 8:46:00 AM
On 28.09.2008 03:03, jackster the jackle wrote:
> thanks...I got that to work...now for my last question...I am trying to
> match on a hash value. I have a text file that gets opened with 4
> different types of statements in it...the values are contained in my
> hash called "h". The values in the hash are (IN,OUT,PP,AA). @acl_list
> contains a bunch of firewall rules that either have the value IN,OUT,PP
> or AA in them. What I am trying to do is loop through @acl_list,
> substitute the value of h with the key (which works great) but then, I
> need to put the number 1 in the first firewall rule that contains IN,
> then the number 2 in the second firewall rule that contains IN etc.
> When I'm finished with the "IN" rules, I want to do the same thing for
> "OUT","PP" and "AA".
>
> What I have here works great but only for the "IN" rules:
>
> k= 1
> @acl_list.each do |acl|
> h.each {|key,value|
> acl.gsub!(/#{key}/,value)}
> if acl[/IN/]
> acl.gsub!(/^/,k.to_s)
> k+=1
> puts acl
> end
>
> What I really need is to match on the value of the hash, then add the
> numbers to the beginning of each line. I thought the following would
> work but it doen't (although #{value} does have the correct info in it).
> Can anyone help me figure out how I can get the loop to match on the
> value of my hash? I think if I can get this to work, everything will be
> the way I need it.
>
> k= 1
> @acl_list.each do |acl|
> h.each {|key,value|
> acl.gsub!(/#{key}/,value)}
> if acl[/#{value}/]
> acl.gsub!(/^/,k.to_s)
> k+=1
> puts acl
> end
It seems you are doing this too complicated and making your life harder
than necessary. If I read you correctly this is what you want to do:
you want to read a file where each line falls in one of four *fixed*
classes (IN, OUT...). You want to number each class individually and
print that out.
If you use a Hash you can do something like this (proper variable naming
goes a long way in making this readable and understandable):
counters = Hash.new 0
ARGF.each do |line|
line_class = line[/\b(?:IN|OUT|PP|AA)\b/]
raise "Unknown rule" unless line_class
print counters[line_class] += 1, " ", line
end
Cheers
robert