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


In reply to Need Help with foreach statement????? by brawal128

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.