954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Uninitialized value - need to set value to "0" if undefined

Hi,

I wrote this script to help me cleanup some data. The value in the 8th column is sometimes empty. When it is empty, I want it to print a zero as in the next part of the script it will be adding a number to whatever is in that column. The script doesn't trigger any errors other than: Use of uninitialized value $start in concatenation (.) or string at lineXXX. Thanks in advance!

Sample data:

23 "ugguuucacgugauacguauu" 22 "ccaugcacuacgcaccaugcacugugcacccuuguaaagugcacuuccauuaugcauagugcacuuugguguggugcgugu" 18 1 "is_MIRNA" 51
23 "ugcacugugcacccuuguaaa" 23 "agaggcauaacgagaggaaccuucgcacacaaacggcacuuaaguugucgggauaacuucacaguuauuucggcgaucauguguuuggcgccguugccggggacaucucucguugcuucg" 18 0 "is_MIRNA"

#!/usr/bin/perl
use warnings;
use strict;


open (FILE1, $ARGV[0]) or die "Can't open file1\n";


my %table;

my ($miRID,$miR,$mirID,$mir,$scaffold,$structure,$is_mir,$start);

foreach (<FILE1>){
	my $line = $_;
	chomp;
	($miRID,$miR,$mirID,$mir,$scaffold,$structure,$is_mir,$start,undef) = split (/\s+/, $line);
	chomp;{
	$table{$mirID} = $start;	
	if (undef $start)		 {
	print "$scaffold\t0\n";
	}
	else {
	print "$mirID\t$scaffold\t$start\n";
		}
		}
	}
close FILE1;
exit;
bio-grad
Junior Poster in Training
50 posts since Oct 2010
Reputation Points: 10
Solved Threads: 1
 

try

my $line = $_;
my $start='a';


This should assign a value to $start which will then be overridden if it's not undef in $line.

then

if ($start=='a' || undef $start) {
print "$scaffold\t0\n";
}
Dandello
Posting Whiz in Training
263 posts since May 2010
Reputation Points: 28
Solved Threads: 23
 

thanks!

bio-grad
Junior Poster in Training
50 posts since Oct 2010
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: