http://www.perlmonks.org?node_id=388223

theroninwins has asked for the wisdom of the Perl Monks concerning the following question:

I know the title is not what it should be but I can't think of a better one so please feel free to rename it.
Anyway here is the problem:
open(OUT, "serlist.txt"); open(OUT2, "desclist.txt"); open(OUT3, "namefile.txt"); my @outlist; while (<OUT>) { my %hash; my @temp = split(/;/,$_); $hash{'file1'} = $temp[0]; $hash{'file2'} = <OUT2>; $hash{'file3'} = <OUT3>; #delete Return foreach (values %hash) { $_ =~ s/\n//gi; } push(@outlist,\%hash); print @outlist; } close(OUT);

What happens is that the hash is not only adding the new lines it learns after running through the text file but adds all he did before just as well. Maybe an examples helps at this point:
148.192.59.254;lwl-h81-cw-u-1-1.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst c +2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interface + Converter) slots} 148.192.59.254;stp-k41-cw-e-2-1.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst c +2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interface + Converter) slots} 148.192.59.254;stp-h104-cw-2-1-2.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst +c2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interfac +e Converter) slots} 148.192.59.254;lwl-h81-cw-u-1-1.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst c +2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interface + Converter) slots} 148.192.59.254;stp-k41-cw-e-2-1.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst c +2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interface + Converter) slots} 148.192.59.254;stp-h104-cw-2-1-2.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst +c2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interfac +e Converter) slots} 148.192.166.249;lwl-h81-cw-u-1-1.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst +c2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interfac +e Converter) slots} 148.192.166.249;stp-k41-cw-e-2-1.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst +c2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interfac +e Converter) slots} 148.192.166.249;stp-h104-cw-2-1-2.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst + c2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interfa +ce Converter) slots} 148.192.166.249;STP-K91-CW-2-5-5.bc.de.bic;FOC0701Z22M;Cisco Catalyst +c2950 switch with 24 10/100 BaseTX ports} 148.192.59.254;lwl-h81-cw-u-1-1.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst c +2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interface + Converter) slots} 148.192.59.254;stp-k41-cw-e-2-1.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst c +2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interface + Converter) slots} 148.192.59.254;stp-h104-cw-2-1-2.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst +c2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interfac +e Converter) slots} 148.192.166.249;lwl-h81-cw-u-1-1.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst +c2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interfac +e Converter) slots} 148.192.166.249;stp-k41-cw-e-2-1.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst +c2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interfac +e Converter) slots} 148.192.166.249;stp-h104-cw-2-1-2.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst + c2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interfa +ce Converter) slots} 148.192.166.249;STP-K91-CW-2-5-5.bc.de.bic;FOC0701Z22M;Cisco Catalyst +c2950 switch with 24 10/100 BaseTX ports} 148.192.88.253;lwl-h81-cw-u-1-1.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst c +2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interface + Converter) slots} 148.192.88.253;stp-k41-cw-e-2-1.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst c +2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interface + Converter) slots} 148.192.88.253;stp-h104-cw-2-1-2.bc.de.bic;FOC0749Z2ZW;Cisco Catalyst +c2950 switch with 24 10/100 BaseTX ports and 2 GBIC (Gigabit Interfac +e Converter) slots} 148.192.88.253;STP-K91-CW-2-5-5.bc.de.bic;FOC0701Z22M;Cisco Catalyst c +2950 switch with 24 10/100 BaseTX ports} 148.192.88.253;stp-g101-cw-1-1-1.bc.de.bic;FOC0643W0DP;Cisco Catalyst +c2950 switch with 24 10/100 BaseTX ports}

As you can see the first 3 lines are the first 3 in the second section and then it repeats it again adding the new line that comes. Then it reapeats the first and second section, and then repeats it again appending the new stuff. The problem is that thjere are about 300 components that need checking and that is goes way over 400MB where only the last bit is needed so to say. And the IPs are also wrong on the repetitions. So how can i do it that the hash only appends the new line(s). Sorry for that explanaition but i can't really do it better this time.

Edit by castaway, readmore tags added.

Replies are listed 'Best First'.
Re: Hash is appending old and new data
by rupesh (Hermit) on Sep 03, 2004 at 09:36 UTC
Re: Hash is appending old and new data
by Eimi Metamorphoumai (Deacon) on Sep 03, 2004 at 13:27 UTC
    It seems to me (without a thorough understanding of what you're actually doing) that the problem is you're declaring @outlist outside of the loop, then for every line in OUT you're pushing more data onto @outlist and then printing the whole thing. If you move the my @outlist inside the while where you declare your hash, or if you move the print @outlist outside of the loop, you should only get each item once.