Solved

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

Posted on 2003-11-25
6
283 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
6 Comments
 

Assisted Solution

by:dkf360
dkf360 earned 70 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 70 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Assisted Solution

by:kandura
kandura earned 70 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Input from stdin for perl 6 118
SIMPLE Perl Regex 1 153
Removing permissions on a Cyrus mailbox via a wrapper script 3 88
Vb script to unzip a files and rename the files 5 82
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now