#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2

    Array blank values are avoided.


    Hi All,

    I have a CSV(abc.csv) file:
    a,b,c,,d,e,
    j,k,l,m,,,
    p,q,r,,,t,

    I wrote the perl code:

    #!/usr/bin/perl
    open FILE, "<", "abc.sv" or die;
    while($line=<FILE>)
    {
    chomp($line);
    push(@array,split/,/,$line);
    }
    $length=@array;
    print "Length of an array is $length\n";
    for($i=0;$i<$length;$i++)
    {
    print "$i, $array[$i]\t";
    }

    ##########################################
    Output I got:
    Length of an array is 16
    0,a 1,b 2,c 3, 4,d 5,e 6,j 7,k 8,l 9,m 10,p 11,q 12,r, 13, 14, 15,t
    ##########################################
    Now the question is,
    why blank spaces of 3rd , 13th and 14th position are printed but not the blank spaces after "m" character in the second line of csv file.
    Why they are avoided?
    And what if I want to print them and take them in counting?
    Can you please help me?
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,221
    Rep Power
    1809
    The answer is in the documentation for split:

    If LIMIT is omitted (or, equivalently, zero), then it is usually treated as if it were instead negative but with the exception that trailing empty fields are stripped (empty leading fields are always preserved)
    It's just a default value. You can set the number of fields to split (7 in this case), or set the LIMIT value to -1 and you won't have to count. It will preserve the trailing empty fields.


    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my @array;
    
    while (<DATA>) {
    	chomp;
    	push @array, [split/,/, $_, 7];
    }
    
    my $length = @array;
    print "Length of array is $length\n";
    
    foreach my $row (@array) {
    	print Dumper $row;
    }
    
    __DATA__
    a,b,c,,d,e,
    j,k,l,m,,,
    p,q,r,,,t,
    Result:
    Code:
    Length of an array is 3
    $VAR1 = [
              'a',
              'b',
              'c',
              '',
              'd',
              'e',
              ''
            ];
    $VAR1 = [
              'j',
              'k',
              'l',
              'm',
              '',
              '',
              ''
            ];
    $VAR1 = [
              'p',
              'q',
              'r',
              '',
              '',
              't',
              ''
            ];

    Comments on this post

    • bonny1989 agrees : Thanks. :)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by keath
    The answer is in the documentation for split:



    It's just a default value. You can set the number of fields to split (7 in this case), or set the LIMIT value to -1 and you won't have to count. It will preserve the trailing empty fields.


    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my @array;
    
    while (<DATA>) {
    	chomp;
    	push @array, [split/,/, $_, 7];
    }
    
    my $length = @array;
    print "Length of array is $length\n";
    
    foreach my $row (@array) {
    	print Dumper $row;
    }
    
    __DATA__
    a,b,c,,d,e,
    j,k,l,m,,,
    p,q,r,,,t,
    Result:
    Code:
    Length of an array is 3
    $VAR1 = [
              'a',
              'b',
              'c',
              '',
              'd',
              'e',
              ''
            ];
    $VAR1 = [
              'j',
              'k',
              'l',
              'm',
              '',
              '',
              ''
            ];
    $VAR1 = [
              'p',
              'q',
              'r',
              '',
              '',
              't',
              ''
            ];

    oh thank you so much for your kind help!!!!

    now can you please help me in:
    _________________________________________________________

    I have one csv file (prac.csv):
    (Which has hexadecimal numbers init)
    a,b,,,
    c,x,,,
    e,r,0x0,,
    ,,0x10,u,
    q,w,0x0,u,
    #####################################################
    Now i wan to take input of prac.csv
    and wan to print its hexadecimal value....

    i.e.
    In first line hex value=,
    In second line hex value=,
    In third line hex value=0x0,
    In fourth line hex value=0x10,
    In fifth line hex value=0x0,

    ####################################################3

    Can you please help me???
  6. #4
  7. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,221
    Rep Power
    1809
    It's more difficult to give help when your file examples are not truly representative.

    In this example, the hex data always seems to appear in the third column. You don't seem interested in the other columns.

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    while (<DATA>) {
    	my $hex = (split/,/, $_, -1)[2];
    	print "$.: $hex\n";
    }
    
    __DATA__
    a,b,,,
    c,x,,,
    e,r,0x0,,
    ,,0x10,u,
    q,w,0x0,u,
    But I don't usually throw away data. If I'm parsing a file, I usually have some interest in all of it, so I'm more likely to do the following:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my @fields = qw/first second hex fourth always_empty/;
    
    while (<DATA>) {
    	chomp;
    	my %row;
    	@row{@fields} = split/,/, $_, -1;
    	#print Dumper \%row;
    	print "$.: $row{'hex'}\n";
    }
    
    __DATA__
    a,b,,,
    c,x,,,
    e,r,0x0,,
    ,,0x10,u,
    q,w,0x0,u,
    I've labelled all the data and stored it in a temp hash. I'd usually give the fields better names, but your data is not clear so that's the best I can do.

    Even though I may only be interested in the hex at this moment, the other data may be of value at some point, and is now easily accessible.


    Edit:

    And I should add that since you have asked several questions about CSV files, be aware that you should use the Text::CSV module to parse actual CSV files. Your examples have all been really basic single letter and number values separated by commas, but more complicated CSV files have quotation marks to encase fields containing commas, such as the number 1,000. In those situations you can't simply split; you need to use the module.
    Last edited by keath; April 15th, 2013 at 06:55 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    Hi Bonnie,

    I gave you a possible solution on the other post that you opened on the same subject.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Sorry Keath for not explaining you clearly but thank you I got the idea from your code and yes Laurent_R i saw that. Thank you to you too.

IMN logo majestic logo threadwatch logo seochat tools logo