PDA

View Full Version : Remove alternate values from an array


rockdoc
02-27-2007, 08:32 AM
Hi,

If I have an array that looks like this
@values = (67 , 8 Geography , 88 , 9 Science , 56 , 11 Maths);

How can I separate every second item in the list into a new array?

I thought about using grep.. but the subject names also contain numbers.

rwedge
02-27-2007, 09:31 AM
One way to do it:my $len = @values = (67,'8 Geography',88,'9 Science',56,'11 Maths');
for (my $i=0;$i<$len-1;$i++) {
push(@nval, $values[++$i]);
}
print @nval;

BTW: the non-numeric values in your array need to be quoted;

FishMonger
02-27-2007, 06:28 PM
This slight adjustment to rwedge's solution is slightly more efficient because is itterates over every other array index.
for (my $i=0;$i<$len-1; $i+=2 ) {
push(@nval, $values[$i]);
}

rwedge
02-28-2007, 12:11 AM
The loop fires the same for each example. The output however is even or odd, depending where you start.
my $len = @values = (67,'8 Geography',88,'9 Science',56,'11 Maths');
my $c = 1;
for (my $i=0;$i<$len-1;$i+=2) {
push(@nval, $values[$i]);
$c++;
}
print "@nval <br> $c";

Output: 67 88 56
4
------------------------------------------------------
my $len = @values = (67,'8 Geography',88,'9 Science',56,'11 Maths');
my $c = 1;
for (my $i=0;$i<$len-1;$i++) {
push(@nval, $values[++$i]);
$c++;
}
print "@nval <br> $c";

Output: 8 Geography 9 Science 11 Maths
4

KevinADC
02-28-2007, 02:04 AM
@values = (67 , '8 Geography' , 88 , '9 Science' , 56 , '11 Maths');
@new = grep {/[a-zA-Z]/} @values;
print "@new";

FishMonger
02-28-2007, 02:31 AM
The loop fires the same for each example. The output however is even or odd, depending where you start.
my $len = @values = (67,'8 Geography',88,'9 Science',56,'11 Maths');
my $c = 1;
for (my $i=0;$i<$len-1;$i+=2) {
push(@nval, $values[$i]);
$c++;
}
print "@nval <br> $c";

Output: 67 88 56
4
------------------------------------------------------
my $len = @values = (67,'8 Geography',88,'9 Science',56,'11 Maths');
my $c = 1;
for (my $i=0;$i<$len-1;$i++) {
push(@nval, $values[++$i]);
$c++;
}
print "@nval <br> $c";

Output: 8 Geography 9 Science 11 Maths
4
Opps, I goofed. I was paying more attention to the loop control and blanked out on the preincrementing of the loop variable.

rockdoc
03-01-2007, 02:07 AM
Thanks everyone for your feedback on this.

I did eventually work out my own solution (listed below), but I see the solutions here are more efficient. I'll look at them closely & try to learn a bit more perl.

(I just converted the list to a string and then created 2 loops, starting the counters at 0 and 1 respectively, and advancing by 2.)

####get every second value in list

@values = (67,'8 Geography',88,'9 Science',56,'11 Maths');

$number = $#values + 1;

$list = join(",", @values);

##########Get percentage values
$percent_list = "";

$counter = 0;
while ($counter < $number)
{
$percent_list = $percent_list . $list[$counter] . "---";
$counter = $counter + 2;
}

##########Get subject values
$subject_list = "";

$counter = 1;
while ($counter < $number)
{
$subject_list = $subject_list . $list[$counter] . "---";
$counter = $counter + 2;
}
############

Then just split $percent_list & $subject_list at the "---"