Processing a Data Structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Iris83
    New Member
    • Feb 2007
    • 7

    Processing a Data Structure

    Hi,

    I have a question about converting some of the data in my dataset but leave some data the way it is.

    I have a hash and if the key is present and the Value of the Hash equals B it should convert these number but if B is not present the data should remain the same.

    my output is that all the data is converted not in the only the cases were B is present.

    This is the script i have

    Code:
    while ($data = <Dataset>) {
    	if (exists $hash[1]) {
    		if ($hash[2]==B) {
    			for ($i = 0; $i < length($data); $i++) {
    				$characterString = substr($data, $i, 1);
    				$newCode = $characterString;
    				if ($characterString eq "1") {
    					$newCode = "0";
    				}
    				if ($characterString eq "0") {
    					$newCode = "1";
    				}
    				if ($characterString eq "2") {
    					$newCode = "3";
    				}
    				if ($characterString eq "3") {
    					$newCode = "2";
    				}
    				substr($data, $i, 1) = $newCode;
    			}
    			print "$data\n";
    		}
    		else {
    			print $data\n";
    		}
    I hope someone can help me with this.
    Last edited by miller; Feb 28 '07, 04:03 PM. Reason: Code tag
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Do you have an array or a hash?

    Code:
    if (exists $hash[1]) {
    $hash[1] is an array index, not a hash key. Hash keys use curly brackets {}

    Code:
    if (exists $hash{1}) {

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      'B' is a string (most likely) not a number, so use 'eq' instead of '==':

      Code:
      $hash[2] eq 'B'

      Comment

      • Iris83
        New Member
        • Feb 2007
        • 7

        #4
        Thank you for your help. I tried it like you said it is a hash so this is what i used but im not getting any output! I hope you can help me figure out what i should do!

        Code:
        while ($data = <Dataset>) {
        	if (exists $hash{1]} {
        		if ($hash{2} eq 'B') {
        			for ($i = 0; $i < length($data); $i++) {
        				$characterString = substr($data, $i, 1);
        				$newCode = $characterString;
        				if ($characterString eq "1") {
        					$newCode = "0";
        				}
        				if ($characterString eq "0") {
        					$newCode = "1";
        				}
        				if ($characterString eq "2") {
        					$newCode = "3";
        				}
        				if ($characterString eq "3") {
        					$newCode = "2";
        				}
        				substr($data, $i, 1) = $newCode;
        			}
        			print "$data\n";
        		}
        		else {
        			print $data\n";
        		}

        Comment

        • Iris83
          New Member
          • Feb 2007
          • 7

          #5
          Accessing The keys and values in a Hash

          Hi i want to use the keys and values of a hash to compare them with other files. But i can't acces them.

          This is my code i hope somone can help!
          Code:
          my %hash;
          
          while( <Data> ) {
          	@elements = split /,/, $_;
          
          	if( $elements[1] ne '' ) {
          		$hash{ $elements[1] } = $elements[2];
          	}
          }
          
          while ($data = <Dataset>) {
          	if (exists $hash{1}) {
          		if ($hash{2} eq 'B')
          			print $data;
          		else {
          			print $data;
          		}
          	}
          }
          Last edited by miller; Mar 1 '07, 04:44 PM. Reason: Code Formatting

          Comment

          • docsnyder
            New Member
            • Dec 2006
            • 88

            #6
            @Iris83

            I'm not quite sure what you are intending to do.

            Code:
            $hash{ $elements[1] } = $elements[2];
            Do you really mean the second and the third element? Remember, the first array element has index 0.

            You are accessing $hash{1} and $hash{2}. Are 1 and 2 really the hash keys you mean?

            Code:
            if ($hash{2} eq 'B')
            print $data;
            else {print $data;
            }
            Why an if-statement when printing data in any case?

            Please, can you give a short example of what your input files consist of?

            Greetz, Doc

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              check you code for proper syntax:

              {1]

              should be:

              {1}

              Comment

              • Iris83
                New Member
                • Feb 2007
                • 7

                #8
                The syntax is indeed {1} in my script but it doesn't work!

                so any tips would be appreciated!

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  need to see some of the data you are working with.

                  Comment

                  • Iris83
                    New Member
                    • Feb 2007
                    • 7

                    #10
                    I have 2 datasets one containing data like this

                    11,1,B
                    12,2,B
                    13,3,T
                    21,4,T
                    22,5,T

                    From this data i want the 2nd row to be the keys in the hash and the 3th row the values.

                    my 2nd data set looks like this

                    1 1 1 0 0
                    2 1 0 1 1
                    3 1 0 1 1
                    5 1 0 0 0

                    for example if the first key which is 1 exists and the value is B i want to reverse the data from 1 to 0 and 0 in 1. If the key is present and the value is T it should just print the normal data.

                    I was told that you could acces the hash keys like this $hash{1} and the values like this $hash{2} .

                    I hope this clarifies thing and you are able to help me with my problem.

                    Code:
                    my %hash;
                    
                    while( <Data> ) {
                    	@elements = split /,/, $_;
                    
                    	if( $elements[1] ne '' ) {
                    		$hash{ $elements[1] } = $elements[2];
                    	}
                    }
                    
                    while ($data = <Dataset>) {
                    	if (exists $hash{1}) {
                    		if ($hash{2} eq 'B') {
                    
                    for ($i = 0; $i < length($data); $i++) {
                                    $characterString = substr($data, $i, 1);
                                    $newCode = $characterString;
                                    if ($characterString eq "0") {
                                            $newCode = "1";
                                    }
                                    if ($characterString eq "1") {
                                            $newCode = "0";
                                            }
                                   
                                    substr($data, $i, 1) = $newCode;
                    			print $data;
                    		else {
                    			print $data;
                    		}
                    	}
                    }
                    }
                    }

                    Comment

                    • miller
                      Recognized Expert Top Contributor
                      • Oct 2006
                      • 1086

                      #11
                      Originally posted by Iris83
                      Code:
                      substr($data, $i, 1) = $newCode;
                      			print $data;
                      		else {
                      			print $data;
                      		}
                      Your above code doesn't even compile. There is a floating else in the above section.

                      - M

                      Comment

                      • miller
                        Recognized Expert Top Contributor
                        • Oct 2006
                        • 1086

                        #12
                        Iris83,

                        These are the exact same problem. I'm merging the threads to keep us from duplicating effort and comments.

                        - M

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          I think this is what you are trying to do:

                          Code:
                          while( <Data> ) {
                             chomp;
                             my @d = split /,/, $_;
                             if( $d[1] ne '' ) {
                                $hash{ $d[1] } = $d[2];
                             }
                          }
                          
                          while (my $data = <Dataset>) {
                             chomp $data;
                             my @e = split /\s+/, $data;  
                             if ($hash{$e[0]} eq 'B') {
                                foreach my $i (1..$#e) {
                                   $e[$i] =~ tr/10/01/;
                                }
                             }
                             my $d = join(' ',@e);
                             print $d,"\n";
                          }
                          it could probably be done many different ways.

                          Comment

                          • miller
                            Recognized Expert Top Contributor
                            • Oct 2006
                            • 1086

                            #14
                            Originally posted by Iris83
                            I was told that you could acces the hash keys like this $hash{1} and the values like this $hash{2} .
                            That is not correct. You find out the basic information concerning hashes in the flowing perldoc link about perl varaible types:



                            Finally, Kevin's code is likely what you're looking for, but you should do some more reading if you expect to be able to debug it in the future.

                            - M

                            Comment

                            Working...