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

How would you test if an element exists? I have a file that is read in and splits the line up into and array based on commas. Im looking for the data that exists in the 6th element. Some lines are blank or just has a comma. How would I have the script check to see if the 6th element exists before it processes the line? Here's my code below and also a copy of what the file looks like:
heres what the file looks like: 20060819,02159,045488061,00 ,0000118,0000000,0000000,0000118,0000000,0 +000000,Zachary ,G 20060819,02159,045775830,00 ,0000649,0000028,0000000,0000649,0000028,0 +000000, , 20060819,02161,023416381,09 ,0000034,0000000,0000000,0000034,0000000,0 +000000,Erinne ,H Here's the code I have: while ($line = <OUTF>) { my @array = split(/,/ , $line); $test = @array; print "$array[5] offers and $test is number of elements\n"; if ($array[5] =~ /0000000/ || $array[5] =~ //) { # the 2nd part is + obviously wrong print "$store_number is not BCE Store\n"; } else { $store_number = $array[1]; print "Store # $store_number\n"; push(@st_n, $store_number); } }
I need it to capture the line with just the comma, or if its a blank line. Let me know if you have any questions.... Thanks, Bob

Replies are listed 'Best First'.
Re: Array element exist
by GrandFather (Saint) on Aug 21, 2006 at 23:06 UTC

    I'm not quite sure what you are after, but this may get you going. Note the technique of including data in a __DATA__ section in the script to facilitate testing - especially by other people.

    use strict; use warnings; my @st_n; while (defined (my $line = <DATA>)) { my @array = split(/,/ , $line); my $test = @array; next if $test < 6; # Skip lines that don't have at least 6 fields my $store_number = $array[1]; print "$array[5] offers and $test is number of elements\n"; if ($array[5] =~ /0000000/ || $array[5] =~ //) { # the 2nd part is + obviously wrong print "$store_number is not BCE Store\n"; } else { print "Store # $store_number\n"; push(@st_n, $store_number); } } __DATA__ 20060819,02159,045488061,00 ,0000118,0000000,0000000,0000118,0000000,0 +000000,Zachary ,G 20060819,02159,045775830,00 ,0000649,0000028,0000000,0000649,0000028,0 +000000, , 20060819,02161,023416381,09 ,0000034,0000000,0000000,0000034,0000000,0 +000000,Erinne ,H

    Prints:

    0000000 offers and 12 is number of elements 02159 is not BCE Store 0000028 offers and 11 is number of elements Store # 02159 0000000 offers and 12 is number of elements 02161 is not BCE Store

    DWIM is Perl's answer to Gödel
      Thanks for the help... It does exactily what Im looking for... -Bob
Re: Array element exist
by graff (Chancellor) on Aug 22, 2006 at 04:01 UTC
    Looking at your sample data, it seems like there may be newline characters embedded within some of the fields -- this would explain how some lines begin with a comma. If that's the issue you are trying to cope with, someone else just posted a very similar question a little while ago: Clean data - where field contains a CRLF

    If you know there should always be 12 comma-separated values in a row of data, you may need to concatenate two consecutive lines in order to get a complete row, which was the topic of that other thread.

    (I thought about recommending a module such as Text::xSV, but your data file would need to be edited in order to conform to the formatting constraints for "standard" CSV files -- there would need to be double quotes around any field that contains a newline.)