User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2007
Posts: 41
Reputation: mank is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mank mank is offline Offline
Light Poster

print, number of character from a file

  #1  
Mar 25th, 2008
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;
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Posts: 41
Reputation: mank is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mank mank is offline Offline
Light Poster

Re: print, number of character from a file

  #2  
Mar 25th, 2008
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;
Reply With Quote  
Join Date: Jan 2006
Posts: 215
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: print, number of character from a file

  #3  
Mar 25th, 2008
Hi, im just modifying your code, use substr() method to get substring from a string.

Syntax: substr EXPR,OFFSET,LENGTH

  1. #!/usr/bin/perl -w
  2. use strict;
  3.  
  4. use warnings;
  5.  
  6. my ($fileName, $firstSequence, $lastSequence) = @ARGV;
  7.  
  8. open (IN, "< $fileName") || die "Can't open $fileName";
  9.  
  10. while(<IN>) {
  11. chomp;
  12. print "\n",substr($_, $firstSequence, $lastSequence);
  13. }
  14.  
  15. close(IN);
  16. 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
Reply With Quote  
Join Date: Jan 2006
Posts: 215
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: print, number of character from a file

  #4  
Mar 25th, 2008
Originally Posted by mank View Post
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
Reply With Quote  
Join Date: Oct 2007
Posts: 41
Reputation: mank is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mank mank is offline Offline
Light Poster

Re: print, number of character from a file

  #5  
Mar 25th, 2008
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.
Reply With Quote  
Join Date: Jan 2006
Posts: 215
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: print, number of character from a file

  #6  
Mar 25th, 2008
Originally Posted by mank View Post
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.
  1. ...
  2. my $line = '';
  3. while my $l(<IN>){
  4. $line .= $l; # entire file in a line
  5. }
  6. ...
challenge the limits
Reply With Quote  
Join Date: Mar 2006
Posts: 584
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 30
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Posting Pro

Re: print, number of character from a file

  #7  
Mar 25th, 2008
You probably want to remove the newlines though.
Reply With Quote  
Join Date: Oct 2007
Posts: 41
Reputation: mank is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mank mank is offline Offline
Light Poster

Re: print, number of character from a file

  #8  
Mar 25th, 2008
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.
Reply With Quote  
Join Date: Oct 2007
Posts: 41
Reputation: mank is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mank mank is offline Offline
Light Poster

Re: print, number of character from a file

  #9  
Mar 26th, 2008
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;
Reply With Quote  
Join Date: Jan 2006
Posts: 215
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: print, number of character from a file

  #10  
Mar 26th, 2008
Originally Posted by mank View Post
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,
  1. open(F, 'f1.txt') or die "$!";
  2. my $line;
  3. while (my $l = <F>){
  4. chomp($l); # get rid of newline at the end of line
  5. $line .= $l;
  6. }
  7. print $line;
  8. close(F);

@Kevin, thanks for that point

katharnakh.
Last edited by katharnakh : Mar 26th, 2008 at 3:53 am.
challenge the limits
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Perl Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 5:52 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC