brawal128 has asked for the wisdom of the Perl Monks concerning the following question:
I'm still learning Perl and I'm a newbie. I have gotten the below hashes to store the information from the .csv files that I want to read and do something with. But, now I'm stuck on getting the information from the %firms into the $upload variable that I want to add and print out to a new .csv file. I would also like to compare the keyvalues to see where I am missing information that is not in the %firms hash and is in the %acs hash. Can someone please help me with figuring out why my hashes aren't working properly in the foreach statements? Any help on this is greatly appreciated. I feel like I'm right there, but I have never tried to work with 2 different files and trying to piece information from both to a completely new file.
#!/perl/bin/perl use strict; use warnings; use Data::Dumper; open FH1, "<", "namecleanupapcdevices1.csv" or die $!; open FH2, "<", "acs_import.csv" or die $!; my (%firms, %acs); my $testrun = ''; my $recnum = 1; my $recnum1 = 1; my ($upload, $text); while (defined( my $line = <FH1> ) ){ chomp $line; my ($type, $location1, $location2, $label, $model, $fi +rmware, $hostname, $contact, $serial, $ip, $mac) = split ',', $line, +12; #$location1 =~ s/"?//g; #$location2 =~ s/"?//g; $label =~ s/\(.+\)?//g; $firms{$recnum}{type} = $type; $firms{$recnum}{location1} = $location1.",".$location2 +; #$firms{$recnum}{location2} = $location2; $firms{$recnum}{label} = $label; $firms{$recnum}{model} = $model; $firms{$recnum}{firmware} = $firmware; $firms{$recnum}{hostname} = $hostname; $firms{$recnum}{contact} = $contact; $firms{$recnum}{serial} = $serial; $firms{$recnum}{ip} = $ip; $firms{$recnum}{mac} = $mac; $recnum++; } while (defined( my $line2 = <FH2> ) ){ chomp $line2; my ($name64, $update64, $description, $subnet, $suppor +t_radius, $radius_secret, $port, $support_keywrap, $keywrap_KEK, $key +wrap_mack, $keywrap_display, $support_TACACS, $tacacsSecret, $singlec +onnect, $legacyTACACS, $Location_String, $Device_Type_String, $System +_Owner) = split ',', $line2, 18; $subnet =~ s/\/32?//g; $acs{$recnum1}{name64} = $name64; $acs{$recnum1}{update64} = $update64; $acs{$recnum1}{description} = $description; $acs{$recnum1}{subnet} = $subnet; $acs{$recnum1}{support_radius} = $support_radius; $acs{$recnum1}{radius_secret} = $radius_secret; $acs{$recnum1}{port} = $port; $acs{$recnum1}{support_keywrap} = $support_keywrap; $acs{$recnum1}{keywrap_KEK} = $keywrap_KEK; $acs{$recnum1}{keywrap_mack} = $keywrap_mack; $acs{$recnum1}{keywrap_display} = $keywrap_display; $acs{$recnum1}{support_TACACS} = $support_TACACS; $acs{$recnum1}{tacacsSecret} = $tacacsSecret; $acs{$recnum1}{singleconnect} = $singleconnect; $acs{$recnum1}{legacyTACACS} = $legacyTACACS; $acs{$recnum1}{Location_String} = $Location_String; $acs{$recnum1}{Device_Type_String} = $Device_Type_Stri +ng; $acs{$recnum1}{System_Owner} = $System_Owner; $recnum1++; } foreach $recnum(keys %firms){ print Dumper $recnum; $firms{$recnum}{label} = $acs{$recnum1}{update64}; $firms{$recnum}{location1} = $acs{$recunm1}{description}; } foreach $recnum1(keys %acs){ print Dumper $recnum1; #foreach $recnum(sort keys %firms){ #if (($acs{$recnum1}{name64} = $firms{$recnum}{label}) + && ($firms{$recnum}{ip} = $acs{$recnum1}{subnet})){ $upload .= "$acs{$recnum1}{name64},$firms{$recnum}{lab +el},$firms{$recnum}{location1},$acs{$recnum1}{subnet}/32,$acs{$recnum +1}{support_radius},$acs{$recnum1}{radius_secret},$acs{$recnum1}{port} +,$acs{$recnum1}{support_keywrap},$acs{$recnum1}{keywrap_KEK},$acs{$re +cnum1}{keywrap_mack},$acs{$recnum1}{keywrap_display},$acs{$recnum1}{s +upport_TACACS},$acs{$recnum1}{tacacsSecret},$acs{$recnum1}{singleconn +ect},$acs{$recnum1}{legacyTACACS},$acs{$recnum1}{Location_String},$ac +s{$recnum1}{Device_Type_String},$acs{$recnum1}{System_Owner}\n"; } # elsif ($acs{$recnum1}{name64} != $firms{$recnum}{labe +l}){ # print "--- Not Found ---\n"; # print "Name: $acs{$recnum1}{name64}\nSubnet: $acs{$re +cnum1}{subnet}\n"; # next; # } print "completed\n"; open OutFile, ">", "ACS_import_list.csv" or die $!; print OutFile $upload; close OutFile; close FH1; close FH2;\
Thanks, Brandon
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Need Help with foreach statement?????
by graff (Chancellor) on Oct 01, 2015 at 21:52 UTC | |
|
Re: Need Help with foreach statement?????
by GotToBTru (Prior) on Oct 02, 2015 at 03:08 UTC | |
|
Re: Need Help with foreach statement?????
by NetWallah (Canon) on Oct 01, 2015 at 23:16 UTC |