in reply to Print to specific line

One approach is to grab the number, stuff all lines into a hash, then print the sorted lines. I made many assumptions about your input and output.
use warnings; use strict; my %lines; while (<DATA>) { my ($num) = $_ =~ /(\d+)$/; $lines{$num} = $_; } for (sort {$a <=> $b} keys %lines) { print $lines{$_}; } __DATA__ foo bar 4 goo 1 hoo boo boo 2 yeah 3

Outputs:

goo 1 hoo boo boo 2 yeah 3 foo bar 4

Replies are listed 'Best First'.
Re^2: Print to specific line
by 1nickt (Canon) on Jun 23, 2015 at 18:07 UTC

    Don't think this would work as he wants if the data are as follows:

    __DATA__ foo bar 3 goo 1 hoo boo boo 5 yeah 7
      It works for me (output):
      goo 1 foo bar 3 hoo boo boo 5 yeah 7

        Hi Toolic,

        Sure don't want to be argumentative, or nitpick, but the OP said in his spec that the strings had to be printed at the line number of the text file that corresponded to the number in the input.

      In what way will it not work?

      Dum Spiro Spero

        Because it doesn't account for non-sequential numbers and therefore doesn't meet the spec.