Author Topic: Array count inaccurate.  (Read 555 times)

0 Members and 1 Guest are viewing this topic.

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Array count inaccurate.
« on: September 27, 2014, 11:45:52 pm »
This should be really simple but for some reason it's not. All I'm trying to do is count the number of IP addresses in a file, and I really did not expect to have any trouble with this at all.

Code: (ruby) [Select]
reg = IO.read(ARGV[0])
rr += reg.scan /(?:\d{1,3}\.){3}\d{1,3}/

puts rr.size
I've manually checked and there should be somewhere around 1000 ip addresses in the particular file I'm using. All this code does is look for IPs in the file using regex, and then puts all of the IPs into an array. Then it uses Ruby's .size method to count how many items are in the array. For some reason this is inaccurate though, telling me there's over 40,000 IPs in the file. Which is impossible. There's no way. The code used to look this this:
Code: (ruby) [Select]
reg = IO.read(ARGV[0])
rr = []
r = reg.scan /(?:\d{1,3}\.){3}\d{1,3}/

rr << r
puts rr.size
But that always returned 1, no matter what. Now I know everyone in the world hates Ruby, but I feel like my particular problem has more to do with general coding concepts.
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: Array count inaccurate.
« Reply #1 on: September 28, 2014, 01:12:17 am »

Code: [Select]
reg=IO.read(ARGV[0]);
print reg.scan(/\d+\.\d+\.\d+\.\d+/).size;

This works

As does this lol
Code: [Select]
print IO.read("input.txt").scan(/\d+\.\d+\.\d+\.\d+/).size;
The problem, I think, arose from your use of the +=
« Last Edit: September 28, 2014, 01:19:07 am by HTH »
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: Array count inaccurate.
« Reply #2 on: September 28, 2014, 09:15:18 pm »
I knew it would be insanely simple. Thanks buddy. Looks like you could use an extra cookie  ;D
« Last Edit: September 28, 2014, 09:15:48 pm by lucid »
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: Array count inaccurate.
« Reply #3 on: September 28, 2014, 10:44:00 pm »
Code: (ruby) [Select]
reg = IO.read(ARGV[0])
rr += reg.scan /(?:\d{1,3}\.){3}\d{1,3}/

puts rr.size
It looks like reg.scan is returning an array, yet += seems to be an arithmetic operator. I'm running ruby 1.9.3p484 and it that snippet just caused a NoMethodError exception to be thrown.

Code: (ruby) [Select]
reg = IO.read(ARGV[0])
rr = []
r = reg.scan /(?:\d{1,3}\.){3}\d{1,3}/

rr << r
puts rr.size
But that always returned 1
Looks like you're pushing r to the end of rr, so you're actually getting [ ["127.0.0.1", "111.111.111.111", "more.ip.addresses.0"] ] and therefore only one element in rr.

Regardless, wouldn't grep be more suited to this task?
Code: (Bash) [Select]
grep -P -c "\d+\.\d+\.\d+\.\d+" file.txt # Print number of matches for (pseudo, since only the 'shape' of address is checked) IPv4 pattern in file.txt.
Indeed it most likely would. I thought it would be fun to write it in Ruby since I've only been doing networking shit with Ruby and I got bored. HTH's advice worked well, and so far is the only advice that worked for me.
« Last Edit: September 29, 2014, 10:00:15 am by lucid »