Solved

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

Posted on 2003-11-25
6
281 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
Comment Utility
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
Comment Utility
#!/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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Assisted Solution

by:kandura
kandura earned 70 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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 …
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…

762 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

10 Experts available now in Live!

Get 1:1 Help Now