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
Solved

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

Posted on 2003-11-25
6
286 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to strip .csv from file name 9 83
Perl passing in variables to do substitution 6 75
How to generate square thumbnail using perl 13 86
perl getopt long help 34 70
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

809 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