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
    It seems to me that you are doing a little bit too much work. A hash AND an array are not needed, just one of them is enough.

    We can either store the lines directly into an array (indexed with the output line numbers) and just print the way you did. Or store the lines into a hash, and sort the keys for printing.

      Hm, can't agree, really. I think he needs an array because he said the line numbers are important.

      If you use a sorted hash then the output file will only contain lines for the strings he has, and if there are any sequential numbers missing from the input, then the output file will not meet the specs.

      (I do agree that you could put the data into an array as you split the input lines, skipping the hash. But I didn't want to clutter my example with code for splitting the input, as he said he has the strings and the line numbers already).

        I agree with Laurent. Using both is unnecessary.

        Here's an adjusted version which only uses a hash and accounts for possible missing line numbers, which prevents the uninitialized warnings.

        use 5.010; use strict; use warnings; use List::Util qw( max ); my %hash = ( 4 => 'The future is unknown.', 1 => 'The day before yesterday sucked.', 5 => 'Today is a good day.', 3 => 'Not much hope for tomorrow.', ); my $last = max keys %hash; for my $line ( 1 .. $last ) { say exists $hash{$line} ? $hash{$line} : ''; }
        Hm, agreed, but the OP did not say there were going to be empty slots, and it seems rather unlikely from his description of the problem.

        But even if such is the case, still, there is no need for duplicating the data.

        My view is that using an array to store the data is the simplest choice. But I would still add code to prevent spurious warnings for undefined values, for example something like:

        for my $i (0..$#array) { print defined $array[$i] ? "$array[$i]\n" : "\n"; }
        But even with a hash, it is quite simple to do more or less the same thing:
        for my $i (0..$max_hash_key) { print defined $hash{$i} ? "$hash{$i}\n" : "\n"; }