Gossamer Forum
Home : General : Perl Programming :

Array count...

Quote Reply
Array count...
Hi. I've been playing around with this code since yesterday afternoon, and it still isn't working. It should be just simple stuff, but its just returning no value at all.

Code:
my @count_slice = split(/\n/, $wanted);
my $match = 0;
if ($#count_slice > 500) {
$match = 1;
}

my $track = 0;
foreach (@count_slice) { $track++; }
my $track = 0;

return $track;

I'm verifying that there is no entries in the array being returned by doing the counting two times (with a foreach, and $#), but why?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Array count... In reply to
Quote:
I'm verifying that there is no entries in the array being returned by doing the counting two times (with a foreach, and $#),

Your code confuses me. $match is never used and also you have:

my $track = 0;

...just before you return so $track will always be 0.

On another note, you are going to far too much trouble to count the elements in the array.

Simply use:

my $count = @count_slice;
Quote Reply
Re: [Paul] Array count... In reply to
That is only part of the routine. It does a lot more after that...where $match is found (only runs certain checks and alterations; the same with $track.

>>>my $count = @count_slice; <<<

Erm, hows that gonna work? I just tried it, and all it did was print out the contents of the array Wink

I just tried the basic version of the method I'm currently using, and came up with something like;

Code:
#!perl

use strict;
print "Content-type: text/html \n\n";

my @count_sliced = qw(1 2 3 4);
print $#count_sliced + 1;

I don't see why the array is not being populated by the split() function...that seems to be the main problem Unimpressed

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Array count... In reply to
Quote:
Erm, hows that gonna work? I just tried it, and all it did was print out the contents of the array

You probably did something wrong, because it works =)

You probably used:

my ($count) = @count_slice;

...which is different from:

my $count = @count_slice;

As for the main error, the only reason for the array being empty is if $wanted was empty. Even if there were no newlines in the string the array would still contain one element.
Quote Reply
Re: [Paul] Array count... In reply to
Mmmm...I'm really not sure what the error was now. I re-wrote the code, and it all seems to be working ok now Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!