<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Copying an Array, with Modifications</title>
	<atom:link href="http://www.bayview.com/blog/2002/12/06/copying-an-array-with-modifications/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bayview.com/blog/2002/12/06/copying-an-array-with-modifications/</link>
	<description>Bay View Consulting Services, Inc.</description>
	<pubDate>Thu, 28 Aug 2008 18:13:50 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: William Ward</title>
		<link>http://www.bayview.com/blog/2002/12/06/copying-an-array-with-modifications/#comment-11504</link>
		<dc:creator>William Ward</dc:creator>
		<pubDate>Thu, 07 Jun 2007 22:33:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=4#comment-11504</guid>
		<description>I don't think you need @AoA1 and @AoA2.  You should be able to use $arr1 and $arr2 directly.  Using @AoA1 and @AoA2 copies the contents of those original arrays unnecessarily; only an issue if they are large of course, but still worthy of remembering.  Here's my version:

use strict;
use warnings;
use Data::Dumper;

my @AoA = ([ 1, 2, 3], [10, 20, 30]);
my @addIn2DAoA = ([ 4, 5 ], [40, 50, 60, 70]);
my @combined2D = merge2D(\@AoA, \@addIn2DAoA);
print Dumper \@AoA, \@addIn2DAoA, \@combined2D;
sub merge2D {
&#160;&#160;&#160;&#160;my (&#36;arr1, &#36;arr2) = @_;
&#160;&#160;&#160;&#160;my &#36;rownum1 = @&#36;arr1;
&#160;&#160;&#160;&#160;my &#36;rownum2 = @&#36;arr2;
&#160;&#160;&#160;&#160;my @combined = ();

&#160;&#160;&#160;&#160;if (&#36;rownum1 != &#36;rownum2)
&#160;&#160;&#160;&#160;{&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;  # end the program, return an empty 2D array
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return @combined;
&#160;&#160;&#160;&#160;}

&#160;&#160;&#160;&#160;my @newrow = ();
&#160;&#160;&#160;&#160;for (my &#36;i = 0; &#36;i &#60; &#36;rownum1; &#36;i++)
&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;@newrow = (@{&#36;arr1-&#62;[&#36;i]}, @{&#36;arr2-&#62;[&#36;i]});
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;push @combined, [@newrow];
&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;return @combined;
}</description>
		<content:encoded><![CDATA[<p>I don&#8217;t think you need @AoA1 and @AoA2.  You should be able to use $arr1 and $arr2 directly.  Using @AoA1 and @AoA2 copies the contents of those original arrays unnecessarily; only an issue if they are large of course, but still worthy of remembering.  Here&#8217;s my version:</p>
<p>use strict;<br />
use warnings;<br />
use Data::Dumper;</p>
<p>my @AoA = ([ 1, 2, 3], [10, 20, 30]);<br />
my @addIn2DAoA = ([ 4, 5 ], [40, 50, 60, 70]);<br />
my @combined2D = merge2D(\@AoA, \@addIn2DAoA);<br />
print Dumper \@AoA, \@addIn2DAoA, \@combined2D;<br />
sub merge2D {<br />
&nbsp;&nbsp;&nbsp;&nbsp;my (&#36;arr1, &#36;arr2) = @_;<br />
&nbsp;&nbsp;&nbsp;&nbsp;my &#36;rownum1 = @&#36;arr1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;my &#36;rownum2 = @&#36;arr2;<br />
&nbsp;&nbsp;&nbsp;&nbsp;my @combined = ();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;if (&#36;rownum1 != &#36;rownum2)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  # end the program, return an empty 2D array<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return @combined;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;my @newrow = ();<br />
&nbsp;&nbsp;&nbsp;&nbsp;for (my &#36;i = 0; &#36;i &lt; &#36;rownum1; &#36;i++)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@newrow = (@{&#36;arr1-&gt;[&#36;i]}, @{&#36;arr2-&gt;[&#36;i]});<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push @combined, [@newrow];<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;return @combined;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chunyu</title>
		<link>http://www.bayview.com/blog/2002/12/06/copying-an-array-with-modifications/#comment-11491</link>
		<dc:creator>Chunyu</dc:creator>
		<pubDate>Thu, 07 Jun 2007 21:35:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=4#comment-11491</guid>
		<description>Here is the subroutine that will do the trick of merging two 2-D array, assuming they have equal number of rows.
Call it by:     @combined2D = merge2D(\@AoA, \@addIn2DAoA);

 sub merge2D {
      my ($arr1, $arr2) = @_;
	my @AoA1 = @{$arr1};
	my @AoA2 = @{$arr2};
	my $rownum1 = @AoA1;
	my $rownum2 = @AoA2;
	my @combined = ();	
			 	
	if ($rownum1 != $rownum2){	# end the program, return an empty 2D array
	 	return @combined;
	 }

  	my @newrow = ();	
  	for (my $i = 0; $i &#60; $rownum1; $i++){
  		@newrow = (@{$AoA1[$i]}, @{$AoA2[$i]});
 		push @combined, [@newrow];
	}    
 		return @combined;
}</description>
		<content:encoded><![CDATA[<p>Here is the subroutine that will do the trick of merging two 2-D array, assuming they have equal number of rows.<br />
Call it by:     @combined2D = merge2D(\@AoA, \@addIn2DAoA);</p>
<p> sub merge2D {<br />
      my ($arr1, $arr2) = @_;<br />
	my @AoA1 = @{$arr1};<br />
	my @AoA2 = @{$arr2};<br />
	my $rownum1 = @AoA1;<br />
	my $rownum2 = @AoA2;<br />
	my @combined = ();	</p>
<p>	if ($rownum1 != $rownum2){	# end the program, return an empty 2D array<br />
	 	return @combined;<br />
	 }</p>
<p>  	my @newrow = ();<br />
  	for (my $i = 0; $i &lt; $rownum1; $i++){<br />
  		@newrow = (@{$AoA1[$i]}, @{$AoA2[$i]});<br />
 		push @combined, [@newrow];<br />
	}<br />
 		return @combined;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chunyu</title>
		<link>http://www.bayview.com/blog/2002/12/06/copying-an-array-with-modifications/#comment-7001</link>
		<dc:creator>Chunyu</dc:creator>
		<pubDate>Wed, 25 Apr 2007 18:06:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=4#comment-7001</guid>
		<description>How to concatenate two 2D array, but put the 2nd array at the end of each row, instead of adding additional rows?
Example
my @first = ( [1, 2, 3], [4, 5, 6] );
my @second = ( [7, 8, 9], [10, 11, 12] );
I need a output of:
@new = ([1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]);

How to get there?

thanks!</description>
		<content:encoded><![CDATA[<p>How to concatenate two 2D array, but put the 2nd array at the end of each row, instead of adding additional rows?<br />
Example<br />
my @first = ( [1, 2, 3], [4, 5, 6] );<br />
my @second = ( [7, 8, 9], [10, 11, 12] );<br />
I need a output of:<br />
@new = ([1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]);</p>
<p>How to get there?</p>
<p>thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William Ward</title>
		<link>http://www.bayview.com/blog/2002/12/06/copying-an-array-with-modifications/#comment-3689</link>
		<dc:creator>William Ward</dc:creator>
		<pubDate>Thu, 15 Feb 2007 19:00:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=4#comment-3689</guid>
		<description>Glad I could help.</description>
		<content:encoded><![CDATA[<p>Glad I could help.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Benjamin Clare</title>
		<link>http://www.bayview.com/blog/2002/12/06/copying-an-array-with-modifications/#comment-3688</link>
		<dc:creator>Benjamin Clare</dc:creator>
		<pubDate>Thu, 15 Feb 2007 17:55:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=4#comment-3688</guid>
		<description>You are a star, thank you!

I have finally managed to get it to work properly. All of the problems stemmed from calling the array from inside the sub which I probably would never have worked out.

Thanks again

Ben</description>
		<content:encoded><![CDATA[<p>You are a star, thank you!</p>
<p>I have finally managed to get it to work properly. All of the problems stemmed from calling the array from inside the sub which I probably would never have worked out.</p>
<p>Thanks again</p>
<p>Ben</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William Ward</title>
		<link>http://www.bayview.com/blog/2002/12/06/copying-an-array-with-modifications/#comment-3670</link>
		<dc:creator>William Ward</dc:creator>
		<pubDate>Thu, 15 Feb 2007 01:20:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=4#comment-3670</guid>
		<description>The problem is your variable @outLines is local to the subroutine.  Since the subroutine is called once for each file, the array is reset each time.  My solution had the array outside the subroutine, and it was being continually added to.</description>
		<content:encoded><![CDATA[<p>The problem is your variable @outLines is local to the subroutine.  Since the subroutine is called once for each file, the array is reset each time.  My solution had the array outside the subroutine, and it was being continually added to.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Benjamin Clare</title>
		<link>http://www.bayview.com/blog/2002/12/06/copying-an-array-with-modifications/#comment-3665</link>
		<dc:creator>Benjamin Clare</dc:creator>
		<pubDate>Wed, 14 Feb 2007 23:26:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=4#comment-3665</guid>
		<description>Thanks for that, you have certainly pushed my in the right direction. However, having modified it slightly I still get an array of array. 

I can sort each sub-array but then end up with multiple sorted lists. I would then like to remove duplicates etc but this only works on each individual list.

I am currently testing on three files and this is the code and output:-

sub process #Reads each review file and creates list of words
{
    my @outLines;  #Data that is going to output
    my $outLines;
    my $line;      #Data that is read line by line
    my @wordList;  #Sorted array
    my $wordList;

    if ( $File::Find::name =~ /\.txt$/ ) #Find .txt files
	{

	open ( REVIEWFILE, $File::Find::name ) or die "Cannot open file: $!"; #Open .txt file

	while (defined($line =  ))
		{
		$line =~ s/[’]+//g; #Remove ‘ from words
		push(@outLines, split(/[^a-z]+/, $line)); #Place each word into array
		}

	close ( REVIEWFILE );
	}
        
        @outLines = sort(@outLines);

        print "@outLines\n";
       
}

Reading files.

  a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a about about about.....
 a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a about acceptable action.....
  a a a a a a a a a a a a a a a a a a about about about about absurdist acting..... 
All files read.</description>
		<content:encoded><![CDATA[<p>Thanks for that, you have certainly pushed my in the right direction. However, having modified it slightly I still get an array of array. </p>
<p>I can sort each sub-array but then end up with multiple sorted lists. I would then like to remove duplicates etc but this only works on each individual list.</p>
<p>I am currently testing on three files and this is the code and output:-</p>
<p>sub process #Reads each review file and creates list of words<br />
{<br />
    my @outLines;  #Data that is going to output<br />
    my $outLines;<br />
    my $line;      #Data that is read line by line<br />
    my @wordList;  #Sorted array<br />
    my $wordList;</p>
<p>    if ( $File::Find::name =~ /\.txt$/ ) #Find .txt files<br />
	{</p>
<p>	open ( REVIEWFILE, $File::Find::name ) or die &#8220;Cannot open file: $!&#8221;; #Open .txt file</p>
<p>	while (defined($line =  ))<br />
		{<br />
		$line =~ s/[’]+//g; #Remove ‘ from words<br />
		push(@outLines, split(/[^a-z]+/, $line)); #Place each word into array<br />
		}</p>
<p>	close ( REVIEWFILE );<br />
	}</p>
<p>        @outLines = sort(@outLines);</p>
<p>        print &#8220;@outLines\n&#8221;;</p>
<p>}</p>
<p>Reading files.</p>
<p>  a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a about about about&#8230;..<br />
 a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a about acceptable action&#8230;..<br />
  a a a a a a a a a a a a a a a a a a about about about about absurdist acting&#8230;..<br />
All files read.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William Ward</title>
		<link>http://www.bayview.com/blog/2002/12/06/copying-an-array-with-modifications/#comment-3663</link>
		<dc:creator>William Ward</dc:creator>
		<pubDate>Wed, 14 Feb 2007 22:03:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=4#comment-3663</guid>
		<description>I would do the split() as I am reading it, and push the result onto an array then.  Something like this:

my @all_words;

sub process #Reads each review file and creates list of words
{
if ( $File::Find::name =~ /\.txt$/ ) #Find .txt files
{
open ( REVIEWFILE, $File::Find::name ) or die “Cannot open file: $!”; #Open .txt file
while (defined(my $line = &#60;REVIEWFILE&#62; ))
{
$line =~ s/[’]+//g; #Remove ‘ from words
push(@all_words, split(/[^a-z]+/, $line)); #Place each word into array
}
close ( REVIEWFILE );
}

I combined two of your substitution regexes into a split argument.  Now I haven't tested this but I think it should work unless I overlooked something...  the key is using push() with a list as the second argument.</description>
		<content:encoded><![CDATA[<p>I would do the split() as I am reading it, and push the result onto an array then.  Something like this:</p>
<p>my @all_words;</p>
<p>sub process #Reads each review file and creates list of words<br />
{<br />
if ( $File::Find::name =~ /\.txt$/ ) #Find .txt files<br />
{<br />
open ( REVIEWFILE, $File::Find::name ) or die “Cannot open file: $!”; #Open .txt file<br />
while (defined(my $line = &lt;REVIEWFILE&gt; ))<br />
{<br />
$line =~ s/[’]+//g; #Remove ‘ from words<br />
push(@all_words, split(/[^a-z]+/, $line)); #Place each word into array<br />
}<br />
close ( REVIEWFILE );<br />
}</p>
<p>I combined two of your substitution regexes into a split argument.  Now I haven&#8217;t tested this but I think it should work unless I overlooked something&#8230;  the key is using push() with a list as the second argument.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Benjamin Clare</title>
		<link>http://www.bayview.com/blog/2002/12/06/copying-an-array-with-modifications/#comment-3662</link>
		<dc:creator>Benjamin Clare</dc:creator>
		<pubDate>Wed, 14 Feb 2007 21:35:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=4#comment-3662</guid>
		<description>I am trying to develop a perl script which will make a sorted list of all the words used in a large number of text files (namely 1000 movie reviews in .txt format). 

I am currently using the File::Find function to iterate through each file and, after some basic regex, creating an array where each element is an array of the line being read (AoA). Then I am writing each array to a seperate file creating a list of every word used. I am then reading this file back to a new array - as each word is a now a new line it is easy to split() in order to sort etc creating the final list.

If that was't too confusing I would like to miss out the step of having to write to a file (OUTWORDS) and be able to read each word of each file to the next element of the original array so that I can sort etc without the need to use a seperate file.

Thank you.

Here is the basic part of the code:

sub process   #Reads each review file and creates list of words
{
    my @outLines;  #Data that is going to output
    my $line;      #Data that is read line by line

    if ( $File::Find::name =~ /\.txt$/ )   #Find .txt files
        {

        open ( REVIEWFILE, $File::Find::name ) or  die "Cannot open file: $!"; #Open .txt file

       # print $File::Find::name . "\n";

          while ( $line =  )
          {
              $line =~ s/[']+//g;         #Remove ' from words
              $line =~ s/[^a-z]/\n/g;     #All non alphabet to newline
              $line =~ s/\n+/\n/g;        #Squeeze newlines

              push(@outLines, $line);     #Place each line into array
          }

      close ( REVIEWFILE );

      open ( OUTWORDS, "&#62;&#62;$outFile") &#124;&#124; die "Cannot open file: $!";
      print ( OUTWORDS @outLines );       #print array to file
      close ( OUTWORDS );

      undef( @outLines );
        }</description>
		<content:encoded><![CDATA[<p>I am trying to develop a perl script which will make a sorted list of all the words used in a large number of text files (namely 1000 movie reviews in .txt format). </p>
<p>I am currently using the File::Find function to iterate through each file and, after some basic regex, creating an array where each element is an array of the line being read (AoA). Then I am writing each array to a seperate file creating a list of every word used. I am then reading this file back to a new array - as each word is a now a new line it is easy to split() in order to sort etc creating the final list.</p>
<p>If that was&#8217;t too confusing I would like to miss out the step of having to write to a file (OUTWORDS) and be able to read each word of each file to the next element of the original array so that I can sort etc without the need to use a seperate file.</p>
<p>Thank you.</p>
<p>Here is the basic part of the code:</p>
<p>sub process   #Reads each review file and creates list of words<br />
{<br />
    my @outLines;  #Data that is going to output<br />
    my $line;      #Data that is read line by line</p>
<p>    if ( $File::Find::name =~ /\.txt$/ )   #Find .txt files<br />
        {</p>
<p>        open ( REVIEWFILE, $File::Find::name ) or  die &#8220;Cannot open file: $!&#8221;; #Open .txt file</p>
<p>       # print $File::Find::name . &#8220;\n&#8221;;</p>
<p>          while ( $line =  )<br />
          {<br />
              $line =~ s/[']+//g;         #Remove &#8216; from words<br />
              $line =~ s/[^a-z]/\n/g;     #All non alphabet to newline<br />
              $line =~ s/\n+/\n/g;        #Squeeze newlines</p>
<p>              push(@outLines, $line);     #Place each line into array<br />
          }</p>
<p>      close ( REVIEWFILE );</p>
<p>      open ( OUTWORDS, &#8220;&gt;&gt;$outFile&#8221;) || die &#8220;Cannot open file: $!&#8221;;<br />
      print ( OUTWORDS @outLines );       #print array to file<br />
      close ( OUTWORDS );</p>
<p>      undef( @outLines );<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sakthivel</title>
		<link>http://www.bayview.com/blog/2002/12/06/copying-an-array-with-modifications/#comment-3140</link>
		<dc:creator>sakthivel</dc:creator>
		<pubDate>Fri, 26 Jan 2007 17:22:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=4#comment-3140</guid>
		<description>plz explain the last asked question</description>
		<content:encoded><![CDATA[<p>plz explain the last asked question</p>
]]></content:encoded>
	</item>
</channel>
</rss>
