Homework: substr function

Reading characters in a string with substr

Today we are going to learn about a new function with the friendly name of “substr”! Its purpose is to extract a piece of a string, also known as substring. This is homework hw6.

For instance consider the following perl line:

print substr('0123456789', 3, 2) . "\n";

It prints:


which is a substring of ‘0123456789’ that starts from the position 3 and is 2 character long.

The function substr takes 3 arguments, for instance:

substr($string, $offset, $length)

The arguments are:

  1. $string: the string it extracts the substring from;
  2. $offset: the starting position of the substring (starting from zero!);
  3. $length: the length of the substring.

So substr(‘abcdef’, 2 , 1) returns ‘c’. This is because the offset is 2 but we start counting from zero, so 2 means that the substring starts from the third character. Since the length is 1 we get a one character string. Here is the full documentation for substr. It is a page from the following website http://perldoc.perl.org, which is very useful because it contains the full documentation about perl.

Now let us see a little program:

$stringona = '0123456789abcdef';
for ($l = length($stringona); $l > 0; $l = $l - 1) {
    print "Sottostringhe di lunghezza $l:\n";
    for ($i = 0; $i <= length($stringona) - $l; $i = $i + 1) {
        print substr($stringona, $i, $l) . "\n";

Try it as usual on codepad.org and then…

Perl script 1:

given the string:


use a loop and substr to print its “codons”, one per line.

Submit it as usual.


3 thoughts on “Homework: substr function

  1. Roberta says:

    Ciao,in questo esercizio non ho capito se dobbiamo scrivere tutti i codoni possibili (quindi a partire da GAG TAT CTG.. , poi quelli a partire da AGT ATC TGG .. e poi quelli a partire da GTA TCT GGA) oppure solo quelli a partire dalla prima base, quindi solo GAG TAT CTG..?
    grazie mille

  2. […] The problem was: […]

  3. Andrea Telatin says:

    entrambe le soluzioni sono valide e interessanti. La seconda mi pare più semplice e “utile” per iniziare.

Leave a Reply

Your email address will not be published. Required fields are marked *