2D array values got replaced error

Discussion in 'Perl Misc' started by Ken Soon, Apr 25, 2007.

  1. Ken Soon

    Ken Soon Guest

    Hmm I go through a list
    -7.9 2.1 F
    -7.4 2.1 F
    -6.9 2.1 F
    -6.4 2.1 F
    -5.9 2.1 P
    -7.9 2.2 F
    -7.4 2.2 F
    -6.9 2.2 F
    -6.4 2.2 F
    -5.9 2.2 P
    And i use the first and 2nd value as counters for my 2D array and third one
    as the value
    Storing using the following code: (I have cut away most codes for simplicity
    sake)
    while (<file1>)
    {
    if ($_ =~ /Site/)
    {
    @arr = split(/\s+/,$_);
    $var1 = $arr[6];
    $var2 = $arr[7];
    $a[$var1][$var2] = $arr[9];
    }
    }

    open (report, ">C://perl//report.txt") or die ("File Invalid");

    Printing with the following code ($lastv is 2.2, $firstv is 2.1, $lasti
    is -5.9, $firsti is -7.9)
    for ($i=$lastv;$i>=$firstv ;$i=$i-0.1)
    {
    print $a[-0.01][2.2];
    for ($j=$firsti;$j<=$lasti;$j=$j+0.5)
    {
    print report "$a[$j][$i],";
    }
    }
    close report;

    However, I got a matrix of just P printed out
    And if I changed the last value from a P to an F, I get the matrix of F. It
    is as though the last value assigned will replace all the values in the
    array.
    is there something I should be careful of when storing values in a 2D array?
    Ken Soon, Apr 25, 2007
    #1
    1. Advertising

  2. Ken Soon

    -berlin.de Guest

    Ken Soon <> wrote in comp.lang.perl.misc:
    > Hmm I go through a list
    > -7.9 2.1 F
    > -7.4 2.1 F
    > -6.9 2.1 F
    > -6.4 2.1 F
    > -5.9 2.1 P
    > -7.9 2.2 F
    > -7.4 2.2 F
    > -6.9 2.2 F
    > -6.4 2.2 F
    > -5.9 2.2 P
    > And i use the first and 2nd value as counters for my 2D array and third one
    > as the value


    You say counters (index is the common term), but the values are
    not natural numbers. Arrays (in Perl and elsewhere) take integer
    indices. A float point number is first converted to an integer.
    Also, negative indices are special (they count from the end of the
    array).

    > Storing using the following code: (I have cut away most codes for simplicity
    > sake)
    > while (<file1>)
    > {
    > if ($_ =~ /Site/)
    > {
    > @arr = split(/\s+/,$_);
    > $var1 = $arr[6];
    > $var2 = $arr[7];
    > $a[$var1][$var2] = $arr[9];
    > }
    > }


    [more code with similar problems snipped]

    That won't do what you want. Use a hash %a instead of the array @a.

    Anno
    -berlin.de, Apr 25, 2007
    #2
    1. Advertising

  3. Ken Soon

    Ken Soon Guest


    > You say counters (index is the common term), but the values are
    > not natural numbers. Arrays (in Perl and elsewhere) take integer
    > indices. A float point number is first converted to an integer.
    > Also, negative indices are special (they count from the end of the
    > array).
    >
    >> Storing using the following code: (I have cut away most codes for
    >> simplicity
    >> sake)
    >> while (<file1>)
    >> {
    >> if ($_ =~ /Site/)
    >> {
    >> @arr = split(/\s+/,$_);
    >> $var1 = $arr[6];
    >> $var2 = $arr[7];
    >> $a[$var1][$var2] = $arr[9];
    >> }
    >> }

    >
    > [more code with similar problems snipped]
    >
    > That won't do what you want. Use a hash %a instead of the array @a.
    >
    > Anno


    Thks, changed to hash. A little bit more complex but it works :)
    Ken Soon, May 2, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasool
    Replies:
    3
    Views:
    524
    mikeb
    Mar 3, 2004
  2. moondaddy
    Replies:
    1
    Views:
    1,776
    moondaddy
    May 4, 2004
  3. tosh
    Replies:
    2
    Views:
    979
  4. Shawn Modersohn

    replaced vs non replaced

    Shawn Modersohn, Jul 19, 2004, in forum: HTML
    Replies:
    4
    Views:
    974
    Shawn Modersohn
    Jul 20, 2004
  5. Phoe6
    Replies:
    0
    Views:
    242
    Phoe6
    Dec 2, 2006
Loading...

Share This Page