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

Hi, I am reading in a tab seperated file, taking the first field (a string of digits), and I am then splitting that string into an array, so I can address each digit seperately. The wierd thing is, although the digits are all showing up fine when I print the entire line from the file, some of the arrays are missing data, so that when I go through the array matching the constants to other variables, suddenly the entry I am looking at contains absolutely nothing. I cannot figure this out. It is not happenning all the time and I do not touch the array between the split and the debug example. Does anyone have any ideas?
while(<FILE>){ my @line = split (/\t/, $_); my @fileid = split(//, $line[0]); .... print "$_"; ##Shows e.g. 1159485660 cindy foreach my $fid(@fileid){ print "$fid "; ##Shows e.g. 1 1 9 4 8 5 6 0 } }

Replies are listed 'Best First'.
Re: Splitting a string is giving me gaps in my array
by dragonchild (Archbishop) on Jan 07, 2005 at 15:19 UTC
    1. Does it happen with certain lines every time? If so, what are those lines?
    2. Have you tried changing your print line to print "'$fid' ";
    3. Why are you parsing tab-separated values yourself? Use Text::xSV

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      1) Yes, it does, e.g.
      110948691 barbie File: 1 1 9 4 8 6 9 1 1115211490 gerald File: 1 1 1 2 1 1 4 9 0 1118091120 twinkie File: 1 1 1 0 9 1 1 2 0

      2) The print line is just my debggung, to make the example easier to read. I am doing a load of stuff with the data, and none of it is coming out!

      3) I have always done it that way ;-)

      Thanks, Jennifer

        1. If that is the case, reduce your code and data to the smallest possible example that still demonstrates the problem. At that point, the solution should be obvious.
        2. It looks to me as if your ...... section is modifying @fileid.
        3. Just cause you always did it that way doesn't mean you should continue to do so. If everyone took that attitude, progress would stop cause no-one would ever improve their methods. :-)

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Splitting a string is giving me gaps in my array
by mr_mischief (Monsignor) on Jan 07, 2005 at 15:31 UTC
    Are you absolutely sure there are never multiple tabs? Have you tried /\t+/ instead of /\t/ ?


    Christopher E. Stith
      It isn't multiple tabs, because the rest of the data is in there - all the digits are from the same field.
Re: Splitting a string is giving me gaps in my array
by holli (Abbot) on Jan 07, 2005 at 16:16 UTC
    what happens if you replace
    foreach my $fid(@fileid) { print "$fid "; ##Shows e.g. 1 1 9 4 8 5 6 0 }
    with
    for (my $i=0; $i<length($line[0]); $i++) { $fid = substr($line[0], $i, 1); print "$fid "; }
    same error?