It would be very helpful to see the relevant part of your code. You'd get better answers faster that way, but here's what I can offer with what you've said:

Sounds like you should build up a data structure in your program and then print the whole thing to the text file.

Assuming, as you say, that you already have the strings and the line numbers, and you've stored them in a hash, then you want something like this:

#!perl use strict; use warnings; use feature qw/ say /; my %hash = ( 4 => 'The future is unknown.', 1 => 'The day before yesterday sucked.', 2 => 'Yesterday was no better.', 5 => 'Today is a good day.', 3 => 'Not much hope for tomorrow.', ); my @output_lines; while (my ($number, $string) = each %hash) { $output_lines[$number] = $string; } for ( @output_lines ) { say $_; } __END__ ## Output: Use of uninitialized value $_ in say at ./foo.pl line 22. The day before yesterday sucked. Yesterday was no better. Not much hope for tomorrow. The future is unknown. Today is a good day.

Of course this assumes that you have a line for each number, otherwise your output file will have a bunch of empty lines. You will also get a warning about an uninitialized value for $_ as the program tries to read any elements of @output_lines which are empty. For example, the first one, if you don't have a string with position zero, as seen above.

UPDATE: Clarified the uninitialized warnings explanation.

In reply to Re: Print to specific line by 1nickt
in thread Print to specific line by perlUser345

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.