•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 391,826 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,699 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser:
Views: 1405 | Replies: 14
![]() |
•
•
Join Date: Oct 2007
Posts: 41
Reputation:
Rep Power: 1
Solved Threads: 0
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
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;•
•
Join Date: Oct 2007
Posts: 41
Reputation:
Rep Power: 1
Solved Threads: 0
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
Syntax:
katharnakh.
substr() method to get substring from a string. Syntax:
substr EXPR,OFFSET,LENGTH Perl Syntax (Toggle Plain Text)
#!/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.
Last edited by katharnakh : Mar 25th, 2008 at 6:34 am.
challenge the limits
•
•
•
•
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.
challenge the limits
•
•
Join Date: Oct 2007
Posts: 41
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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.
Perl Syntax (Toggle Plain Text)
... my $line = ''; while my $l(<IN>){ $line .= $l; # entire file in a line } ...
challenge the limits
•
•
Join Date: Oct 2007
Posts: 41
Reputation:
Rep Power: 1
Solved Threads: 0
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.
•
•
Join Date: Oct 2007
Posts: 41
Reputation:
Rep Power: 1
Solved Threads: 0
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
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,
Perl Syntax (Toggle Plain Text)
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.
Last edited by katharnakh : Mar 26th, 2008 at 3:53 am.
challenge the limits
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Perl Marketplace
Similar Threads
- Noob needs help i/o file data (Python)
- Count number of occurrences of a character search it in a file (C++)
- File Processing C++ (C++)
- problem with svchost.exe (Windows NT / 2000 / XP / 2003)
- reading a file into code (Java)
- Text File Input and manipulation (C++)
- I need special stuff in my project like system("cls") (C++)
Other Threads in the Perl Forum
- Previous Thread: merging two files and removing duplicates
- Next Thread: blast crashed


Linear Mode