Deleting and moving multi-dim array "columns"

D

Domenico Discepola

Hello. Given a variable $delete_col, how can I "delete" an entire array
"column" and "move" the remaining columns to the left? E.g. how do I turn:
( ["a", "b", "c"], ["1", "2", "3"] ) into ( ["a", "c"], ["1", "3"] ) given
that the value of $delete_col = 1?

TIA

#########
#!perl
use strict;
use warnings;

my $delete_col = 1;
my @arr01 = ( ["a", "b", "c"], ["1", "2", "3"] );
my ( $row, $col ) = 0;

foreach ( @arr01 ) {
#Get # cols in array
my $tot_cols = ( $#{$arr01[$row]} );

for ($col=0;$col<=$tot_cols;$col++) {

#Printing just for an example
if ( $col == $tot_cols ) {
print "$arr01[${row}][${col}]" ;
} else {
print "$arr01[${row}][${col}]:" ;
}
}
print "\n";
$row++;
}
exit 0;
 
J

Jay Tilton

: Hello. Given a variable $delete_col, how can I "delete" an entire array
: "column" and "move" the remaining columns to the left? E.g. how do I turn:
: ( ["a", "b", "c"], ["1", "2", "3"] ) into ( ["a", "c"], ["1", "3"] ) given
: that the value of $delete_col = 1?
:
: TIA
:
: #########
: #!perl
: use strict;
: use warnings;
:
: my $delete_col = 1;
: my @arr01 = ( ["a", "b", "c"], ["1", "2", "3"] );

splice(@$_, $delete_col, 1) for @arr01;

: my ( $row, $col ) = 0;
: foreach ( @arr01 ) {
: #Get # cols in array
: my $tot_cols = ( $#{$arr01[$row]} );
: for ($col=0;$col<=$tot_cols;$col++) {
: #Printing just for an example
: if ( $col == $tot_cols ) {
: print "$arr01[${row}][${col}]" ;
: } else {
: print "$arr01[${row}][${col}]:" ;
: }
: }
: print "\n";
: $row++;
: }

A less clumsy way to print out your matrix:

print join(':', @$_), "\n" for @arr01;
 
U

Uri Guttman

DD" == Domenico Discepola said:
Hello. Given a variable $delete_col, how can I "delete" an entire array
"column" and "move" the remaining columns to the left? E.g. how do I turn:
( ["a", "b", "c"], ["1", "2", "3"] ) into ( ["a", "c"], ["1", "3"] ) given
that the value of $delete_col = 1?

perldoc -f splice

splice( @{$_}, $delete_col, 1 ) for @list_of_rows ;

uri
 
B

Brian McCauley

Uri Guttman said:
Hello. Given a variable $delete_col, how can I "delete" an entire array
"column" and "move" the remaining columns to the left? E.g. how do I turn:
( ["a", "b", "c"], ["1", "2", "3"] ) into ( ["a", "c"], ["1", "3"] ) given
that the value of $delete_col = 1?

perldoc -f splice

splice( @{$_}, $delete_col, 1 ) for @list_of_rows ;

It should be noted that 4 of the puntuation marks above are included
by Uri because, presumably, he finds that they aid (human)
readability. As far as Perl is concerned they are redundant.

splice @$_, $delete_col, 1 for @list_of_rows;

Which you find more readable is a matter of taste.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
D

Domenico Discepola

Jay Tilton said:
: Hello. Given a variable $delete_col, how can I "delete" an entire array
: "column" and "move" the remaining columns to the left? E.g. how do I turn:
: ( ["a", "b", "c"], ["1", "2", "3"] ) into ( ["a", "c"], ["1", "3"] ) given
: that the value of $delete_col = 1?

Thanks for your replies - it's always nice to know that my ~20 lines of code
can be reduced down to 4 ;-) That one liner ( print join(':', @$_), "\n"
for @arr01; ) will certainly come in handy...

Dom
 
U

Uri Guttman

It should be noted that 4 of the puntuation marks above are included
by Uri because, presumably, he finds that they aid (human)
readability. As far as Perl is concerned they are redundant.

you betcha your ass it is more readable! :)
splice @$_, $delete_col, 1 for @list_of_rows;
Which you find more readable is a matter of taste.

and mine is correct as are all of my opinions! :)

uri
 
U

Uri Guttman

DD" == Domenico Discepola said:
Jay Tilton said:
: Hello. Given a variable $delete_col, how can I "delete" an entire array
: "column" and "move" the remaining columns to the left? E.g. how do I turn:
: ( ["a", "b", "c"], ["1", "2", "3"] ) into ( ["a", "c"], ["1", "3"] ) given
: that the value of $delete_col = 1?
Thanks for your replies - it's always nice to know that my ~20 lines of code
can be reduced down to 4 ;-) That one liner ( print join(':', @$_), "\n"
for @arr01; ) will certainly come in handy...

local $" = ':' ;
print map "@{$_}\n", @arr01 ;

i actually don't use $" anywhere but that is a decent idiom to know.

also a rule i have is "print rarely, print late".

print is very slow so it is better to call it as rarely as
possible. build up your strings or arrays of strings and print them all
at once. the for version calls print over and over.

print late means that you shouldn't print as soon as you have data. in
many case you may want to control where and when stuff gets printed. if
you have lots of print statements that is annoying to manage. so again,
build up the text in a scalar or array and print at the end. if it is in
a sub, i like to return the data to be printed and let the caller decide
what to do with it. it could be printed to the screen and logged and
whatever and that logic should be in the called in most cases.

uri
 
P

pkent

Brian McCauley said:
It should be noted that 4 of the puntuation marks above are included
by Uri because, presumably, he finds that they aid (human)
readability. As far as Perl is concerned they are redundant.

splice @$_, $delete_col, 1 for @list_of_rows;

Which you find more readable is a matter of taste.

IMHO the example above isn't so hard to read anyway because it's just a
splice() with the 'for' statement modifier, so it's pretty clear-cut...
but some code isn't as clear.

As my job, like many people here, involves working with other people's
code, if something makes no real difference to perl but helps some
people understand my code better by making parts of it explicit and not
requiring them to even think (and thereby run the risk of thinking the
wrong thing), I'll write the one that helps out people. I care about
people. The machine will, as you say, interpret it the same.

P // working with old, badly commented, hard to read code for money!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top