Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

printing certain number of data

by drock (Beadle)
on Jul 01, 2004 at 15:46 UTC ( [id://371132]=perlquestion: print w/replies, xml ) Need Help??

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

All, I am trying to figure out how to print 8 scalers/elements then \n, then 5 more lines of 8 or less for a max total of 40 Here is my code:
print FILEOUT "eject 0,0,0 "; my $count = `wc -l <$ejectapes`; if ($count <= 40 ) { while(<FILE>) { chomp $_; print FILEOUT "$_ "; # if 1..8 #print substr($a,0,7); } close (FILEOUT); }
The substr is sort of what I want, but is sprintf possible ...what should I use? Here is what my end results will look like if I had a file with 40 E strings: Exxxx Exxxx Exxxx Exxxx Exxxx Exxxx Exxxx Exxxx \n x5 \n anything less but it cannot exceed 8 per line: Exxxx Exxxx Exxxx Exxxx Exxxx Exxxx Exxxx Exxxx \n Exxxx ..... \n thank you,

Replies are listed 'Best First'.
Re: printing certain number of data
by Fletch (Bishop) on Jul 01, 2004 at 16:07 UTC

    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.

      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

Re: printing certain number of data
by TrekNoid (Pilgrim) on Jul 01, 2004 at 17:39 UTC
    I might be missing what you're after... The following take a string and cuts it into 8-character strings, separated by spaces, and puts a newline after each 8th one.

    Maybe your input file is a list of file names? If that's the case, you seem to know how to get the filename you want. Just use the parts that you need.

    If I'm *way* off, I apologize... I'm still pretty new at this :)

    my $a = "E1111111E2222222E3333333E4444444E5555555E6666666E7777777E8888 +888E99999"; my $cnt = 0; while (length($a)) { my $b = substr($a,0,8); print "$b "; $a = substr($a, 8); if (!(++$cnt%8)) { print "\n"; } } print "\n";
Re: printing certain number of data
by tachyon (Chancellor) on Jul 02, 2004 at 00:34 UTC

    Making a wild stab in the dark given the vagueness of input and output available/desired I am assuming an input file format like:

    # input foo bar baz boo foo bar baz bax foo bar baz boo foo bar foo bar baz boo foo bar baz bax foo bar baz boo foo bar baz bax my $max = 40; while(<FILE>){ chomp; my @elems = split ' '; @elems = grep{ defined $_ }@elems[0..7]; $max -= scalar @elems; last if $max < 0; # exit if printing would make more than 40 elemen +ts; print "@elems\n"; last if $. == 6; # exit at 6th line (1+5 more) }

    cheers

    tachyon

Log In?
Username:
Password:

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

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

    No recent polls found