At 6:47 PM +0800 1/13/10, Majian wrote: >Hi,all > >There is a problem confused me for a long time . >It is: > >cat test.txt >------------------------ >1, >2, >3, >4, >5, >6, >7, >8, >9, >10, >11, >------------------------ > > >Then I want to this result : >1 2 3 6 9 > 4 7 10 > 5 8 11 > >I don't know how to print the result use the Perl script clearly. >Could someone give me some suggestions?
>Here are some snippets to get you started (untested): >1. open the file: > open( my $fh, '<', $filename) or die("Can't open file $filename: $!"); > >2. Read the lines into an array, one line per element: > my @lines = <$fh>; > >3. Remove the newlines and commas from the end of the strings: > > chomp(@lines); > s/,$// for @lines; > >4. Print the lines in any order according to whatever logic you desire: > > print "$_ " for @lines[0,1]; > for( my $i = 2; ($i+6) <= $#lines; $i++ ) { > print "$_ " for @lines[$i,$i+3,$i+6]; > print "\n"; > } > > >-- >Jim Gibson >j...@gibson.org -- Does this occur when you copy a file from one operating system to another in binary? (i.e. return carriage character needs to be translated)? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/