use strict; use warnings; # shift the file from args my $file_path = shift @ARGV; # a simple switch my $sw = 1; # take indices in reverse order and skipping 2th 4th.. my @wanted_lines = grep {$sw++ and $sw % 2 == 0} reverse @ARGV; open my $fh, '<', $file_path or die "unable to open $file_path! $!"; # in list context eat all the file at once my @all_lines = <$fh>; # of those all lines print a slice: the slice is @wanted_lines # with all valuse lowered by one because indices of array start at 0 # lines starts a 1 print for @all_lines[map{$_-1}@wanted_lines]; # invoke like: # inverted_alternated.pl file_to_read.txt 1 2 3 4