in reply to Unexpected Array

Looks like your code is filling @finalarray with a frequent undef. When you print it via "@finalarray", the array is stringified, which joins each defined element of the array separated by a space (actually $"). Thus, if you iterate through the list, you'll pick up the undefined values, but if you just print it in a way that interpolates it, those undefined values will be ignored.

Check your code to be sure you're building your array correctly.. Check your assumptions. What happens if one of your tests fails? Are you still storing a value?

Replies are listed 'Best First'.
RE: Re: Unexpected Array
by ImpalaSS (Monk) on Nov 13, 2000 at 23:33 UTC
    here is the code
    $file = "/usr/local/rf-north/cgi-bin/chandata.txt"; open (FH, $file) || die "Unable to open file $file\n"; @data = <FH>; close (FH); foreach $item (@data){ @line = split (/ /, $item); push(@finalarray, @line); }

    Does this help?>
    Thanks Again
    Dipul
      It would help if we could see the contents of a sample line. I suspect it looks like this:
      some stuff some more stuff
      with a bunch of spaces. You are splitting on / /, which is a single space, which means you are going to get a lot of empty elements in your resulting list. You probably want to change your split statement to split on /\s+/, which will catch all whitespace between elements.
      It seems to me that you want to create an array containing the words of the file. If you split on space like you have done split / /, $item, then you will only split on one space. I think you want to split on:
      split /\s+/, $item
      or just:
      split ' ', $item
      The last one will strip all blanks at the start as well as on the end of the string.

      Autark

      Fastolfe is in all likelyhood completely correct.

      Don't forget that $item will have a newline at the end of it. That's not the problem here, but it may not be what you intend.

      Also, if chandata.txt gets very large, you're holding it in memory twice. You may want to loop through the file line by line and just extract the info you want.