
KevinR
Veteran

Oct 23, 2008, 7:23 PM
Post #15 of 17
(5976 views)
|
Re: [TheDragonReborn] Help with key/value pairs
[In reply to]
|
Can't Post
|
|
Fixing a couple of things in your code produces the output you desire but I don't understand what those numbers signify.
$dataFile = 'test.txt' ; unless(open(DATAFILE, $dataFile)) { print "Error !! Did not read file.\n" ; exit ; } $x = 1 ; $i = 1 ; while($line = <DATA>) { # Test file #print $line ; @tokens = split(/\s/, $line) ; $node1 = $tokens[0] ; $node2 = $tokens[1] ; if($KeyMap{"$node1"}) { # return $KeyMap{$node1} ; } else { $KeyMap{$node1} = $i; # Record the highest value node (Last increment will be highest node) $endvertex = $KeyMap{$node1} ; ++$i ; } if($KeyMap{"$node2"}) { # return $KeyMap{$node2} ; } else { $KeyMap{$node2} = $i ; # Record the highest value node (Last increment will be highest node) $endvertex = $KeyMap{$node2} ; ++$i ; } print $node1 , " ", $node2 , "\n" ; print $KeyMap{$node1}, " ", $KeyMap{$node2},"\n" ; ++$x ; } close DATAFILE ; print "\nendvertex = $endvertex\n" ; exit ; Your use of the "return" function is not correct, but I am not sure what you are trying to do in those lines. -------------------------------------------------
|