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

Hi all,

I'm really new in this field, so my problem may be so basic for you, please accept my apologize in advance. I faced with the following error during running a perl script

.
Use of uninitialized value in concatenation (.) or string at sequence +_replacement.pl line 56, <TWO> line 48001. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48007. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48023. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48033. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48055. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48059. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48061. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48079. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48087. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48101. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48103. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48107. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48131. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48135. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48189. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48219. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48221. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48225. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48235. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48237. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48243. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48247. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48253. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48299. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48351. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48371. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48387. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48391. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48395. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48403. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48423. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48425. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48447. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48451. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48453. Use of uninitialized value in concatenation (.) or string at sequence_ +replacement.pl line 56, <TWO> line 48455.
.

The error was really large and sounds that it's for almost line, I put just small part of it. I would highly appreciate if you could please help me to solve the problem in a simple language

.

Here is the code:

#!/usr/bin/perl use strict; use warnings; use Getopt::Long; #set variables my ($optHelp, $optFile1, $optFile2, $optList); #get program options GetOptions ('h' => \$optHelp, 'a=s' => \$optFile1, 'b=s' => \$optFile2 +, 'l=s' => \$optList); # there are better ways to do this but I like this strategy :) if($optHelp || !$optFile1 || !$optFile2 || !$optList ){ print "Usege:\n\n"; print "./program -a file1 -b file2 -l list<.tsv> \n\n"; print "Note: sequences from file2 are replaced with those in file1 a +ccording to the list -l\n\n"; exit(0); } #allocate memory (well, more of set variables) my %hash1 =(); my %list = (); #open files open (ONE, "<", $optFile1) or die "$!"; open (TWO, "<", $optFile2) or die "$!"; open (LIST, "<", $optList) or die "$!"; #load list which should look like: #file1_id <tab> file2_id while(<LIST>){ chomp; /^(.*?)\t(.*)/; $list{$1} = $2; #e } close LIST; #read file one into memory my $head; while(<ONE>){ chomp; if(/>(.*)/){$head = $1; next;} #e $hash1{$head} .= $_; } close ONE; #replace fasta record from file to with the one in file one if id is s +et in list file my $x = 0; while(<TWO>){ chomp; ## You can play with this if one line fasfa sequence is not what you + can use if(/>(.*)/){if(defined $list{$1}){print $_. "\n" . $hash1{$list{$1}} + . "\n"; $x = 1}else{$x = 0}} #e print $_ . "\n" if $x == 0; #e } close TWO;

Replies are listed 'Best First'.
Re: Getting (famous) error during running perl script
by derby (Abbot) on Jul 04, 2015 at 10:54 UTC

    You concating an uninitialized variable on line 56 ... that's pretty plain. :-)

    Maybe an example would help:

    #!/usr/bin/env perl use strict; use warnings; my $foo = "foo"; my $bar; # $bar is uninitialized -- it has no value. my $foobar = $foo . $bar;
    This could be harmless or devastating ... it really depends on what your code is doing and wether or not you expect/need the variable to have a value or not.

    -derby
Re: Getting (famous) error during running perl script
by 1nickt (Canon) on Jul 04, 2015 at 11:16 UTC

    Hi, welcome to the monastery.

    Somewhere in your program you have a concatenation, which means to join two things together. It may look like this:

    print 'value = ' . $value;

    or

    print "value = $value";

    . . . or similar.

    If $value is "uninitialized" Perl will spit out this error (but only if you have warnings enabled -- you do; good job, otherwise your script would be failing silently!)

    So you should always check to see that what you are attempting to concatenate, actually exists:

    if ($value) { print "value = " . $value; } else { print 'Yikes! No value for $value.'; }
    Remember: Ne dederis in spiritu molere illegitimi!
Re: Getting (famous) error during running perl script
by Random_Walk (Prior) on Jul 04, 2015 at 10:56 UTC

    may we see the code please? This error tells us a variable has no value assigned to it. A wild guess, when you are printing it in some output. Without the code, it's not possible to help much though

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

      Of course, I updated my question with the code. This code is for replacing sequences of interest from one fasta file to another. Another thing that may be useful to know is that I can see sequence of interest before running the script, but after the running the script, the output file just showed the header of sequence not sequence itself, as if there isn't such a sequence at all. Please let me know if you need further information.Thanks for your help

      .
        Are you sure that $hash1{$list{$1} } is defined?

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

        In Re: Getting (famous) error during running perl script, BillKSmith suggests blank lines in your input file -- and there's a decent chance that's it.

        However, dissecting the line with the issue into digestible form:

        if (/>(.*)/) { if (defined $list{$1}) { print $_. "\n" . $hash1{$list{$1}} . "\n"; $x = 1 } else { $x = 0 } } #e print $_ . "\n" if $x == 0; #e

        I can't help but wonder: You check to see if $list{$1}is defined, but make no attempt to ascertain if $hash1{$list{$1}}is defined -- but that's what you're using there.

        Could it be that you need to change that line to:

        if (defined $hash1{$list{$1}})

        If it might help you a bit?

