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

Write a Perl script that uses <> to read STDIN such that you can pipe the output of the previous program into it and then sort the cut fragments by length, try doing this using split and storing matches in an array. For example, splitting the sting at the ^ character into a single array variables.

The output of the previous code looks like this...

  AAAAAAAAGACGT^CTTTTTTTAAAAAAAAGACGT^CTTTTTTT

and so the array for this sectionwould look like

 ("AAAAAAAAGACGT","CTTTTTTTAAAAAAAAGACGT","CTTTTTTT")

I am not quite sure how to do this... I imagine the sort function would be used

  sort { length($a) <=> length($b)

and

 split(/\^/

and on the command line I would put previous_script.pl text.txt | new_script

Thank you

Replies are listed 'Best First'.
Re: Creating array from previous script using split
by stevieb (Canon) on Oct 22, 2015 at 17:36 UTC

    So you're splitting and sorting in the second script after outputting only a single string from the first, correct?

    You're very close with your split and sort so I'll let you peruse the docs so you can learn how to finish that.

    For passing data from one script to another in the manner you've been asked, you'll need to print the string at the end of script one, then research how to use the STDIN file handle as input into a script, to accept the data into script two.

Re: Creating array from previous script using split
by ww (Archbishop) on Oct 22, 2015 at 22:03 UTC

    Smells like homework.

    Is it?

    If so, label it. If not, tell us why you're using two scripts.