Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A problem in my another code 2

Status
Not open for further replies.

Everwood

Technical User
Jul 18, 2005
78
US
Hi all, I got an error message when running the code at the end of this post. It said that "Use of uninitialized value in join or string at make_file2.pl line 51."

Although I doubt the error message, I would appreciate if you can find something I really missed.

Thank you!

Alex






#!/usr/bin/perl
use strict;
use warnings;
my @fullset =
qw(GTAC CGAG GTAA CGAA AAAT CGAC AGTG AGTA AGTC AAAA CGAT AAAC GTAT GGTT AGTT AACA GTAG GAAT TTAG ATAT TTAC TTAA GAAC GAAA GAAG GAGA TTAT ACTT GCTC GGAC GCTA ATAA ATAC CCAT ATAG GCTT CCAA ATTT CCAC GTGG TTGC CCAG TAAC TAAG GTCT TAAA TGTC TGTA TCTT TGTG TAAT ACAT TCTG CTCA CTCC TCTC TATG TCTA TGTT AAGG GCCG CGGA AAGC ACGT AAGA CTAA GTGA GTGC GGCT ACGA ACGC AAGT ACGG CGGT GGCC GGCA GGCG TTCT AGGG AGGA AGGC AATC GATT AAAG CCCT GACT ATCA AGGT TTCG CTGG TTCA TTCC CACT GACA GACC GATG GATA GACG AATG TAGT AGAC ATCT GCCC GCCA CCGG TACT TCAG CCGC CAGT CCTT CACG ATCG TAGG TAGA TATT CAGA CCTC CAGC CCGT CCTG CAGG TGGG ACTC TGGC TGGA TCGC TCGA TCGG TGGT GCCT CCCG ACTG GATC TCGT ATCC ACAG CGGC ACAC ATGT ACAA CTAG CTTC TAGC GTCG TCAC CGGG CTAC GGAA AGAA CCTA AGAG GGAG ATTG CTCT ATTC GGAT ATTA AGAT TACG CTTG TGCG TGAT TTTA TTTC TTTG CAAG AACG GCAG GCAA CAAA TGAG TTTT TGAA TGAC GCAT CAAT CTGT GGGG TCAT AATT CGTT CTCG TATA CTAT TATC TCAA AATA CTGA CTGC CGTG CGTC CGTA GTTA GTTC GGGT GTCA GTTG GTCC AACC ACCG ACCA ACCC ACTA GGGC GTTT GGGA CTTT ACCT AACT GAGT GGTA GGTG AGCA GTGT AGCC TTGT AGCG GAGG CCGA GAGC TTGA CTTA AGCT TTGG ATGC ATGA ATGG TGCT GGTC GCGC CAAC GCGA GCGG CCCC TGCC CCCA TGCA CACA GCAC CACC TACA TACC GCGT CATA CATC CATG TCCT CGCT CATT GCTG CGCG TCCG CGCC TCCC CGCA TCCA);

my (@major, @minor,$y, @combinations);
my ($prob, $records) = (55, 100); # Probably of major pair, #num records to generate