Re: Getting (famous) error during running perl script
by GrandFather (Saint) on Jul 04, 2015 at 11:09 UTC
Re: Getting (famous) error during running perl script
by toolic (Bishop) on Jul 04, 2015 at 13:37 UTC
Re: Getting (famous) error during running perl script
by kcott (Archbishop) on Jul 05, 2015 at 08:27 UTC

    G'day Kati,

    Welcome to the Monastery.

    "Use of uninitialized value in concatenation (.) or string at sequence_replacement.pl line 56, <TWO> line ..."

    Here's line 56:

    if(/>(.*)/){if(defined $list{$1}){print $_. "\n" . $hash1{$list{$1}} . + "\n"; $x = 1}else{$x = 0}}

    Here's the only concatenation occurring on that line:

    print $_. "\n" . $hash1{$list{$1}} . "\n";
    • $_ is the line you've read (with line-ending removed): clearly initialized.
    • "\n" (2 instances) is a string literal: clearly initialized.
    • $list{$1} has been checked with defined: clearly initialized.
    • $hash1{$list{$1}} is all that's left that could possibly be uninitialized.

    That's the problem identified. At this point, I'll leave the solution for you to determine (a good learning exercise). If you really can't fix this yourself, feel free to ask another question but ensure you tell us what you've tried. Adding print statements to see the value of $list{$1} on each iteration would probably be a good start; however, see my comments on laying out your code (below) before doing so.

    "I'm really new in this field, ..."

    Here's a few tips and pointers:

    Writing code like

    if(/>(.*)/){if(defined $list{$1}){print $_. "\n" . $hash1{$list{$1}} . + "\n"; $x = 1}else{$x = 0}}

    is a recipe for disaster. It's hard to read; hard to maintain; hard to troubleshoot. Take a look at "perlstyle: The Perl Style Guide"; choose a style that you're happy with and use it consistently. Here's how I would have laid out that code. Note: it's exactly the same code you wrote — bugs and all!

    if (/>(.*)/) { if (defined $list{$1}) { print $_ . "\n" . $hash1{$list{$1}} . "\n"; $x = 1; } else { $x = 0; } }

    That's now much easier to read: you can see the logic at a glance. Accordingly, it's also easier to maintain and troubleshoot.

    The print function takes a list. There's no reason to perform three separate concatenation operations to generate an argument for print — just provide a list:

    print $_, "\n", $hash1{$list{$1}}, "\n";

    You'd probably benefit from reading "perlintro: Perl Introduction for Beginners". While it's very basic in itself, each section contains links to more detailed information and advanced topics.

    Perl has lots of documentation, all of which you can find linked from the online "perl manpage". Take some time seeing what's there. The first three sections (Overview, Tutorials and Reference Manual) probably contain pretty much everything you might need while you're learning.

    A word of caution: "perlfunc: Perl Built-in Functions" documents all functions in a single web page and is enormous. You'll rarely, if ever, need this. Instead, use the "Perl Functions A-Z" index. [There's a link to that on the online "perl manpage": left sidebar; under "Reference"; labelled "Functions"].

    "I faced with the following error ..."

    Actually, that's a warning, not an error. You used the strict and warnings pragmata: this is very good — keep doing it. Use of the latter is why you got all those warnings. By default, those types of messages are restricted to just one line; however, you can find a much more verbose version (of both warning and error messages) from "perldiag: Perl Diagnostic Messages". While you're learning, you can get the verbose form automatically with the diagnostics pragma. This can cause a lot of clutter in your output and, eventually, you won't want it. Also, don't use this in any production code: it's only intended as a developer tool.

    Finally, I did see your original post with just the output and a variety of requests to see the code. Now that you've supplied the code, tracking down the source of the problem was easy: had you supplied the code in the first place, you could have had an answer hours ago. I'm not trying to tell you off — this was your first post and you didn't know. For future reference, following the guidelines in "How do I post a question effectively?" will get you answers instead of requests for more information.

    -- Ken

Re: Getting (famous) error during running perl script
by BillKSmith (Monsignor) on Jul 04, 2015 at 18:26 UTC

    A common cause of this error is blank lines at the end of your input file. In that case, the program usually runs correctly except for the warnings.

    Bill