HI

I am trying to print a certain number of sequences from a file.
So if the sequence is like
LKSAJDLSAJDLSAJDLKSJD.
then I want to write from 10th character to 20th character.
please help. Here is my code

#!/usr/bin/perl -w
use strict;

use warnings;

my ($fileName, $firstSequence, $lastSequence) = @ARGV;

open IN, "< $fileName" || die "Can't open $fileName";

while(<IN>) {
  chomp;
  my @line = split(/\t/, $_); 
#  print $line[0];
  print $line[$firstSequence..$lastSequence];
#  print $line[$lastsequence]; 
}

close(IN);
exit;

This only checks in a line, but what if the sequence is multiline

#!/usr/bin/perl -w

use strict;
use warnings;

my ($fileName, $firstSequence, $lastSequence) = @ARGV;

open (IN, "<$fileName") || die "Can't open $fileName";

while(<IN>) { 
 chomp;
  my @line = split(//, $_);  

  print @line[$firstSequence..$lastSequence];

 print "\n";
}

close(IN);
exit;

Hi, im just modifying your code, use substr() method to get substring from a string.

Syntax: substr EXPR,OFFSET,LENGTH

#!/usr/bin/perl -w
use strict;

use warnings;

my ($fileName, $firstSequence, $lastSequence) = @ARGV;

open (IN, "< $fileName") || die "Can't open $fileName";

while(<IN>) {
  chomp;
  print "\n",substr($_, $firstSequence, $lastSequence);
}

close(IN);
exit;

print $line[$firstSequence..$lastSequence]; this line will get you element at index 10 till element at index 20 from @line array, not sub string of an element.

katharnakh.

This only checks in a line, but what if the sequence is multiline

#!/usr/bin/perl -w

use strict;
use warnings;

my ($fileName, $firstSequence, $lastSequence) = @ARGV;

open (IN, "<$fileName") || die "Can't open $fileName";

while(<IN>) { 
 chomp;
  my @line = split(//, $_);  

  print @line[$firstSequence..$lastSequence];

 print "\n";
}

close(IN);
exit;

While loop in your code, will loop through lines of input file. What's question, explain clearly.

katharnakh.

this code only reads line by line
so if there is a single sequene like (distributed over 2 lines)

LASJDLKAJSDKJSADJLKSADLJLSAKJDK
AKSJDKLJSAKDJJSAD

so my code only prints character in the first and 2nd line seperately.
I want to add these two lines to a single line and print out the character sequence of that line.

this code only reads line by line
so if there is a single sequene like (distributed over 2 lines)

LASJDLKAJSDKJSADJLKSADLJLSAKJDK
AKSJDKLJSAKDJJSAD

so my code only prints character in the first and 2nd line seperately.
I want to add these two lines to a single line and print out the character sequence of that line.

Then you must go with concatenation.

...
my $line = '';
while my $l(<IN>){
    $line .= $l; # entire file in a line
}
...

its showing errors when I try to run that code

#!/usr/bin/perl -w

open (IN, "<$fileName") || die "Cant open $fileName";

my $line = '';

while my $1(<IN>){
chomp;
$line .= $1;
}

print $line;

exit;
./test.pl abc.txt
syntax error at ./test.pl line 7, near "while my "
syntax error at ./test.pl line 10, near "}"
Execution of ./test.pl aborted due to compilation errors.

ok, so the file contains data as

output.txt

>name1
lsdflsjdfljdsfsjdlfjlksdjfsdf
ksjdfjdsf
ksjdflkjsflksdf
>name2
ksajdasjd
aksjdalksjd
kajlkdjasd

I am trying to print from character #x to character #y for each name
so that it becomes

>name1
char#x....char#y
>name2
char#x....char#y

Here is my code

#!/usr/bin/perl -w

use strict;

use warnings;

my ($fileName, $firstSequence, $lastSequence) = @ARGV;

open (IN, "<$fileName") || die "Can't open $fileName";

my @line=();
my @line1=();

OUTER: while(<IN>) {
@line=(); 
 chomp;
   if (/>/) { 
     
       print "$_\n";
       next;
       @line1=();
    }
 @line = split(//, $_);

 push(@line1,@line);

}
print @line1;
print @line1[$firstSequence-1..$lastSequence-1];

close(IN);
exit;

its showing errors when I try to run that code

#!/usr/bin/perl -w

open (IN, "<$fileName") || die "Cant open $fileName";

my $line = '';

while my $1(<IN>){
chomp;
$line .= $1;
}

print $line;

exit;
./test.pl abc.txt
syntax error at ./test.pl line 7, near "while my "
syntax error at ./test.pl line 10, near "}"
Execution of ./test.pl aborted due to compilation errors.

sorry for that incomplete code. it should be like,

open(F, 'f1.txt') or die "$!";
my $line;
while (my $l = <F>){
		chomp($l); # get rid of newline at the end of line
		$line .= $l;
}
print $line;
close(F);

@Kevin, thanks for that point :)

katharnakh.

thanks
this is what my current code looks like.
But I still cant make it stop once it gets a (">") and then output the result
and then again start concatenating till it finds the next ('>') or stop if end of file is reached

#!/usr/bin/perl -w

use strict;
use warnings;

	my ($fileName, $firstSequence, $lastSequence) = @ARGV;

	open (IN, "<$fileName") || die "Can't open $fileName";

     
	my $line;
        my @line=();
OUTER2:	while (my $l = <IN>){
        if($l eq ""){last EXIT2;}
	if ($l =~ />/) {  
             print "$l\n";
	       next;
	    }

	chomp($l);
	$line .= $l;
	}
	@line = split(//, $line);  
	print @line[$firstSequence-1..$lastSequence-1];
        last OUTER2;
EXIT2:	close(IN);

Hello Mank,
I think you have not understood what last LABEL in perl does. Why are you using last in your code? I dont understand this in your code

...
last OUTER2; # why?

Probably you have to read documentation before you start doing it again. Type this in your command prompt and go through it, perldoc -f last .

katharnakh.

yes
I dont want to use that label there, its just going to re-read my file,
I dont want to do that, I just want to make sure @line has all the sequence after line containng ">" to line containing ">"

so its like

>label1
data1.....
data2......
data3......
>label2
data1....
data2...
data3...

so I want
>label1
data1.....data2.....data3.....
>label2
data1.....data2.....data3.....

then @line[$seq1..$seq2] wiill contain all data from data2 to data3 for each sequence

then you got to do within your while loop itself. Because, within the while loop itself you will read entire file.
So while reading the file itself you will you print whatever you want. If you want to re read the file, then you must either seek() the start of the file or close the file and reopen and read the file.

Hope you understand.
katharnakh.

This article has been dead for over six months. Start a new discussion instead.