Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

adding to an array from foreach loop

by flieckster (Scribe)
on Jan 10, 2018 at 20:34 UTC ( #1207090=perlquestion: print w/replies, xml ) Need Help??

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

Hi, i have a litle script that opens a txt doc, and looks into each row, and counts the files. It then prints out each rows name, and the count, and it that works fine. but i want to total the count of all the rows and give me the total at the bottom. I tired using
push (@total, $fileslistcount); $total = @total;)
to push all the arrays into one sum, but its adding up the total rows of the txt doc, not the sum total of the file counts. how do add the totals to one array and save it so it only prints out once after the loop?
chdir($desktop_dir) or mail ($to = $dllist, $from = $dllist, $subject= + "ERROR $desktop_dir $!", $body ="ERROR $desktop_dir $!" ); (@listofdir) = glob "*txt"; foreach my $file (@listofdir){ chdir($desktop_dir) or mail ($to = $dllist, $from = $dllist, $subject= + "ERROR $desktop_dir $!", $body ="ERROR $desktop_dir $!" ); open(my $fh, '<:encoding(UTF-8)', $file)or mail ($to = $dllist, $from += $dllist, $subject= "error opening $file $!", $body ="ERROR opening + to $file $!" ); open FILE, ">> $mastercount" or mail ($to = $dllist, $from = $dllist, + $subject= "ERROR opening $mastercount", $body ="ERROR opening $mast +ercount" ); while (my $row = <$fh>) {#2 chomp $row; foreach ($row) { chdir ($row)or mail ($to = $dllist, $from = $dllist, $subject= + "error opening $row", $body ="ERROR opening to $row" ); if ( -e "Thumbs.db") { unlink ("Thumbs.db") or print "thumbs.db $!\n"; }; (@fileslist) = glob "*{jpg,gif,psd,png,tif,tiff}"; my $fileslistcount = @fileslist; push (@total, $fileslistcount); $total = @total; if ($fileslistcount > 0){ print "total files in $row:$fileslistcount\n"; } else { # print "not enought files to count\n"; } } } print "total files in $total\n"; }; close FILE;

Replies are listed 'Best First'.
Re: adding to an array from foreach loop
by poj (Abbot) on Jan 10, 2018 at 21:43 UTC
    #push (@total, $fileslistcount); #$total = @total; $total += $fileslistcount;
      well that was simple. thanks!
Re: adding to an array from foreach loop
by Laurent_R (Canon) on Jan 11, 2018 at 00:10 UTC
    As a side note:
    # ... else { # print "not enought files to count\n"; } } } print "total files in $total\n"; };
    Gee, this really looks bad.

    Formatting and especially indenting your code correctly is not optional, it is a necessity. The compiler might not care, but the human reader does.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1207090]
Approved by stevieb
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2022-06-29 07:40 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (96 votes). Check out past polls.