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

In reply to How can one sort array elements in different text files? by supriyoch_2008

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.