Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-11-25
6
Medium Priority
?
291 Views
Last Modified: 2010-03-04
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
Comment
Question by:Dmitriy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Assisted Solution

by:dkf360
dkf360 earned 280 total points
ID: 9819765
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
 
LVL 28

Expert Comment

by:FishMonger
ID: 9819970
#!/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
 
LVL 28

Accepted Solution

by:
FishMonger earned 280 total points
ID: 9820118
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!

 
LVL 18

Assisted Solution

by:kandura
kandura earned 280 total points
ID: 9820298
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
 
LVL 28

Expert Comment

by:FishMonger
ID: 9820325
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
 
LVL 1

Expert Comment

by:josephfluckiger
ID: 9821094
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question