Monday, September 10, 2012

perl - 15 examples of substr command



substr is a perl function to extract substring from a string. substring in Perl is little special compared to other languages due to some additional functionalities. Let us discuss the substr function with examples.

Syntax:
 substr(STRING, OFFSET,  LENGTH, REPLACEMENT)
where STRING - the actual string from which the substring is to be extracted
          OFFSET - the starting position from where to extract
          LENGTH - the number of characters to be extracted from OFFSET (Optional)
          REPLACEMENT - the string to be replaced (Optional)

1. To extract a substring from the 4th character onwards:
$ perl -e 'print substr("welcome",3),"\n";'
come
Offset 0 stands for 1st character, 1 for 2nd character and so on. Hence, for the 4th character, the offset given is 3. Since no LENGTH is provided, all the remaining characters from the offset are extracted.

2. To extract 2 characters from the 4th character:
$ perl -e 'print substr("welcome",3,2),"\n";'
co
  3 is the OFFSET and 2 is the LENGTH. Hence, the 2 characters "co" are extracted.

3. To replace 3(4th-6th) characters with "COM"
$ perl -e 'my $x="welcome";substr($x,3,3,"COM");print $x,"\n";'
welCOMe
When the 4th argument of substr is present, it means a substring is to be replaced. So, the substring of length 3 characters starting from the 4th character will be replaced with "COM" in the original string($x) itself. Hence, "welcome" becomes "welCOMe" after replacing 3 characters with 3 other characters.

4. Another way for the above:
$ perl -e 'my $x="welcome";substr($x,3,3)="COM";print $x,"\n";'
welCOMe
This is a little different compared to other programming languages wherein lvalue is a function. substr($x,3,3)="COM" is just another way of writing substr($x,3,3,"COM").

5. To replace 2 characters with 3 characters "ABC" :
$ perl -e 'my $x="welcome";substr($x,2,2,"ABC");print $x,"\n";'
weABCDome
   substr can also replace characters of different size as well. In this case, we replaced 2 characters with 3 characters.

6. To replace 3 characters with 2 characters:
$ perl -e 'my $x="welcome";substr($x,2,3,"AB");print $x,"\n";'
weABme
   The 3 character sub-string 'lco' is replaced with "AB".

7. To insert a substring "ABC" in the middle after 2 characters:
$ perl -e 'my $x="welcome";substr($x,2,0,"ABC");print $x,"\n";'
weABClcome
    Giving the length 0 means no character is to be replaced. Hence, the replacement string simply gets inserted in the desired place. In this way, substr can insert a sub-string as well.

8. To insert a substring in the beginning of a string:
$ perl -e 'my $x="welcome";substr($x,0,0,"ABC");print $x,"\n";'
ABCwelcome
   By making the offset 0, the insertion happens in the beginning.

9. To extract last 3 characters from a string:
$ perl -e 'print substr("welcome",-3),"\n";'
ome
substr can also take negative offset during which it will extract characters from behind.

10. To extract 2 characters before the last character:
$ perl -e 'print substr("welcome",-3,2),"\n";'
om
   By giving the length as 2, only 2 characters from the 3rd last character are obtained.

11. To delete a sub-string or to delete the last 3 characters:
$ perl -e 'my $x="welcome";substr($x,-3,3,"");print $x,"\n";'
welc
     "-3,3" tells the last 3 characters are to be replaced. By providing the replacement string as empty , the last 3 characters get deleted. However, this could also have been achieved using the "0,4" as length and offset, but this needs the user to know the length of the string.

12. To capitalize a sub-string or to capitalize the last 4 characters:
$ perl -e 'my $x="welcome";substr($x,-4)=~tr/[a-z]/[A-Z]/;print $x,"\n";'
welCOME
   tr command is against the sub-string to translate all the lower case alphabets to upper-case.

13. To substitute e with E only in the sub-string of last 3 characters:
$ perl -e 'my $x="welcome";substr($x,-3)=~s/e/E/;print $x,"\n";'
welcomE
    substitution(s) command is used to substitute 'e' with 'E'. substr allows this substitution to happen only in part of a string.

14. To append a string using the substr:
$ perl -e 'my $x="welcome";substr($x,7)="123";print $x,"\n";'
welcome123
By giving the offset equal to the length of the variable, a string can be appended.

15. To append a string with a space gap:
$ perl -e 'my $x="welcome";substr($x,8)="123";print $x,"\n";'
substr outside of string at -e line 1.
An error message is thrown out. This is because at the offset 8 is completely outside of the variable's space, and hence the string cannot be added.

However, we can tweak it by giving a space in the string to be appended:
$ perl -e 'my $x="welcome";substr($x,7)=" 123";print $x,"\n";'
welcome 123

No comments:

Post a Comment