Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i get a syntax error when i define $ispresent can u see any obvious errors? i've only programmed in ADA so i'm sure there is a better way to do this but i'm learning perl and i'm trying to keep it simple.
$firstrandom = $list1[rand @list1]; push @list2, $random; $count = $#list1 while ($count != 0){ ###create list2 $random = $list1[rand @list1]; $ispresent = 'false'; for $I (0..$#list2){##check to see if team is already in list2 if ($random == $list2[$I]) { $ispresent = 'true'}; }; if ($ispresent =='false') { ##if not add it to @list2 push @list2, $random; $count--; }; };
i also have a syntax error problem with the qw($round $teamname $score); part. i'm trying to recreate the entire returned record set as a data structure so that i can print a competition tree with the variables from @list_of_teamdata refered to as eg.
$list_of_teamdata[7][1]
and print them where i like.
@row; $rowcount = 0; while (@row = $sth5->fetchrow_array()) { $round = $row[0]; $teamname = $row[1]; ## need to put each row into the list. $Score = $row[2]; of lists a variable at a time push (${list_of_teamdata[$rowcount]}, qw($round $teamname $score); $rowcount++; }
i'd really appreciate it if you could help

Replies are listed 'Best First'.
(ar0n) Re: syntax issues
by ar0n (Priest) on Mar 29, 2001 at 00:57 UTC
    Missing semicolon in line 3:
    $count = $#list1
    as for question two:

    Update: Doh! Give me some time. I'm confused.

    Update 2:
    while (my @row = $sth5->fetchrow_array()) { push @list_of_teamdata, [ @row[0..2] ]; }
    This pushes a reference to a copy of @row onto the array. You don't need to first declare variables of elements in the array, since you appeared to be pushing them on in the same order as they were in @row.

    Oh, and the 's' in $Score is a capital whereas the 's' in the $score you were trying to push onto the array was not :-)

    You may want to re-read perlref...

    This is of course all untested, standard disclaimer holds...

    [ ar0n, a confused little boy ]

      my data structure has to have the format of
      team1 score team2 score team3 score etc
      so i end up with a team name and a score in each element of  @list_of_teamdata so that when i reference the score in row 0 i can use the step.  @list_of_teamdata[0][0]
Re: syntax issues
by gregw (Beadle) on Mar 29, 2001 at 02:45 UTC
    The statement:
    if ($ispresent =='false') {
    should employ the 'eq' string comparison operator rather than the '==' numeric comparison operator, like so:
    if ($ispresent eq 'false') {
    This is one of those little 'top-10-things-to-check-for' perl quirks.
Re: syntax issues
by Vondikall (Initiate) on Mar 29, 2001 at 16:55 UTC
    1. You are pushing an undefined value $random instead of $firstrandom
    2. You are checking for string equality with == instead of cmp
    3. There was a semicolon missing.
    properly:
    $firstrandom = $list1[rand @list1]; push @list2, $firstrandom; $count = $#list1; while ($count != 0) { ###create list2 $random = $list1[rand @list1]; $ispresent = 'false'; for $I (0..$#list2){ ##check to see if team is already in lis +t2 if ($random == $list2[$I]) { $ispresent = 'true' }; }; if($ispresent eq 'false') { ##if not add it to @list2 push @list2, $random; $count--; }; };

    You are building a randomized version of @list1. The most efficient way of randomly shaking up a list like this is the Fisher-Yates shuffle:

    Fisher-Yates (from the Perl Cookbook from O'Reilly):

    # fisher_yates_shuffle( \@array ) : generate a random permutation # of @array in place sub fisher_yates_shuffle { my $array = shift; my $i; for ($i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i,$j] = @$array[$j,$i]; } } fisher_yates_shuffle( \@array ); # permutes @array in place