nathalie wrote:
Hi
Hello,
I have this format of file: (see attached example) 1 3206102-3207048 3411782-3411981 3660632-3661428 2 4481796-4482748 4483180-4483486 and I would like to change it to this 1 3206102-3207048 1 3411782-3411981 1 3660632-3661428 2 4481796-4482748 2 4483180-4483486 ..... I have tried with this script to create an array for each line, and to print the first element (1 or 2) with the rest of the line but the output don't seem to be right, could you please advise? #!/software/bin/perl use warnings; use strict; my $file="example.txt"; my $in; open( $in , '<' , $file ) or die( $! ); #open( $out, ">>txtout"); while (<$in>){ next if /^#/; my @lines=split(/\t/); chomp; for (@lines) { print $lines[0],"\t",$_,"\n"; };
You want something like this: #!/software/bin/perl use warnings; use strict; my $file = "example.txt"; open my $in, '<', $file or die "Cannot open '$file' because: $!"; while ( <$in> ) { next if /^#/; chomp; my ( $key, @fields ) = split /\t/; print map "$key\t$_\n", @fields; } __END__ John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/