Open main menu

Biolecture.org β

Changes

PERL

1,417 bytes added, 18:46, 17 June 2016
no edit summary
<hr />
<p><span style="font-size:14px">1) <strong>Variable</strong></span></p>
<p>Variable is a place to store a value, so we can refer to it or manipulate it throughout program. Perl has three types of variables; scalars, arrays and hases.</p>
</pre>
<p><span style="font-size:14px"><strong>Array (@)</strong></span></p>
<p><span style="font-size:14px">An array stores a list of values. While a scalar variable can only store one value, an array can store many. Perl array names are prefixed with an at-sign (@).&nbsp;In Perl, array indices start with 0, so to refer to the first element of the array @colors, you use $colors[0]. Note that when you&#39;re referring to a single element of an array, you prefix the name with a $ instead of the @. The $-sign again indicates that it&#39;s a single (scalar) value; the @-sign means you&#39;re talking about the entire array.</span></p>
<p>ex)</p>
<p>&nbsp;</p>
<p><span style="font-size:14px"><strong>Hash (%)</strong></span></p>
<p><span style="font-size:14px">A hash is a special kind of array - an associative array, or paired group of elements. Perl hash names are prefixed with a percent sign (%), and consist of pairs of elements - a key and a data value.</span></p>
<p>ex)</p>
);</pre>
<p>2) <strong>Function</strong></p> <p>Functions are blocks of codes that are given names so that we can use them as needed. Functions help&nbsp;to organize code into pieces that are easy to understand and work with. They help to&nbsp;build program step by step, testing the code along the way.</p> <p><span style="font-size:14px">2<strong>substr( ) </strong>Function</strongspan></p> <p><span style="font-size:14px">The&nbsp;<code>substr()</code>&nbsp;function is used to return a substring from the expression supplied as its first argument.&nbsp;</span><code>substr()</code>&nbsp;has a variable number of arguments, it can be told to start at an offset from either end of the expression, so it&nbsp;can supply a replacement string so that it replaces part of the expression as well as returning it, and it can be assigned to.</p> <p>substr( ) have three parameters; ( character, offset : location from &quot;0&quot;, length of character which will be extracted ). And it is also possible to add fourth parameter, the character which wants to exchage with.&nbsp;</p> <p>ex1)&nbsp;</p> <pre>my $str = &quot;The black cat climbed the green tree&quot;; say substr $str, 4, 5; # black</pre> <p>ex2)</p> <pre>my $z = substr $str, 14, 7, &quot;jumped from&quot;; say $z; # climbed say $str; # The black cat jumped from the green tree</pre>
<p>&nbsp;</p>