in reply to printing certain number of data

perldoc -f splice. Keep in mind this is destructive so you might want to do the splicing to a copy rather than your original if you need to use the source array again later.

my @src = 'a' .. 'zz'; my $count = 0; while( my @cur = splice( @src, 0, 8 ) ) { print join( "\t", @cur ), "\n"; $count += @cur; last if $count >= 40; }

Update: Clarified warning.

Replies are listed 'Best First'.
Re^2: printing certain number of data
by drock (Beadle) on Jul 01, 2004 at 16:49 UTC
    I would rather use substr since these are techinically strings! Can substr allow for spaces in between the indexs? FILEOUT looks like E2323 E2343 E4344
    while (<FILE>) { chomp $_; print FILEOUT substr($_, 0, 7);
    but where do I go from here to get the remaining E string list? I have not been able to make substr include spaces as my output is all strung together as such.... E2323E3243E2432E543

      substr won't include any spaces if your original source string doesn't have any. At any rate, just make an array out of your source string and use what I showed above.

      my @src; { my $tmp = $a; push @src, substr( $tmp, 0, 7, "" ) while $tmp; } # . . . as above . . .

      Update: And looking again at your sample it appears you might have rows of space separated data, in which case you'd want to use something along the lines of while( <SRC> ) { chomp; push @src, split( /\s+/, $_)  } to build @src. Sample data in <code> tags would help clear things up.

      I'm afraid I have no idea of what you are trying to achieve. In your original post you say:

      ...how to print 8 scalers/elements then \n, then 5 more lines of 8 or less for a max total of 40...

      but that makes a maximum total of 48.

      And maybe you could show us what your input file looks like?

      dave