# This is a basic ceiling function for $num_major
for ($y=0; $y<=99; $y++) {
my $num_major = ($prob / 100) * $records;
if ($num_major != int($num_major)) { $num_major = int($num_major++); }
my $num_minor = $records - $num_major;

{ my @temp = @fullset;
foreach (0..1) {
my $r_pair = splice(@temp, int(rand($#temp+1)), 1);
$major[$_] = $r_pair;
}}

foreach my $pos (0..$#major) {
my @list;
foreach (1..$num_major) { push(@list, $major[$pos]); }

my $pair = $major[$pos]; # because array indices don't #work in
# a pattern match.
my @minor_list = grep { not /[$pair]/ } @fullset;
foreach (1..$num_minor) {
push(@list,$minor_list[int(rand($#minor_list+1))]);
}
foreach (0..$records-1) {
$combinations[$_][$pos] = splice(@list, int(rand($#list+1)),

}
}



open OUTPUT, "> outfile9_55_$y.txt" or die "Cannot open output file.\n";
foreach (@combinations) {
print OUTPUT join('', @{$_}), "\n";
}
close OUTPUT;
replace();

}
exit();


sub replace {
my (@short, @long,$x,$r,$output_norm);


open (SHORT, "< outfile9_55_$y.txt");
chomp (@short = <SHORT>);
close SHORT;

open (LONG, "< long_sequences.txt");
chomp (@long = <LONG>);
close LONG;

open (OUT_HTML, "> output9_55_$y.html");
print OUT_HTML "<pre>";
open (OUT_NORM, "> output9_55_$y.txt");

for ($x=0; $x<=$#short; $x++) {
$r=2;

$output_norm = substr($long[$x], $r, length $short[$x]);
substr($long[$x], $r, length $short[$x]) = "<font color=red><b>$short[$x]</b></font>";
print OUT_HTML "$long[$x]\n";
$long[$x] =~ s/<[^>]+>//g;
print OUT_NORM ">SeqName$x\n$long[$x]\n";
}
print OUT_NORM ">SeqName$x\n$long[$x]\n";
}

close OUT_HTML;
close OUT_NORM;
}
 
The intersting thing is that another very similar code runs well. I only changed the "fullset" in the above code but it turned out that the error message occured.

The similar code is:

#!/usr/bin/perl
use strict;
use warnings;
my @fullset = qw(AA AC AG AT CA CC CG CT GA GC GG GT TA TC TG TT);
my (@major, @minor,$y, @combinations);
my ($prob, $records) = (91, 100); # Probably of major pair, num records to generate

# This is a basic ceiling function for $num_major
for ($y=0; $y<=99; $y++) {
my $num_major = ($prob / 100) * $records;
if ($num_major != int($num_major)) { $num_major = int($num_major++); }
my $num_minor = $records - $num_major;

{ my @temp = @fullset;
foreach (0..3) {
my $r_pair = splice(@temp, int(rand($#temp+1)), 1);
$major[$_] = $r_pair;
}}

foreach my $pos (0..$#major) {
my @list;
foreach (1..$num_major) { push(@list, $major[$pos]); }

my $pair = $major[$pos]; # because array indices don't work in
# a pattern match.
my @minor_list = grep { not /[$pair]/ } @fullset;
foreach (1..$num_minor) {
push(@list,$minor_list[int(rand($#minor_list+1))]);
}
foreach (0..$records-1) {
$combinations[$_][$pos] = splice(@list, int(rand($#list+1)),
1);
}
}



open OUTPUT, "> outfile8_91_$y.txt" or die "Cannot open output file.\n";
foreach (@combinations) {
print OUTPUT join('', @{$_}), "\n";
}
close OUTPUT;
replace();

close OUTPUT;
replace();

}
exit();


sub replace {
my (@short, @long,$x,$r,$output_norm);


open (SHORT, "< outfile8_91_$y.txt");
chomp (@short = <SHORT>);
close SHORT;

open (LONG, "< long_sequences.txt");
chomp (@long = <LONG>);
close LONG;

open (OUT_HTML, "> output8_91_$y.html");
print OUT_HTML "<pre>";
open (OUT_NORM, "> output8_91_$y.txt");

for ($x=0; $x<=$#short; $x++) {
$r=2;

$output_norm = substr($long[$x], $r, length $short[$x]);
substr($long[$x], $r, length $short[$x]) = "<font color=red><b>$short[$x]</b></font>";
print OUT_HTML "$long[$x]\n";
$long[$x] =~ s/<[^>]+>//g;
print OUT_NORM ">SeqName$x\n$long[$x]\n";
}

close OUT_HTML;
close OUT_NORM;
}

 
Give this a shot:
Code:
my (@major, @minor,$y, @combinations);
my ($prob, $records) = (55, 100); # Probably of major pair, #num records to generate

for ($y=0; $y<=99; $y++) {
    # This is a basic ceiling function for $num_major
    my $num_major = ($prob / 100) * $records;
    if ($num_major != int($num_major)) { $num_major = int($num_major++); }
    my $num_minor = $records - $num_major;

    { my @temp = @fullset;
    foreach (0..1) {
        my $r_pair = splice(@temp, int(rand($#temp+1)), 1);
        $major[$_] = $r_pair;
    }}

    foreach my $pos (0..$#major) {
        my @list;
        foreach (1..$num_major) { push(@list, $major[$pos]); }
        my @minor_list = grep { $_ ne $major[$pos] } @fullset;        
        foreach (1..$num_minor) {
            push(@list,$minor_list[int(rand($#minor_list+1))]);
        }
        foreach (0..$records-1) {
           $combinations[$_][$pos] = splice(@list, int(rand($#list+1)),1);
        }
    }

    open OUTPUT, "> ./output/outfile9_55_$y.txt" or die "Cannot open output file.\n";
    foreach (@combinations) {
        print OUTPUT join('', @{$_}), "\n";
    }
    close OUTPUT;
    replace();

}

sub replace {
    my (@short, @long,$x,$r,$output_norm);           

    open (SHORT, "< ./output/outfile9_55_$y.txt");
    chomp (@short = <SHORT>);             
    close SHORT;

    open (LONG, "< long_sequences.txt");
    chomp (@long = <LONG>);
    close LONG;              

    open (OUT_HTML, "> ./output/output9_55_$y.html");
    print OUT_HTML "<pre>";        
    open (OUT_NORM, "> ./output/output9_55_$y.txt");

    for ($x=0; $x<=$#short; $x++) {
        $r=2;
       
        $output_norm = substr($long[$x], $r, length $short[$x]);
        substr($long[$x], $r, length $short[$x]) = "<font color=red><b>$short[$x]</b></font>";
        print OUT_HTML "$long[$x]\n";
        $long[$x] =~ s/<[^>]+>//g;
        print OUT_NORM ">SeqName$x\n$long[$x]\n";
    }
    
    close OUT_HTML;
    close OUT_NORM;
}
Oh, and I might suggest indenting a little more - makes it easier to check for syntax problems.
 
Thank rharsh for help! That code does work!

But what is the problem in my first code?

Thanks again!

Alex
 
I think these lines were the biggest problem:

Code:
foreach (0..$records-1) {
         $combinations[$_][$pos] = splice(@list, int(rand($#list+1)),

 }
Which should read something more like:
Code:
foreach (0..$records-1) {
    $combinations[$_][$pos] = splice(@list, int(rand($#list+1))[red][b],1);[/b][/red]
}
 
Hi rharsh! I tried that code. Although the message "Use of uninitialized value in join or string at make_file2.pl line 51"
still exsited, it seemed that that code can generate the right results.

Thank you again!
 
Which is line 51? The wrapping makes it hard for me to tell.

f

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
hi fish,

I doubt line 51 is within

open OUTPUT, "> outfile9_55_$y.txt" or die "Cannot open output file.\n";
foreach (@combinations) {
print OUTPUT join('', @{$_}), "\n";
}

thanks
 
You can get this error from join. I've simulated it using this
Code:
#!/usr/bin/perl
use strict;
use warnings;

my @combinations = (
        [ 'apple', 'bee', 'cat' ],
        [ 'janet', 'john', 'burt' ],
        [ 'hi', 'lo', [red]undef[/red] ],
);

foreach (@combinations) {
    print join('', @{$_}), "\n"; # this is your code fragment
}
which produces this
Code:
applebeecat
janetjohnburt
[red]Use of uninitialized value in join or string at t line 12.[/red]
hilo
so it looks like you do have a genuinely uninitialised variable hinding somewhere within @combinations. It's probably worth chasing as this could indicate a problem elsewhere.

You could pinpoint the erroneous records with something like
Code:
foreach my $c (@combinations) {
    print OUTPUT join('', map {
            defined $_ ? $_ : 'UNDEF'
        } @$c ), "\n";
}
which will flag them in-line. If you want to check the lot,
Code:
foreach my $c ( @combinations ) {
   if ( grep {! defined $_} @$c ) {
       warn "Got an undef\n";
   }
}
does the biz.

Yours,

fish




&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top