Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Print to specific line

by Laurent_R (Canon)
on Jun 23, 2015 at 17:50 UTC ( [id://1131683]=note: print w/replies, xml ) Need Help??


in reply to Re: Print to specific line
in thread Print to specific line

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.

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

    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} : ''; }

        If, as I suspect, the OP does not want the numbers to print:

        use strict; use warnings; my (%hash); while (<>) { chomp; my ($text,$line) = $_ =~ m/(.+)(\d+)$/; $hash{$line} = $text; } print $hash{$_} . "\n" for (sort keys %hash)

        Since I use only the keys, no worries about missing line numbers.

        Usage:

        perl pm1.pl < input.txt > output.txt

        Update: I along with other respondents missed the fact that there could potentially be non-sequential line numbers in the data, and there might need to be blank lines created accordingly. Updated again to be a bit more perlish.

        use strict; use warnings; my (@array); while (<>) { $array[$2] = 1 if ($_ =~ m/(.+)(\d+)$/) } print defined $_ ? $_ . "\n" : "\n" for @array[1..$#array];
        Dum Spiro Spero
      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"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1131683]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-23 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found