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

HI All, I have the following data in a csv file:

DeviceID,FreeSpace,Size C:,95342874624,476216029184 E:,2119757824,2133852160 G:,3295703040,21425549312

I use the following code to load the txt file into an array.

open(my $fh, '<', $file) or die "Could not open '$file' $!\n"; while (my $line = <$fh>) { chomp $line; my ($drive, $sizegb, $freegb) = split (/,/,$line); print qq($sizegb); $sizegb /= 1024; print qq($sizegb); }

When I run the script: The first print of size gb output: 9 5 3 4 2 8 7 4 6 2 4
The second print output: 0
I am not sure where i am going wrong with the spaces in the number of output 1 which I assume is why the division is not working. When i try to do an index for space i get -1 as the result.
Please assist.
Regards
Vishaal

Replies are listed 'Best First'.
Re: Arrays and division not working...
by poj (Abbot) on Jan 06, 2015 at 21:51 UTC
    try
    open (my $fh, '<:encoding(ucs-2le)', $file) or
    open (my $fh, '<:encoding(ucs-2be)', $file) poj

      Thanks, the following resolved my problem.
      '<:encoding(ucs-2le)' when opening the file. It now works.

Re: Arrays and division not working...
by nlwhittle (Beadle) on Jan 06, 2015 at 21:40 UTC

    I copied your code and ran it against the data you provided and it works the way you would expect on my computer. I'm not sure what's going on here, but here's the code (I added newlines to your print statements):

    #!/usr/bin/perl use strict; use warnings; my $file = "sizes"; open(my $fh, '<', $file) or die "Could not open '$file' $!\n"; while (my $line = <$fh>) { chomp $line; my ($drive, $sizegb, $freegb) = split (/,/,$line); print qq($sizegb\n); $sizegb /= 1024; print qq($sizegb\n); }

    For the data in file "sizes", I used what you provided, minus the headers. Here's the output:

    95342874624 93108276 2119757824 2070076 3295703040 3218460
    --Nick
Re: Arrays and division not working...
by pme (Monsignor) on Jan 06, 2015 at 21:42 UTC
    Hi Vishza,

    I have no idea how you got 0 for the second print. What I see is you should skip the header and append '\n' to the print statements.

    open(my $fh, '<', $file) or die "Could not open '$file' $!\n"; <$fh>; ### skip header while (my $line = <$fh>) { chomp $line; my ($drive, $sizegb, $freegb) = split (/,/,$line); print qq($sizegb\n); ### \n for new line $sizegb /= 1024; print qq($sizegb\n); ### \n for new line }
    Regards
Re: Arrays and division not working...
by Anonymous Monk on Jan 06, 2015 at 21:42 UTC

    I can't seem to reproduce the problem you describe with this code and sample input. On my machine, if I add newlines to the output, I get:

    FreeSpace 0 95342874624 93108276 2119757824 2070076 3295703040 3218460

    Does this example code exhibit the problem on your machine, or have you perhaps simplified your actual code a little bit too much and removed the code that is causing the problem?

    Note that you should be doing use warnings; use strict; at the beginning of your script; this would be telling you that Argument "FreeSpace" isn't numeric in division (/), since you can't use numeric division on the string "FreeSpace". But I don't think that would be the source of the problem you describe. In any case, you can skip the header line by doing something like my $header = <$fh>; just before the loop.