supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:
Hi Perlmonks,
I am interested to sort the elements of an array by two's or three's in different text files within a folder. The array is @array=qw/a b c d e f g h i/; within the "result" folder the text file 1.txt should contain a b; 2.txt should contain c d; 3.txt should contain e f etc. I have written a code but it does not give the desired results. I welcome suggestions from perlmonks to solve this problem.
Here goes my code:
#!/usr/bin/perl use warnings; use strict; my @array= qw/a b c d e f g h i/; my $size= 2; # Number of elements in each text file my $count= @array; my $file_num1= $count/$size; my $integer= int$file_num1; my $file_number= $integer+1; # maximum number of files print "\n Number of text files: $file_number\n"; # To create a result folder on desktop: my $dir="Result"; mkdir $dir or die $!; # code to sort two elements in each text file: my $num1=0; for (@array) { $num1++; # for LOOP begins # code to be written to place $size elements in each file # To create the text files like 1.txt, 2.txt inside Result folder: #################################################### my $output="$dir/$num1.txt"; open (my $fh,">",$output) or die"Cannot open file'$output'.\n"; ############################################### # print $fh ?? close $output; } # for LOOP ends exit;
I got the results as follows with the creation of a Result folder containing 9 text files i.e. 1.txt ... 9.txt:
C:\Users\x>cd d* C:\Users\x\Desktop>p.pl Number of text files: 5 C:\Users\x\Desktop>
The expected Result folder must contain 5 text files with two elements like:
1.txt a b 2.txt c d .. 5.txt i
|
|---|