Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Unexpected Array

by ImpalaSS (Monk)
on Nov 13, 2000 at 23:26 UTC ( #41388=perlquestion: print w/replies, xml ) Need Help??

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

hello, I have this array called @finalarray, when i print the array using print "@array"; i get this:
PPA001P_Dalton beginningChanNumber.1 1 PPA001P_Dalton beginningChanNum +ber.2 2 PPA001P_Dalton beginningChanNumber.3 3 PPA001P_Dalton beginningChanNumber.4 5 PPA001P_Dalton chanSize.1 1 PPA +001P_Dalton chanSize.2 1 PPA001P_Dalton chanSize.3 2 PPA001P_Dalton chanSize.4 6 PPA003P_HunlockCrk beginningChanNumber.1 1 + PPA003P_HunlockCrk beginningChanNumber.2 2 PPA003P_HunlockCrk beginningChanNumber.3 3 PPA003P_HunlockCrk beginnin +gChanNumber.4 9 PPA003P_HunlockCrk chanSize.1 1 PPA003P_HunlockCrk chanSize.2 1 PPA003P_HunlockCrk chanSize.3 2 PPA003 +P_HunlockCrk chanSize.4 6 PPA004P_Springtown beginningChanNumber.1 1 PPA004P_Springtown beginningChanNumber.2 2 PPA +004P_Springtown beginningChanNumber.3 3 PPA004P_Springtown beginningChanNumber.4 7 PPA004P_Springtown chanSize +.1 1 PPA004P_Springtown chanSize.2 1 PPA004P_Springtown chanSize.3 4 PPA004P_Springtown chanSize.4 6 PPA008 +P_Greenfield beginningChanNumber.1 1
only a lot longer. HOwever, when i print it using:
foreach $item(@finalarray) { print "$item \n"; } <code> I get: <br> <code> chanSize.3 2 PPA001P_Dalton beginningChanNumber.1 1 PPA001P_Dalton beginningChanNumber.2
with all kinds of unexpected values in this array, so when i want the 5th item in the array and i do a  $finalarray[5], i dont get the 5th variable, but a blank space. Any ways i can fix this?
Thanks In advance
Dipul Patel

Replies are listed 'Best First'.
Re: Unexpected Array
by Fastolfe (Vicar) on Nov 13, 2000 at 23:31 UTC
    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?

      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.

Re: Unexpected Array
by clemburg (Curate) on Nov 13, 2000 at 23:53 UTC

    You might want to take another look at the default behavior for split, maybe by typing perldoc -f split at the command line in your favorite shell.

    Until then, if it is just that you want each word from the input appearing on a separate line, try this:

    $ perl -lne 'for (split) {print}' < yourfile.txt

    Try to figure out why this does what it does. Maybe you will see then what went wrong with your program.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: Unexpected Array
by ImpalaSS (Monk) on Nov 13, 2000 at 23:43 UTC
    Thank you Very much guys. The /\s+/ is what the problem was :)
    Dipul

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://41388]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2021-09-20 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?