in reply to Print to specific line
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Print to specific line
by Laurent_R (Canon) on Jun 23, 2015 at 17:50 UTC | |
by 1nickt (Canon) on Jun 23, 2015 at 18:01 UTC | |
by fishmonger (Chaplain) on Jun 23, 2015 at 18:46 UTC | |
by GotToBTru (Prior) on Jun 23, 2015 at 19:01 UTC | |
by Laurent_R (Canon) on Jun 23, 2015 at 19:03 UTC |