Parsing a file into a hash

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joe2684
    New Member
    • Jul 2007
    • 4

    Parsing a file into a hash

    I have got a file containing info abt node id and labels, which looks like this

    Code:
    -645	_1__46__1__46__1__46__145__45__RXN
    -435	_3__45__OXOACYL__45__ACP__45__REDUCT__45__RXN
    -1084	RXN__45__4262
    -565	RXN__45__4264
    -740	RXN__45__710
    -1290	RXN__45__7698
    -1017	LIPOXYGENASE__45__RXN
    -198	RXN__45__1321
    -1151	RXN__45__4121
    -55	GIBBERELLIN__45__3__45__BETA__45__DIOXYGENASE__45__RXN
    -1276	NARINGENIN__45__3__45__DIOXYGENASE__45__RXN
    -653	RXN__45__113
    -455	RXN__45__114
    i have to read this file into a hash, for which i converted it first into an array, and then to hash, using a program like this

    [CODE=perl]
    open (NODES, "node.txt") || die "File not found\n";
    @nodearray=<NOD ES>;
    %nodehash=@node array;
    [/CODE]

    now i have got to take labels as input from the user, and return the node id. I have been trying the following code

    [CODE=perl]
    # usr/bin/perl -w

    $start_node = 'AT1G51570';
    $end_node = 'AT5G1110';

    open (NODES, "node.txt") || die "File not found\n";
    @nodearray=<NOD ES>;
    %nodehash=@node array;

    while (($node_id,$lab el) = each %nodehash) {
    if ($nodehash{$lab el} = $start_node) {
    print $nodehash{$node _id};
    }
    }
    [/CODE]

    but nothing is working out. Please help me out on it. I am new to programming, and my deadline is just arriving. Please suggest something....
    Last edited by miller; Jul 24 '07, 04:25 PM. Reason: Code Tag and ReFormatting
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Please suggest something...
    ask a question that we can understand so we can attempt to answer it.

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Originally posted by joe2684
      but nothing is working out. Please help me out on it. I am new to programming, and my deadline is just arriving. Please suggest something....
      Just taking a quick look, I am going to guess that this is not the entire data file since the $start_node value doesn't exist anywhere in there.

      I must say that I agree with Kevin and need to clarify better what exactly you are asking and looking for. You say that the two fields in the file are node_id and label, in that order. Then the user enters a label, you are supposed to match it against the file and if a match occours, then print the node_id. From what I can see though, that is NOT what you are doing. You are comparing what the user would enter to a fixed value($start_no de), if the user were even able to enter a value. Your script doesn't allow for the user input that you are mentioning.

      I think that you really need to clean up this program and try to get it working. Heck, pick up a copy of Learning Perl as it will truely help you with the language and writing Perl code. If you still have problems after trying to meet your requirements, then be sure to repost, but please word the question better so we are able to assist you.

      Regards,

      Jeff

      Comment

      Working...