[Solved] Reading a file and output in a particular format in Perl [closed]


I think I know what you want:

perl -F'\s' -anE'BEGIN{$/="\n\n";}$i=$F[6];say"$_ - ",$i--for($F[0]..$F[2])'

Simple, isn’t it?

BTW, your example output is wrong. I see the image (copy) mirrored. Left value is bigger than right. So values in second column should go down. If it can vary in your input data you have to use little bit longer solution.

perl -F'\s' -anE'BEGIN{$/="\n\n";}$i=$F[6];say"$_ - ",$i<$F[8]?$i++:$i--for($F[0]..$F[2])'

There is normal script version:

use strict;
use warnings;

$/ = '\n\n';
$\ = '\n';

while (<>) {
    my ( $f, $t, $i, $j ) = ( split ' ' )[ 0, 2, 6, 8 ];
    print "$_ - ", $i < $j ? $i++ : $i-- for ( $f .. $t );
}

Edit:

How it works? $/ = '\n\n'; switches reading into paragraph mode. (“Lines” are separated by two ends of line.) $\ = '\n'; adds end of line after each print invocation. Using say instead of print will do same thing but you have to use use feature 'say'; or -E switch. split ' ' or -F'\s' splits read paragraph by white chars which includes end of line. (Note ' ' has special meaning in split invocation.) Then it uses 1st, 3rd, 7th and 9th “word” (indexes 0,2,6,8) to determine which coordinates should be printed. $f .. $t generates original coordinates and $i is starting point for image coordinates. $i < $j chooses direction of image coordinates and $i < $j ? $i++ : $i-- just generates them. Suffix for is pretty Perl idiomatic way how to iterate trough the generated original coordinates.

18

solved Reading a file and output in a particular format in Perl [closed]