<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Remember the code? &#187; perl</title>
	<atom:link href="http://code.neox.net/category/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.neox.net</link>
	<description>a place where I store my secret powerful coding snippets, or just to remember how to do things</description>
	<lastBuildDate>Fri, 19 Jun 2009 11:36:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>[Perl] Counting referenced array&#8217;s length</title>
		<link>http://code.neox.net/2008/05/15/perl-counting-referenced-arrays-length/</link>
		<comments>http://code.neox.net/2008/05/15/perl-counting-referenced-arrays-length/#comments</comments>
		<pubDate>Thu, 15 May 2008 22:12:37 +0000</pubDate>
		<dc:creator>HanaDaddy</dc:creator>
				<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://code.neox.net/?p=10</guid>
		<description><![CDATA[Perl is a powerful script language which is much faster than simple shell script and there are lots of libraries that support virtually every tasks out there. But it is little difficult to learn at first.
One of the weak area of perl is that it doesn&#8217;t support multi dimensional array naturally. In order to implement [...]]]></description>
			<content:encoded><![CDATA[<p>Perl is a powerful script language which is much faster than simple shell script and there are lots of libraries that support virtually every tasks out there. But it is little difficult to learn at first.</p>
<p>One of the weak area of perl is that it doesn&#8217;t support multi dimensional array naturally. In order to implement multi dimensional array, you will need to use pointer references. (There may be other ways) </p>
<p>You can referece variable and array by adding <code>"\"</code> backslash in front of the variable.</p>
<p>For example</p>

<div class="wp_syntax"><div class="code"><pre class="perl"><span style="color: #808080; font-style: italic;">#create array</span>
<span style="color: #0000ff;">@a</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">#create reference to array a</span>
<span style="color: #0000ff;">$aref</span>=\<span style="color: #0000ff;">@a</span>;</pre></div></div>

<p>In order to access the variable,you just add the proper symbol in front of the var name.<br />
You will need to use the referenced variable name with the &#8216;$&#8217; sign intact.</p>
<ul>
<li>if you are accessing it as a array  , @$ref would refer to the whole array</li>
<li>if you are accessing the element of the array , $$ref[0] would refer the first element.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="perl"><span style="color: #808080; font-style: italic;">#To access the first element of the referenced array</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;first element: &quot;</span> . <span style="color: #0000ff;">$$arref</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;  <span style="color: #808080; font-style: italic;"># this will print '2'</span></pre></div></div>

<p><span id="more-10"></span><br />
To find Referenced Array&#8217;s length, you use the same convention. Just like the regular array, you put <code>$#</code> in front of the referenced array&#8217;s name. Note the curly bracket will help you to read source code more clearly.</p>

<div class="wp_syntax"><div class="code"><pre class="perl"><span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;array length is &quot;</span>. $<span style="color: #808080; font-style: italic;">#{$arref}  . &quot; or &quot; . $#$arref  . &quot;\n&quot;; </span>
<span style="color: #808080; font-style: italic;">#array length is 5 or 5</span></pre></div></div>

<p><code>$#</code> convention returns the length &#8211; 1 . So in fact the actual length is 6.</p>
<p>Here is the complete example.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="perl"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@a</span> = <span style="color: #000066;">qw</span><span style="color: #66cc66;">&#40;</span>one two three four five six<span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$aref</span> = \<span style="color: #0000ff;">@a</span>;
&nbsp;
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;-- Actual Array --<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Number of elements in the array: $#a<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>; 
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;-- Reference --<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Number of elements in the array (ref): &quot;</span>. $<span style="color: #808080; font-style: italic;">#{$aref} . &quot;\n&quot;;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Number of elements in the array (ref): &quot;</span> . <span style="color: #0000ff;">@$aref</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre></td></tr></table></div>

<p>Output</p>

<div class="wp_syntax"><div class="code"><pre>$&gt; perl test.pl
-- Actual Array --
Number of elements in the array: 5
-- Reference --
Number of elements in the array (ref): 5
Number of elements in the array (ref): 6</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://code.neox.net/2008/05/15/perl-counting-referenced-arrays-length/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk (enhanced)

Served from: code.neox.net @ 2012-02-07 13:18:05 -->
