Wagner,
Many thanks for the help.
It turned out that the problem is related to the input file, where each line ends with both a \n and \r. It seems that chomp() has removed \n, but \r is still there, so that the key and value etc are not integers.
I also noticed that if I read the file, and then print the line, the \r seems being removed.
I am wondering if there is some general guidelines how to handle a file which may/may not have \r at the end of each line.
-Zhenhai
============================================ On Tue, 17 May 2005, Wagner, David --- Senior Programmer Analyst --- WGO wrote:
Zhenhai Duan wrote:Hi,
I have a simple code but I just cannot get the correct result. Can anyone take a look at the code for me? Many thanks.
print "\nOutput: AS -- Subnets\n"; foreach $key (sort numerically (keys %subnets)) { print "key = $key", "\n"; @subnet = split(/:/, $subnets{$key}); for ($i = 0; $i <= $#subnet; $i++) { print$subnet[$i], "\n"; } }
foreach $key ( sort numerically ( keys %subnets)) { printf outfile "%d\t->\t%s\n", $key, $subnets{$key}; }
I modified only the subroutine numerically to be {$a <=> $b } I also made the data by doing:
my %subnets = qw(1 1:4 4 4:1 80 80:);
I added a use strict and warnings which required minor changes to add my to your variables.
Output came out as you desired ( see below). I am running AS 5.8.3 build 809. Wags ;) =====================================================================
Output: AS -- Subnets key = 1 1 4 key = 4 4 1 key = 80 80 1 -> 1:4 4 -> 4:1 80 -> 80:
THe output on STDOUT is:
Output: AS -- Subnets key = 1 1 4 key = 4 4 1 key = 80 80
ANd it is correct. However the results in the output file (outfile) is:
4 -> 1 1 -> 480 -> 80
Actually I would expect:
1 -> 1:4 4 -> 4:1 80: -> 80
(I am using Net::Netmask module, and the perl version is:
This is perl, v5.8.3 built for i386-linux-thread-multi
) Many thanks for the help, -Zhenhai
******************************************************* This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. *******************************************************
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>