View Full Version : array copy
cginewbie
09-08-03, 07:58 PM
hello,
Thanks if you can help...
Suppose that I have an array
@a = (a, b, c, d, e);
1. What are the choices to copy @b from @a with elements 1, 2, and 3?
2. What are the choices to permanently delete last elements @d, @e
thanks
1.
@a = ("a, b, c, d, e");
@b = "@a";
print "@b";
2.
Dont know really
hello,
Thanks if you can help...
Suppose that I have an array
@a = (a, b, c, d, e);
1. What are the choices to copy @b from @a with elements 1, 2, and 3?
2. What are the choices to permanently delete last elements @d, @e
thanks
#!/usr/bin/perl
use strict;
# COPY ELEMENTS
for(my $count=1; $count<4; $count++) {
$b[$count] = $a[$count];
print "Element $count of Array B has value ".$a[$count];
}
# DELETE ELEMENTS
$d[$#d] = '';
$e[$#e] = '';
__END__
rob2132
09-29-03, 01:07 AM
It's much easier to do this and very simple;
#!/usr/bin/perl
use warnings;
use strict;
my @a = qw(a b c d e); # array
print "\@a = @a\n"; # show current array
$#a = $#a-2; # New last element.
print "\@a = @a\n"; # Show new array.
[rob@system rob]$ ./example.pl
@a = a b c d e
@a = a b c
You can use slices as well:
@a = @a[0..$#a-2];
Or orther methods. These are the fastest, no need for all the other clutter.
If you really mean that you have an array of arrays, like your original question seems to indicate, then that's another matter--but I don't think that's what you meant.
vBulletin® v3.6.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.