Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Array

Status
Not open for further replies.

olmos

Technical User
Joined
Oct 25, 2000
Messages
135
Location
US
I am reading a file with a ip address and email addresses in each line ( in a comma deliminated format). I created an array and split it by commas. I need to read each
ip address by subnet and group them all together so that later I can use the email addresses from those subnets for creating a batch file that will email everyone for each different subnet.

I need to get rid of the duplicate email addresses for the subnets.
My array is currently just reading it line by line but I need it to somehow group all the subnets together. I am not sure how to do this since I am just learning perl..
Should I use a hash ? but I'm not too sure how this may work.

Thanks for your help,
olmos..
 
my @subnet_arr = ();
while (<FILE>) {
my ($ip, $email) = split /,/, $_;
my ($ip1, $ip2, $ip3, $ip4) = split /\./, $ip;
my $ip_subnet = &quot;$ip3&quot;; ### subnet is 3rd ip piece
my $element = $ip_subnet . &quot;|&quot; . $email;
push(@subnet_arr, $element);
}

my @sorted_subnet_emails = sort @subnet_arr;

my $email_save = &quot;&quot;;
foreach (@sorted_subnet_emails) {
if ($email ne $email_save) {
my ($ip_subnet, $email) = split /\|/, $_;
$email_save = $email;
}
}

***NOTE: This is completely untested, but it should work.

I'm sure there's a better way to do this...but this should get you going anyway. You could use hashes, but
the sort's with hashes aren't as straight forward as the
sorts with arrays.
Hardy Merrill
Mission Critical Linux, Inc.
 
Thanks for your help. Do you know how I can add commas between each
element. I do not know how many elements I may have. I used

sub comify_series {
join(&quot;, &quot;, @_[0 .. ($#_)])

but doesn't seem to work .

Thanks ,
olmos
 
You don't need to specify the element numbers, just simply:
Code:
join ',',@_;
will do the trick.

brendanc@icehouse.net
 
My program is sort of working except, it is not picking up some of the elements,and it
seems to happen when it gets close to the reading the end of the file, usually when the emails have dots in the name. It works fine when the emails do not have any dots in the name. I don't know why because at the start of the file it does contain emails with dots and it is printing them out ok. Any suggestions?

Thanks for your help,
olmos
.
...

while (<INFO>)

{
chomp;

@elements = split(/,{1,}/);

$ipaddress = $elements[0];
$user = &quot;$elements[1]&quot;;
$sysadmin = &quot;$elements[2]&quot;;

($ip1, $ip2, $ip3, $ip4) = split(/\./,$ipaddress);
## Checking to see if there was no third element...
if ($sysadmin eq $null)
{
$sysadmin = &quot;$user&quot;;
}

## Checking to see whether or not we have a new subnet...
if ($ipsubnet eq &quot;0&quot;)
{
## First time through...
push(@subnetemail, $sysadmin);
$ipsubnet = $ip3;
$classip = &quot;$ip1\.$ip2&quot;; ## This is the first, so we need a
classip...
}
elsif ($ipsubnet ne $ip3)
{
## This is a unique subnet -- time to print out the last subnet
$oipsubnet = $ipsubnet;
$ipsubnet = $ip3;
@uniquemail = grep { ! $seen{$_} ++ } @subnetemail;
$addcomma = join(&quot;, &quot;, @uniquemail);
print &quot;$emailprogrampath $messagepath \&quot;$reportpath\\$classip.$oipsubne
t$filetype\&quot; &quot; .
&quot;-t $addcomma -noh2 -b $sender -s \&quot;$subject $classip\.$oipsubnet
.x \&quot; \n&quot; ;
$trash = splice(@subnetemail, 0); ## Clearing out the @subnetemail a
rray...
@subnetemail = $trash;
$classip = &quot;$ip1\.$ip2&quot;; ## Build the classip for this new
subnet.
## First time through...
push(@subnetemail, $sysadmin);
$ipsubnet = $ip3;
$classip = &quot;$ip1\.$ip2&quot;; ## This is the first, so we need a
classip...
}
elsif ($ipsubnet ne $ip3)
{
## This is a unique subnet -- time to print out the last subnet
$oipsubnet = $ipsubnet;
$ipsubnet = $ip3;
@uniquemail = grep { ! $seen{$_} ++ } @subnetemail;
$addcomma = join(&quot;, &quot;, @uniquemail);
print &quot;$emailprogrampath $messagepath \&quot;$reportpath\\$classip.$oipsubne
t$filetype\&quot; &quot; .
&quot;-t $addcomma -noh2 -b $sender -s \&quot;$subject $classip\.$oipsubnet
.x \&quot; \n&quot; ;
$trash = splice(@subnetemail, 0); ## Clearing out the @subnetemail a
rray...
@subnetemail = $trash;
$classip = &quot;$ip1\.$ip2&quot;; ## Build the classip for this new
subnet.
push(@subnetemail, $sysadmin);
}
else
{
## This is another of the same subnet we are already looking at.
push(@subnetemail, $sysadmin);
}

} # end while
@uniquemail = grep { ! $seen{$_} ++ } @subnetemail;
$addcomma = join(&quot;, &quot;, @uniquemail);
print &quot;$emailprogrampath $messagepath \&quot;$reportpath\\$classip.$ip3$filetype\&quot; -
t $addcomma -noh2 -b $sender -s \&quot;$subject $classip\.$ip3.x \&quot; \n&quot;;
close(INFO); # Close the file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top