I think what I would call a "row" is different than what you are calling a
row.  I would call the third row 

        "ten", "eleven", "twelve"

but that wouldn't be too difficult, so I assume that you mean  

        "three"
        "six"
        "nine"
        "twelve"

for this, I'd do something like this

        @row3 = map {[splice(@$_, 3, 1)]} @matrix;

now, @matrix looks like this

        (
                ["one","two"]
                ["four","five"]
                ["seven","eight"]
                ["ten","eleven"]
        )

and @row three looks like this

        (
                ["three"]
                ["six"]
                ["nine"]
                ["twelve"]
        )

Note: @row3 three is still a list of lists.  If you want it in a regular
list format, remove the square brackets.


HTH

wantor


> -----Original Message-----
> From: Moulder, Glen [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, March 13, 2001 2:46 PM
> To: 'perl-win32-users'
> Subject: Q: Using splice on a matrix
> 
> 
> Good day perlfolk,
> 
> I often work with data matrices (lists of lists) in my 
> scripts.  A simple example is:
> 
> @matrix = ();
> @list1 = ("one","two","three");
> @list2 = ("four","five","six");
> @list3 = ("seven","eight","nine");
> @list4 = ("ten","eleven","twelve");
> push @matrix, [ @list1 ];
> push @matrix, [ @list2 ];
> push @matrix, [ @list3 ];
> push @matrix, [ @list4 ];
> 
> Sometimes I want to extract one row, like maybe the third 
> one, shortening the matrix and putting the row in another 
> list.  The only method I've found so far that works is the 
> tedious one of using for loops to iterate through the matrix 
> and create two new matrices, one that contains the row(s) I 
> want, and one the row(s) I don't currently want, discarding 
> the original matrix.  I've tried using different variations 
> of splice (sample below) to accomplish the creation of the 
> "wanted" and "unwanted" matrices, but have had no success.
> 
> $row_elements = $#list4;
> splice(@new_list,0,$row_elements,splice(@matrix,2,$row_elements));
> 
> None of the common Perl books talk about this.  Chapter 7 of 
> Mastering Algorithms with Perl talks about two modules for 
> dealing with matrices, Math::MatrixReal and PDL.  PDL is only 
> available on CPAN and does not install correctly on Win32.  
> The other module has an unusual one-indexing feature (the 
> upper left element of a matrix is 1,1 - not 0,0 as in regular 
> lists) that I don't want to use because I'm sure I'll get 
> confused working with indices on different types of lists.  
> Plus, I'd rather not install yet another module to do 
> something that could be accomplished with a function.  Anyone 
> working with matrices and splice?  I could use a hand 
> understanding this.  
> 
> TIA,
> Glen
> _______________________________________________
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
> 
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

Reply via email to