in reply to How to perform single operation to each line in file

I did some changes in to your code & was able to achieve what you were looking.Here is the code

#!/usr/bin/perl use strict; use warnings; use strict; use warnings; use diagnostics; use Modern::Perl; open FH, '<', 'Perl_file.txt' or die "Could not open file= $!"; while( <FH>) { chomp($_); my $line =$_; $line =~ s/\s+//gs; my $char_a = substr($line, 0, 2); my $char_b = substr($line,2,2); print $char_a; print $char_b; }

Data i inserted in to Perl_file.txt was

Raam larry marry sephali

OUTPUT:

Raamlarrmarrseph

Replies are listed 'Best First'.
Re^2: How to perform single operation to each line in file
by Laurent_R (Canon) on Jan 21, 2014 at 18:10 UTC
    You rightly removed the line returns with chomp, you need to add them back when you print:
    print $char_a, "\n"; print $char_b, "\n";
    Additional comments: why do you have use strict and use warnings twice (actually three times since you are also using modern Perl)? Why did you change from a lexical filehandle ($FH) to a bare word filehandler (FH)?