Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Generating a List of numbers

by mlebel (Hermit)
on Feb 02, 2012 at 02:59 UTC ( #951330=note: print w/ replies, xml ) Need Help??


in reply to Generating a List of numbers

I read the post "I know what I mean. Why don't you?" so based on that I will re-write this again.(i'm really not sure how short i can keep it but i will try).

I fixed a part of my problem. I needed to open the "tmpipaccountingfile" during the outer loop and close it right after the end of the second loop.

I shrunk the "tmpipaccountingfile" to this:

Source Destination Packets Bytes 15.254.32.120 10.2.9.2 5 504 209.68.19.130 10.2.9.2 33 15941 17.149.36.162 10.2.9.3 14 3416 17.149.36.15 10.2.9.3 14 3404 67.148.147.65 10.2.9.3 57 39207 8.8.8.8 10.2.9.6 84 8006 Accounting data age is 0w1d Box#

And i put the rest of the data within the script which looks like this:

#!/usr/bin/perl use warnings; use strict; my $tmpipaccountingfile = "tmpipaccountingfile"; my $DestDevice = "Box"; foreach my $OuterData (<DATA>) { print "OuterData = $OuterData"; open TMPIPACCOUNTINGFILE, "<", "$tmpipaccountingfile"; foreach my $Line (<TMPIPACCOUNTINGFILE>) { chomp $Line; next if ($Line =~ m/^sh/); next if ($Line =~ m/^\s*Source/); next if ($Line =~ m/^$DestDevice/); next if ($Line =~ m/^Accounting/); next if ($Line =~ m/^$/); next unless ($Line =~ m/^\s*\d*.\d*.\d*.\d*\s*$OuterData/); print "Line = $Line\n\n"; } close TMPIPACCOUNTINGFILE; } exit; __DATA__ 10.2.9.2 10.2.9.3 10.2.9.6

Now this mostly works. The new problem that I am running into now, is that I don't get any output with the script like this. But if i replace the line "next unless ($Line =~ m/^\s*\d*.\d*.\d*.\d*\s*"$OuterData"/);" with "next unless ($Line =~ m/^\s*\d*.\d*.\d*.\d*\s*"10.2.9.6"/);" for example, I will get the following output:

OuterData = 10.2.9.2 Line = 8.8.8.8 10.2.9.6 84 8006 OuterData = 10.2.9.3 Line = 8.8.8.8 10.2.9.6 84 8006 OuterData = 10.2.9.6 Line = 8.8.8.8 10.2.9.6 84 8006

I beleive that this proves that it works but it's not what I am looking for. The output that I am expecting to see with the code that I provided is this:

OuterData = 10.2.9.2 Line = 15.254.32.120 10.2.9.2 5 504 Line = 209.68.19.130 10.2.9.2 33 15941 OuterData = 10.2.9.3 Line = 17.149.36.162 10.2.9.3 14 3416 Line = 17.149.36.15 10.2.9.3 14 3404 Line = 17.149.36.15 10.2.9.3 14 3404 Line = 67.148.147.65 10.2.9.3 57 39207 OuterData = 10.2.9.6 Line = 8.8.8.8 10.2.9.6 84 8006

I hope that this is clear because I have no clue how to simplify it any further.

Aaron, I will be going to read up on hash array's since it seems that it's probably the way to achieve this from what I gather from all the posts. From what you said, it sounds like what I am looking for and if I need a hash to acheive this, how would that be written?

The results of each ip groups found would ultimately be pushed to an array for some calculations to be done for each ip's individually. I say this incase it influences any code that you might provide

Now, I must say that you guy's are pretty tough, but it's ok, I have thick skin and I am learning in the process to become a better programmer. so thank you.

So, lastly, how do i fix this?


Comment on Re: Generating a List of numbers
Select or Download Code
Re^2: Generating a List of numbers
by choroba (Abbot) on Feb 02, 2012 at 09:22 UTC
    You are not chomping $OuterData. Therefore, it contains a newline at the end which prevents it from matching in the regular expression.
    Update: Looping over the file several times makes your algorithm very slow if the file is big. Using a hash, you can avoid this problem:

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://951330]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-10-31 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (215 votes), past polls