Search code examples
perlhashnewlinesplice

Splicing an array generates an unwanted new line


What I want to do is to compare the keys of two hashes, and if a key from the hash1 exists in hash2, then insert the corresponding value from hash1 in the value of the hash2, and retrieve the modified value.
I'm trying to do so by splitting the value from hash2 in an array (said value is a tab delimited string) and splicing the array.
I expect the output to look like this :

foo    bar    baz    qux

but it generates an unwanted newline right after the splice :

foo    bar    modified
qux

How can I avoid this newline?

Here is the corresponding code :

foreach my $hash1key ( keys %hash1 ) {
    if ( exists $hash2{($hash1key)} ) {
        my $line = $hash2{$hash1key};
        my @results = split /\t/, $line;        
        splice @results, 2, 1, $hash1{$hash1key};
        for ( @results ){ 
            print OUT $_."\t";
        }
    }
}

Solution

  • The values in %hash1 are clearly terminated by newlines

    The code you show works fine; you need to debug the population of %hash1