Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: While Loops

by Anonymous Monk
on Jan 08, 2013 at 18:53 UTC ( [id://1012310]=note: print w/replies, xml ) Need Help??


in reply to While Loops
in thread While Loops

I would like to read the 1st 100 records from the text file, print them, and read the next 100 and print them, etc until I run out of record in the text file

Replies are listed 'Best First'.
Re^2: While Loops
by kennethk (Abbot) on Jan 08, 2013 at 18:58 UTC
    With the improved spec, you'd want something more like:

    #!/usr/bin/perl -w use strict; my @data=(); my $file='c:\text.txt'; open my $fh,"<", $file or die "$file: $!"; while (my $line = <$fh>){ push @data, $line; if (@data == 100) { print @data; @data = (); } } print @data;

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re^2: While Loops
by blue_cowdawg (Monsignor) on Jan 08, 2013 at 19:02 UTC

    #!/usr/bin/perl -w use strict; my @data=(); my $file='c:\text.txt'; my $count=0; open FILE,"< $file" or die "$file: $!"; while (my $line =<FILE> ){ chomp $line; push @data,[split /\t/,$line]; $count++; next if $count < 99; while ($#data >= 0 ) { printf "%s\n",join(",",shift @data); } $count = 0; }
    So.. how much more of your homework do you need me to do?


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re^2: While Loops
by tbone654 (Beadle) on Jan 08, 2013 at 20:10 UTC

    we're really guessing at what you are trying to do, so in that spirit maybe here's a real life example that has enough stuff in it to be useful on a few fronts...


    test file
    #ls -la > test.txt ... abreviated

    -rw-r--r--  1 OQ91   Domain Users   273532 Sep 25 15:29 spy.csv.bak
    -rw-r--r--  1 OQ91   Domain Users    14704 Jan  8 12:08 spy.sdf
    -rw-r--r--  1 OQ91   Domain Users     4329 Oct 17 10:28 spy.sdf.bak
    -rwxr-xr-x  1 OQ91   Domain Users  1196465 Sep 26 03:36 spywork.ods
    -rw-r--r--  1 OQ91   Domain Users   312739 Jan  8 09:40 spyx.csv
    -rwxrwxr-x  1 OQ91   Domain Users     4875 Dec 26 22:05 stone.pl
    -rwxr-xr-x  1 OQ91   Domain Users     2396 Oct 25 09:57 stone.pl.121025
    -rwxr-xr-x  1 OQ91   Domain Users     3375 Oct 29 10:27 stone.pl.121026
    -rwxr-xr-x  1 OQ91   Domain Users     3811 Oct 29 07:52 stone.pl.121029
    -rwxr-xr-x  1 OQ91   Domain Users     3809 Nov  8 11:06 stone.pl.121108
    -rwxr-xr-x  1 OQ91   Domain Users 77687808 Sep 26 03:46 Taylor_Book_08.xls
    -rw-r--r--  1 OQ91   Domain Users     9095 Dec 26 22:14 test15.pl
    -rw-r--r--  1 OQ91   Domain Users     4350 Nov  7 01:07 test15.pl.121107
    -rw-r--r--  1 OQ91   Domain Users     4424 Nov  7 11:57 test15.pl.121107a
    -rw-r--r--  1 OQ91   Domain Users     8630 Nov  7 13:57 test15.pl.121107b
    -rw-r--r--  1 OQ91   Domain Users     9039 Nov  8 08:59 test15.pl.121108
    -rw-r--r--  1 OQ91   Domain Users     9156 Dec 26 22:10 test15.pl.121226
    -rw-r--r--  1 OQ91   Domain Users    46987 Dec  4 13:25 zzz.out
    
    $ cat test.txt | perl -l -n -e 'BEGIN {$x="OQ91";$linecount=0;$sum=0;} if((split)[2]==$x){$linecount++ ;$sum += (split)[5]; printf "%s\t %s\t\t %s\n",(split)[2],(split)[5],(split)[9]} ; END {printf "linecount: %d  Sum: %d",$linecount,$sum}'

    Here's some output... abreviated

    OQ91     312739          spyx.csv
    OQ91     4875            stone.pl
    OQ91     2396            stone.pl.121025
    OQ91     3375            stone.pl.121026
    OQ91     3811            stone.pl.121029
    OQ91     3809            stone.pl.121108
    OQ91     77687808                Taylor_Book_08.xls
    OQ91     9095            test15.pl
    OQ91     4350            test15.pl.121107
    OQ91     4424            test15.pl.121107a
    OQ91     8630            test15.pl.121107b
    OQ91     9039            test15.pl.121108
    OQ91     9156            test15.pl.121226
    OQ91     46987           zzz.out
    linecount: 61  Sum: 88200728
    

    sums the 5th column if the tests are true, counts the lines... prints the 2nd, 5th and 9th columns, AND it's a one liner...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1012310]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found