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

I need to split the .bin file based on the combination of Unique Number, description and Page number (Page 1 of X) using Perl. As provided in the sample data for Unique number 111-111111 there are two pages of statement which starts from Page 1 of 1 and ends at Page 1 of 1 itself and the second Unique number is 222-222222 which starts from Page 1 of 3 and ends at Page 3 of 3. The sample input needs to be split up in two files. File 1 and File 2. Below is the code which I wrote,I am unable to understand how do I put the page validation in place with a combination of a unique account number My perl version v5.10.1. Appreciate any help and thanks in advance.
#!/usr/bin/perl use strict; use warnings; my $input = do { local $/; <DATA> }; my @input = split /(?=\d+ Page)/, $input; foreach (0 .. $#input) { say "Record $_ is: $input[$_]"; }
Here is the sample Data
Page 1 of 1 Account Unique_Number Description GENERAL 111-111111 This is the first line I am learning perl Page 1 of 3 Account Unique_Number Description PERSONAL 222-222222 This is the first line I am learning perl Page 2 of 3 Account Unique_Number Description Educational 222-222222 This is the second line I am learning perl Page 3 of 3 Account Unique Number Description Educational 222-222222 This is the third line I am learning perl

Replies are listed 'Best First'.
Re: Segregate data based on Page number and account combination
by AnomalousMonk (Archbishop) on Nov 14, 2018 at 22:33 UTC

    It's good that you've provided an example input data file, but I don't understand what you would want to see in the two output files to be produced from this input. I also don't understand what the process of "page validation" is. Can you please supply more info?

    BTW: The example data seems rather messy. E.g., there is no "Beginning Value" for the account number 222-222222 "Educational" "Account Type/Name": what, if any, value should appear in the output for the beginning value? Perhaps also see How do I post a question effectively? and I know what I mean. Why don't you?


    Give a man a fish:  <%-{-{-{-<

      Thanks for your reply below are the two expected output files. The page validation should work for a Account Number as for example in Output 2 for account number 222-222222 there are three pages of statements which collectively needs to be printed and since Output 1 has only 1 page for a unique account so only that data is printed.I have corrected the data as well please have a look now. Thanks for your help
      Output 1:
      Page 1 of 1 Page 1 of 1 Account Unique_Number Description GENERAL 111-111111 This is the first line I am learning perl
      output 2:
      Page 1 of 3 Account Unique_Number Description PERSONAL 222-222222 This is the first line I am learning perl Page 2 of 3 Account Unique_Number Description Educational 222-222222 This is the second line I am learning perl Page 3 of 3 Account Unique Number Description Educational 222-222222 This is the third line I am learning perl
      Expected Output in File 1:
      Page 1 of 1 Account Unique_Number Description GENERAL 111-111111 This is the first line I am learning perl
      Expected Output in File 2:
      Page 1 of 3 Account Unique_Number Description PERSONAL 222-222222 This is the first line I am learning perl Page 2 of 3 Account Unique_Number Description Educational 222-222222 This is the second line I am learning perl Page 3 of 3 Account Unique Number Description Educational 222-222222 This is the third line I am learning perl

        Open a new file at each Page 1 line.

        #!/usr/bin/perl use strict; my $fileno = 0; my $fh; while (<DATA>){ if (/^ *Page 1/){ close $fh if $fileno; ++$fileno; print "Opening file $fileno\n"; open $fh,'>',"$fileno.txt" or die "Could not open $fileno.txt"; } print $fh $_; } close $fh; __DATA__ Page 1 of 1 Account Account_Number Beginning_Value GENERAL 111-111111 $88,0853.95 Page 1 of 3 Account Account_Number Beginning Value PERSONAL 222-222222 $88,0853.95 Page 2 of 3 Account Account_Number Beginning Value Educational 222-222222 Page 3 of 3 Account Account Number Beginning Value Educational 222-222222
        poj
Re: Segregate data based on Page number and account combination
by AnomalousMonk (Archbishop) on Nov 15, 2018 at 03:51 UTC

    Update: The OPer has removed the original input/output file examples and substituted significantly different ones; this reply is no longer germane.

    GENERAL     111-111111      $88,0853.95

    And I just noticed this, but what kind of number is $88,0853.95? (Also in output data here.)


    Give a man a fish:  <%-{-{-{-<

      oops that's the trick the 1% uses to own 99% of stuff!

Re: Segregate data based on Page number and account combination
by Corion (Patriarch) on Nov 14, 2018 at 22:12 UTC

    Crossposted from StackOverflow.

    Crossposting is acceptable, but it is considered polite to inform about it so that efforts are not duplicated.

      Apologies for this, I was unaware of this being in place. Would you mind providing a solution if you are aware I am really struggling to figure this out. Thanks