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

I am beginner in perl. I am reading word pairs from text file in [['treatments', 'sunlight'], ['treatments', 'help'],...] format. I am reading it using perl .But after doing lot of pre-processing so that $umls->getConceptList($t1); get string .But its still showing same error.Dont know what is wrong with syntax or is there something else i m missing...

#**************problem in below line**** my $cu1 = $umls->getConceptList($t1);

(Instead of passing $t1="$w[0]",$t2="$w[1]" if I pass string like $t1="Medicine",$t2= "head" directly then it works perfectly fine. Dont know whats problem with assignment of $t1,$t2. Please help me to resolve it.....

******CODE************** for(my $i=0;$i<$n_size;$i++){ my @w= split(/,/ , $new[$i]); $w[0]=~s/\'//; #pre-process to remove ' from text $w[0]=~s/\'//; as @w='word1' 'word2' $w[1]=~s/\'//; $w[1]=~s/\'//; my $t1 = "$w[0]"; #$w[0]=word1 without single quotes my $t2 = "$w[1]"; #$w[1]=word2 print $t1,$t2,"__________"; #working my $cu1 = $umls->getConceptList($t1); #calling function from umls +::similarity #ERRRO 1 as mentioned my $cui1 =pop @{$cu1}; my $cu2 = $umls->getConceptList($t2);***error my $cui2 = pop @{$cu2}; print $cui1; # ****ERROR2 unintialized $cui1 my $lvalue = $lch->getRelatedness($cui1, $cui2); my $value = $path->getRelatedness($cui1, $cui2);

Replies are listed 'Best First'.
Re: Use of uninitialized value $cui1 in print
by huck (Prior) on Mar 03, 2017 at 05:20 UTC

    You have given us very little to work on. you need to include much more information. Fix the above code to look like this

    ######################################## print 'size new:'.scalar(@new)."\n"; print 'n_size :'.$n_size."\n"; print 'new :'.join('|',@new)."\n"; ######################################## for(my $i=0;$i<$n_size;$i++){ my @w= split(/,/ , $new[$i]); ######################################## print 'i :'.$i."\n"; print 'size w :'.scalar(@w)."\n"; print 'w :'.join('|',@w)."\n"; ######################################## $w[0]=~s/\'//; #pre-process to remove ' from text $w[0]=~s/\'//; as @w='word1' 'word2' $w[1]=~s/\'//; $w[1]=~s/\'//; ######################################## print 'w[0] :'.$w[0]."\n"; print 'w[1] :'.$w[1]."\n"; print 'size w :'.scalar(@w)."\n"; ######################################## my $t1 = "$w[0]"; #$w[0]=word1 without single quotes my $t2 = "$w[1]"; #$w[1]=word2 ######################################## print 't1 :'.$t1."\n"; print 't2 :'.$t2."\n"; ######################################## # print $t1,$t2,"__________"; #working my $cu1 = $umls->getConceptList($t1); #calling function from umls +::similarity #ERRRO 1 as mentioned ######################################## print 'raw cu1:'.$cu1."\n"; print 'size cu1:'.scalar(@{$cu1})."\n"; print 'cu1 :'.join('|',@{$cu1})."\n"; ######################################## my $cui1 =pop @{$cu1}; ######################################## print 'new cu1:'.$cu1."\n"; ######################################## my $cu2 = $umls->getConceptList($t2);***error ######################################## print 'raw cu2:'.$cu2."\n"; print 'size cu2:'.scalar(@{$cu2})."\n"; print 'cu2 :'.join('|',@{$cu2})."\n"; ######################################## my $cui2 = pop @{$cu2}; ######################################## print 'new cu2:'.$cu2."\n"; ######################################## # print $cui1; # ****ERROR2 unintialized $cui1 my $lvalue = $lch->getRelatedness($cui1, $cui2); my $value = $path->getRelatedness($cui1, $cui2);
    Then run it and post the output. Then someone may be able to help you.

      ######################################## print 'raw cu1:'.$cu1."\n"; print 'size cu1:'.scalar(@{$cu1})."\n"; print 'cu1 :'.join('|',@{$cu1})."\n"; ########################################

      Raksha Jalan: Rather than using print statements in this way, I would recommend the use of something like Data::Dumper (which is core). It will IMHO be much more informative about what the variable (e.g., $cu1) really is, and will avoid the generation of a bunch more warnings if the variable is undefined.

      c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dumper; ;; my $x; print Dumper $x; ;; $x = []; print Dumper $x; ;; $x = [ 'hi', 'there' ]; print Dumper $x; " $VAR1 = undef; $VAR1 = []; $VAR1 = [ 'hi', 'there' ];
      BTW: I prefer the Data::Dump module, but it isn't core.


      Give a man a fish:  <%-{-{-{-<

      Thanks for reply. surprisedly value of $cui. raw $cui1 are not as expected AS it uniq ids not hash.

      here is the o/p after running above: i :405 size w :2 w : 'severe'| 'sunburn' w[0] : severe w[1] : sunburn size w :2 t1 : severe t2 : sunburn raw cu1:ARRAY(0x1dcf698) size cu1:0 cu1 : new cu1:ARRAY(0x1dcf698) raw cu2:ARRAY(0x1dcf6e0) size cu2:0 cu2 : new cu2:ARRAY(0x1dcf6e0)

        The strings in the  @w array seem to have whitespace before the first single-quote. Any leading whitespace will remain after the single-quotes are deleted from the string. Is this whitespace expected? Use of
            print Dumper \@w;
        would make any leading (or trailing) whitespace obvious.

        BTW: All single-quotes could be deleted from a string in a single
            $w[0]=~s/'//g;
        statement. Please see perlre, perlretut and perlrequick, and Regexp Quote-Like Operators in perlop for s///g. All single-quotes could be deleted from every string in an array with
            s/'//g for @w;
        (but this still leaves any whitespace untouched).


        Give a man a fish:  <%-{-{-{-<

        Ah, the joys of debuging by proxy. it seems i missed something in the debug statements and print 'new  cu1:'.$cu1."\n"; should have been print 'cui1    :'.$cui1."\n"; and print 'new  cu2:'.$cu2."\n"; should have been print 'cui2    :'.$cui2."\n";

        but this simple exercise has located the problem anyway, in that on i=405 the cu1/cu2 arrays are empty. that means the pops return undef and hence your error. is there anything different about 405? as noted by AnomalousMonk the strings in @w have leading spaces, is that true only on 405? Maybe that is the problem and needs to be fixed somehow, in @new or by regexp maybe.

        to workaround this for now you could change to

        unless (scalar(@{$cu1})) {print "cu1 is empty\n"; next} my $cui1 =pop @{$cu1}; ######################################## print 'cui1 :'.$cui1."\n"; ######################################## my $cu2 = $umls->getConceptList($t2);***error ######################################## print 'raw cu2:'.$cu2."\n"; print 'size cu2:'.scalar(@{$cu2})."\n"; print 'cu2 :'.join('|',@{$cu2})."\n"; ######################################## unless (scalar(@{$cu2})) {print "cu2 is empty\n"; next} my $cui2 = pop @{$cu2}; ######################################## print 'cui2 :'.$cui2."\n"; ########################################
        Notice how it aborts the rest of the loop if either of the arrays are empty. Notice it also includes the updated debug statements. Notice you may want to expand the output printed if they are empty

Re: Use of uninitialized value $cui1 in print
by Raksha Jalan (Initiate) on Mar 02, 2017 at 16:59 UTC
    Error1 is: "Use of uninitialized value $concept1 in string eq at UMLS/Similarity/lch.pm
      Error1 is: ...

      Ok, so what's the code that produces the error? I don't see any  eq operator ("string eq") in the OPed code, nor a  $concept1 scalar. (Update: A small point: a "warning" (see warnings, which you are wise to use, as you seem to be doing) is not an error, but merits your attention nonetheless.)

      Update: Rats. Many small changes to added update sentence in preceding paragraph just to get it to make sense!


      Give a man a fish:  <%-{-{-{-<

        # UMLS::Similarity::lch.pm # # Module implementing the semantic relatedness measure described # by Leacock and Chodorow (1998). # <code> sub getRelatedness { my $self = shift; return undef if(!defined $self || !ref $self); my $concept1 = shift; my $concept2 = shift; # get the interfaceg my $interface = $self->{'interface'}; # find the length of the shortest path # if concept 1 and 2 are the same just return 1 my $length = 0; if($concept1 eq $concept2) { $length = 1; } else { $length = $interface->findShortestPathLength($concept1, $co +ncept2); } # get the depth of the taxonomy my $depth = $interface->depth(); # if the length of hte path is less than zero return -1 if($length < 0) { return -1; } # calculate lch my $score = -1 * log ($length / (2 * $depth)); return $score; } =

        this subroutine in code is by default present in umls::similarity package. Where our $t1 is passes as $concept1 and $t2 is passed as $concept 2.So I dont think there is anything wrong in this code.What am I thinking is maybe $t1 is not able to get value as string the way it should be.Well I am beginner so I dont have much knowledge so plz help me