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

Everytime I run this script it outputs only half of the information from the input file. (Skips every other "block"). I have included the input file and code below. I know I must be doing something wrong but I don't know what.
my $curpos = -1; my $curpos1 = -1; my $curpos2 = -1; my $curpos3 = -1; my $cur = -1; open (QUOTAOUT, ">D:\\Home.txt") || die "Can't open output file."; $file = "Home01.txt"; chomp ($file); open (QUOTA1, "D:\\Home01.txt") || die "Can't open file."; while (defined ($file = <QUOTA1>)) { $quota = <QUOTA1>; next if ($quota eq ""); @quota = split(/\t/, $quota); @quota[1] =~ s/^\://; @quota[1] =~ s/^\s+//; next if (@quota[0] eq "Used(%)"); if (@quota[0] eq "Object") { @quota[1] =~ tr/A-Z/a-z/; @uidt = split(/\\/, @quota[1]); chomp (@uid[2]); @uidt[2] =~ s/\W.*//; $curpos = $curpos + 1; $curpos3 = $curpos3 + 1; @uid[$curpos] = @uidt[2]; @path[$curpos3] = @quota[1]; next; } if (@quota[0] eq "Quota") { @quotast = split(/\s/, @quota[1]); chomp (@quotast[0]); @quotast[0] =~ s/\W.*//; $curpos1 = $curpos1 + 1; @quotas[$curpos1] = @quotast[0]; next; } if (@quota[0] eq "Current") { @usaget = split(/\s/, @quota[1]); chomp (@usaget[0]); @usaget[0] =~ s/\W.*//; $curpos2 = $curpos2 + 1; @usage[$curpos2] = @usaget[0]; next; } } close QUOTA1; chomp (@path); foreach $uid(@uid) { $cur = $cur + 1; print QUOTAOUT "Usage for $uid is @usage[$cur] bytes. Quota is @quotas[$cur] bytes. Server is lnxdayhome01. Path is @path[$ +cur]\n";
}
Begin Input File: Object : T:\HD1\UDENTJP Quota : 250000000 Bytes Current : 0 Bytes Used(%) : 0% Object : T:\HD1\UDEPAML Quota : 250000000 Bytes Current : 336821 Bytes Used(%) : % Object : T:\HD1\UDERRTX Quota : 250000000 Bytes Current : 70095734 Bytes Used(%) : 28% Object : T:\HD1\UDESCJT Quota : 250000000 Bytes Current : 81945 Bytes Used(%) : % Object : T:\HD1\UDETELX Quota : 250000000 Bytes Current : 229442791 Bytes Used(%) : 92% Object : T:\HD1\UDEUTGB Quota : 250000000 Bytes Current : 81945 Bytes Used(%) : % Object : T:\HD1\UDEVIR1 Quota : 250000000 Bytes Current : 0 Bytes Used(%) : 0% Object : T:\HD1\UDIDLRX Quota : 250000000 Bytes Current : 140305441 Bytes Used(%) : 56% Object : T:\HD1\UDIETRD Quota : 250000000 Bytes Current : 53772515 Bytes Used(%) : 22% Object : T:\HD1\UDIFRBX Quota : 250000000 Bytes Current : 63196422 Bytes Used(%) : 25% Object : T:\HD1\UDIGGSX Quota : 250000000 Bytes Current : 32722470 Bytes Used(%) : 13% Object : T:\HD1\UDILLBX Quota : 250000000 Bytes Current : 98329 Bytes Used(%) : % Object : T:\HD1\UDILLJ1 Quota : 250000000 Bytes Current : 111969540 Bytes Used(%) : 45% Object : T:\HD1\UDINELM Quota : 250000000 Bytes Current : 23118920 Bytes Used(%) : 9% Object : t:\HD1\UDIXOA1 Quota : 250000000 Bytes Current : 0 Bytes Used(%) : 0% Object : T:\HD1\UDIXOMD Quota : 250000000 Bytes Current : 4518573 Bytes Used(%) : 2%
End Input File

Thanks

Replies are listed 'Best First'.
Re: Skipping lines
by jsprat (Curate) on Jul 15, 2002 at 21:04 UTC
    while (defined ($file = <QUOTA1>)) { $quota = <QUOTA1>;

    $file gets one line, $quota gets the next. And you never use the line in $file

    Pretty common mistake - one of Perl's Seven Deadly Sins.

Re: Skipping lines
by chromatic (Archbishop) on Jul 15, 2002 at 21:04 UTC

    Remember the phrase, "You can't step twice in the same stream." You're reading from QUOTA twice for each loop iteration, once into $file and once into $quota. while has special magic to do the defined test for you, so you can skip that check.

Re: Skipping lines
by talexb (Chancellor) on Jul 15, 2002 at 21:27 UTC
    This code snippet is not pretty. :)

    If you had used strict then you would have found that there are a pile of errors that need to be fixed. Here's what I would recommend: Start over with a clean sheet of paper and write down what the code's supposed to do.

    A wild guess is that it's supposed to check each Object and see if the percent Used matches the Current divided by the Quota. Start with that and go from there.

    --t. alex

    "Mud, mud, glorious mud. Nothing quite like it for cooling the blood!" --Michael Flanders and Donald Swann

Re: Skipping lines
by boo_radley (Parson) on Jul 15, 2002 at 21:47 UTC
    here's a short solution that changes the input record separator to 2 newlines, one of my favorite tricks. I also wanted to see if I could get each variable populated with only one split which turned out to be easier than I thought.
    use strict; use warnings; { local $/="\n\n"; while (<DATA>) { my (undef, $o, undef, $q, undef, $c, undef, $u) = split /\n|( +?: : )/, $_; print "Usage for object is $u bytes. Quota is $q bytes. Serve +r is lnxdayhome01. Path is $o\n"; } } __DATA__ Object : T:\HD1\UDENTJP Quota : 250000000 Bytes Current : 0 Bytes Used(%) : 0%

      No point in grabbing all those values from split and then throwing them away (with undef)-

      my ($o, $q, $c, $u) = (split /\n|(?: : )/, $_)[1,3,5,7];

      --
      Until you've lost your reputation, you never realize what a burden it was or what freedom really is. -Margaret Mitchell

        Thanks all. I removed the line $quota = <QUOTA1>; and lo
        and behold it worked! (I knew I was doing something wrong.)