<8----- use strict; use warnings; my @GridMulti; # Multi dimensional array: playing field my $GM_Height = 0; # Height of terrain in units above 0 my $MaxX = 80; # Number of field columns my $MaxY = 70; # Number of field rows my $MaxFields = (($MaxX + 1) * ($MaxY + 1)) - 1; # Number of fields my @CheckArray1; # First Check Array my @CheckArray2; # Second Check Array my $Differences = 0; # Number of differences #Reading loop. Using rand here to prevent a extremely long post for my $xiter (0 .. $MaxX) { for my $yiter (0 .. $MaxY) { $GridMulti[$xiter][$yiter][$GM_Height] = int (rand 256); push @CheckArray1, $GridMulti[$xiter][$yiter][$GM_Height]; } } #CheckLoop for my $xiter (0 .. $MaxX) { for my $yiter (0 .. $MaxY) { push @CheckArray2, $GridMulti[$xiter][$yiter][$GM_Height]; } } for my $iter (0 .. $MaxFields) { if ($CheckArray1[$iter] != $CheckArray2[$iter]) { $Differences++; } } print "\n$Differences differences\n"; [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => <4087964D.2050608@thepierianspring.org> [ref] => <4087A478.18574.7F3DCA@localhost> [htmlstate] => on_nl2br [postusername] => Randy [ip] => ml-perl@thepier [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 2 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> Values in array may change - PERL Beginners
Professional Web Applications Themes

Values in array may change - PERL Beginners

Every now and again I like to program for fun. In this case it is a simulation. The basis is a grid that holds things like altitude, pieces and things like that. To hold all data in a way I can remember everything easily, I chose to put all in a multi dimensional array. If there is a better way, please do tell. Loading the grid from file gave me a surprise. The values in the array do not seem to be static. The change seems to occur after leaving the Reading loop. In this loop values do not change, ...

  1. #1

    Default Values in array may change

    Every now and again I like to program for fun. In this case it is a
    simulation. The basis is a grid that holds things like altitude,
    pieces and things like that. To hold all data in a way I can remember
    everything easily, I chose to put all in a multi dimensional array.
    If there is a better way, please do tell. Loading the grid from file
    gave me a surprise. The values in the array do not seem to be static.
    The change seems to occur after leaving the Reading loop. In this
    loop values do not change, calling them after the loop some values
    will have changed in a regular manner. References give similar
    results

    Executing the program below gives a lot of differences. There is a
    discernable patern in where those differences occur which leads me to
    believe that perl is not at fault here, but me. (As I have sufficient
    trust in my programming abillities, that would have been the logical
    conclusion anyway.) Especially as another perl version on a linux
    machine exhibits the same behaviour. The Perl verion used here is
    v5.8.1 built for MSWin32-x86-multi-thread

    Question: what am I doing wrong here?


    my GridMulti; # Multi dimensional array: playing field
    my $GM_Height = 0; # Height of terrain in units above 0
    my $MaxX = 80; # Number of field columns
    my $MaxY = 70; # Number of field rows
    my $MaxFields = (($MaxX + 1) * ($MaxY + 1)) - 1;
    # Number of fields

    my CheckArray1; # First Check Array
    my CheckArray2; # Second Check Array

    my $Differences = 0; # Number of differences


    #Reading loop. Using rand here to prevent a extremely long post
    for my $xiter (0 .. $MaxX) {
    for my $yiter (0 .. $MaxY) {
    $GridMulti[$xiter . $yiter . $GM_Height] = int (rand 256);
    push CheckArray1, $GridMulti[$xiter . $yiter . $GM_Height];
    }
    }

    #CheckLoop
    for my $xiter (0 .. $MaxX) {
    for my $yiter (0 .. $MaxY) {
    push CheckArray2, $GridMulti[$xiter . $yiter . $GM_Height];
    }
    }

    for my $iter (0 .. $MaxFields) {
    if ($CheckArray1[$iter] != $CheckArray2[$iter]) {
    $Differences++;
    }
    }

    print "\n$Differences differences\n";
    Niek Guest

  2. #2

    Default Re: Values in array may change

    Niek Oosterlaak wrote: 

    For multidimensional arrays, the preferred syntax is:

    my a;
    $a[$x][$y][$z] = 'value';

    see perldoc perllol

    corrected code below.

    Randy.

    -----><8-----
    use strict;
    use warnings;

    my GridMulti; # Multi dimensional array: playing field
    my $GM_Height = 0; # Height of terrain in units above 0
    my $MaxX = 80; # Number of field columns
    my $MaxY = 70; # Number of field rows
    my $MaxFields = (($MaxX + 1) * ($MaxY + 1)) - 1;
    # Number of fields

    my CheckArray1; # First Check Array
    my CheckArray2; # Second Check Array

    my $Differences = 0; # Number of differences


    #Reading loop. Using rand here to prevent a extremely long post
    for my $xiter (0 .. $MaxX) {
    for my $yiter (0 .. $MaxY) {
    $GridMulti[$xiter][$yiter][$GM_Height] = int (rand 256);
    push CheckArray1, $GridMulti[$xiter][$yiter][$GM_Height];
    }
    }

    #CheckLoop
    for my $xiter (0 .. $MaxX) {
    for my $yiter (0 .. $MaxY) {
    push CheckArray2, $GridMulti[$xiter][$yiter][$GM_Height];
    }
    }

    for my $iter (0 .. $MaxFields) {
    if ($CheckArray1[$iter] != $CheckArray2[$iter]) {
    $Differences++;
    }
    }

    print "\n$Differences differences\n";


    Randy Guest

  3. #3

    Default Re: Values in array may change

    Niek Oosterlaak wrote: 

    Well, you didn't describe the pattern but on my computer the anomaly
    shows up when $xiter is in the range 0 to 7 AND $yiter is in the range
    10 to 70. This looks like a bug in perl. BTW I am running Perl 5.6.0
    on Linux.

     

    You say that you are using a multi-dimensional array but that is not it,
    you are concatenating the numbers together in a single dimensional
    array. If you do use a multi-dimensional array it will solve your
    problem:

    $GridMulti[ $xiter ][ $yiter ][ $GM_Height ] = int (rand 256);
    push CheckArray1, $GridMulti[ $xiter ][ $yiter ][ $GM_Height ];

     

    push CheckArray2, $GridMulti[ $xiter ][ $yiter ][ $GM_Height ];

     

    You don't really need the $MaxFields variable:

    for my $iter ( 0 .. $#CheckArray1 ) {

     



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  4. #4

    Default Re: Values in array may change

    Randy W. Sims wrote: 
    >
    > For multidimensional arrays, the preferred syntax is:
    >
    > my a;
    > $a[$x][$y][$z] = 'value';
    >
    > see perldoc perllol[/ref]
    Thanks. Works as expected. Sorry for asking something I should have
    looked up. Another programming language's syntax interfered.

    Regards,
    Niek.
    Niek Guest

Similar Threads

  1. concatenating values in an array
    By roncfusion in forum Coldfusion - Getting Started
    Replies: 3
    Last Post: November 30th, 07:30 PM
  2. insert values in an array
    By Rob in forum PHP Development
    Replies: 2
    Last Post: February 5th, 07:14 PM
  3. Deleting same values from an array
    By Stephen Craton in forum PHP Development
    Replies: 2
    Last Post: August 23rd, 12:04 AM
  4. asp: values in array not in order??
    By Sue Adams in forum ASP
    Replies: 1
    Last Post: August 16th, 06:49 PM
  5. Random Array values
    By mike in forum PHP Development
    Replies: 0
    Last Post: July 14th, 03:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139