Very first PERL program with strings

using substr, chomp, assigning variables

Page 1 of 1

1 Replies - 519 Views - Last Post: 15 February 2010 - 06:26 AM

#1 amindlessbrain  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 13-October 09

Very first PERL program with strings

Posted 14 February 2010 - 06:25 AM

Hello everyone,

I am ultra new to perl, as in this is my first Perl program. I took Python eons ago, and it seems to be similar from what I remember.

Anyways, I am having some trouble.

#!/usr/bin/perl 

my $DNA = 'ATTAGGATGCCTGCGCGCGAATGCGC';

my $len  = length $DNA;

print "Length DNA: ", $DNA, " is ", $len, " sequences long ", "\n";

my $DNA1 = substr ($DNA , 0, $len, 'GCGCGCGAATGCGC' );

$len = length $DNA1;

print "Length DNA1: ", $DNA1, " is ", $len, " sequences long ", "\n";

my $DNA2 = substr ($DNA , 0, $len, 'GCGCGCG');

$len = length $DNA2;

print "Length DNA2: ", $DNA2, " is ", $len, " sequences long ", "\n";



When I run it I get

Length DNA: ATTAGGATGCCTGCGCGCGAATGCGC is 26 sequences long
Length DNA1: ATTAGGATGCCTGCGCGCGAATGCGC is 26 sequences long
Length DNA2: GCGCGCGAATGCGC is 14 sequences long

For whatever reason its skipping down. What is printing out as DNA1 is actually DNA, and what is printing out as DNA2 is actually DNA1, and no DNA2 is printing out at all.

Any help would be very appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Very first PERL program with strings

#2 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Very first PERL program with strings

Posted 15 February 2010 - 06:26 AM

When you use substr with four arguments, it returns the original value of the string, not the modified version. Per http://perldoc.perl....ons/substr.html :

Quote

An alternative to using substr() as an lvalue is to specify the replacement string as the 4th argument. This allows you to replace parts of the EXPR and return what was there before in one operation, just as you can with splice().
(emphasis mine)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1