Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Printing out a list of characters and the position they are in

This is a fairly simple question.  I need a perl script that takes in the following parameters:

scriptname.pl <file.txt> <starting char. index> <ending char. index>

I need the script to print out the index number and the character that is in that place in the file.  

For example, if I have a file with the following composition:
a4poiuasdlkjga g23098u>.       d;jeopaiu4095u4032q58';ejrE        kj;lkj   ao0897wu987

$> scriptname.pl file.txt 20 34    should return the following output:
20:9
21:8
22:u
23:>
24:.
25:
26:
27:
28:
29:
30:
31:
32:d
33:;
34:j
------->Entire String:98u>.       d;j

Thanks in advance,
Dmitriy
0
Dmitriy
Asked:
Dmitriy
3 Solutions
 
dkf360Commented:
Here is the perl script to do what you asked.

#!/usr/bin/perl

# store arg values
$filename = @ARGV[0];
$start = @ARGV[1];
$end = @ARGV[2];
$count = 0;
$record = false;

# open file to read
open FILE, "$filename" or die "Can't open filename";

#read until end of file
while (!eof(FILE))  
{
  $count++;
  $character=getc(FILE);
 
  #print to output the given character range
  if ($count == $start) {
      $record = true;
  }
  elsif ($count > $end) {
      $record = false;
  }
  if ($record eq true) {
      print "$count:$character\n";
  }
}

close FILE;
0
 
FishMongerCommented:
#!/usr/bin/perl -w

use strict;
my ($filename, $start, $end) = @ARGV;

open FILE, "$filename" or die "unable open $filename $!";

{
local $/;
my $str = <FILE>;
my @str = split //, $str;
for my $i ($start..$end) {
   print "$i: $str[$i-1]\n";
}
}
0
 
FishMongerCommented:
Here's another option that's sililar to dkf360's but a little more efficient.

#!/usr/bin/perl

use strict;

my ($filename, $start, $end) = @ARGV;
my ($char_num, $char);

open FILE, "$filename" or die "unable open $filename $!";

while (!eof(FILE)){
   $char_num++;
   $char = getc(FILE);
   if ($char_num >= $start && $char_num <= $end) {
      print "$char_num: $char\n";
   }
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kanduraCommented:
How about this:

#!/usr/bin/perl

use strict;
my ($filename, $start, $end) = @ARGV;
my $buf;
my $i = $start;

open F, $filename or die "Error opening $filename: $!";
seek(F,$start,0) or die "Error seeking to $start: $!";
read(F, $buf, $end-$start) or die "Error reading character range: $!";
close F or die "Error closing $filename: $!";

print map { $i++ . ":$_$/" } split //, $buf;
0
 
FishMongerCommented:
We could make it a little more efficient if we add one more line after the print statement.

  last if $char_num == $end;

That will make it drop out of the loop after we print the last char that we want, i.e., if we're only wanting char 1..10, there is no need to read the rest of the file.
0
 
josephfluckigerCommented:
I just have to say you guys write amazing code. I like how we've started a who can be the briefest competition here. Instead of "FILE", we're now using "F"! I guess that's the beauty of Perl :).


0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now