Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8808609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:40:02+00:00 2026-06-14T02:40:02+00:00

I am trying to create a program in Perl that will read through thousands

  • 0

I am trying to create a program in Perl that will read through thousands of characters that and attempt to find a matching string of characters. I need to print out the string plus the next five letters. I also need to print out the location of where it was found, i.e. how many letters in. I am pretty new to Perl. I’m just learning it in class now.

Here is the code I have so far:

#!/usr/bin/perl

$sequence = 'abcd';
$fileName = 'file.txt';

#Opening file
unless (open(fileName, $fileName)) {
    print "Cannot open file.";
    exit;
}
@tempArr = <fileName>;    #Adding the lines to an array
close fileName;           #closing the file
$characters = join('', @tempArr);    #making it a clean string
$characters =~ s/\s//g;               #removing white lines
if (characters =~ m/$sequence/i) {

    #Print $sequence and next five characters
}
else {
    print "Does not contain the sequence.";
}

exit;

file.txt will look like:

aajbkjklasjlksjadlasjdaljasdlkajs
aabasdajlakjdlasdjkalsdkjalsdkjds
askdjakldamwnemwnamsndjawekljadsa
abcassdadadfaasabsadfabcdhereeakj

I need to print out the “abcdheree”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:40:04+00:00Added an answer on June 14, 2026 at 2:40 am

    To print $sequence & the 5 characters after it, try using :

    if ($characters =~ m/$sequence.{5}/i) {
        print "$&\n";
    

    (You forgot the $ on characters)

    NOTE

    • . mean any character
    • {5} is a quantifier
    • when using open, use 3 arguments like : open my $fh, "<", "$file" or die $!; See http://perldoc.perl.org/perlopentut.html
    • always put use strict; use warnings; at the top of your script
    • don’t forget $ on variables (you miss many ones)
    • use my for declaring variables
    • maybe a better approach than making one big string: you can process the array line by line like : foreach my $line (@tempArr) { #process $line }
    • you call the array @melTemp1 that is never declared

    FINALLY

    #!/usr/bin/perl
    use strict; use warnings;
    
    my $sequence = 'abcd';
    my $fileName = 'file.txt';
    
    #Opening file
    open my $fh, "<", $fileName or die "Cannot open file. [$!]";
    
    my @tempArr = <$fh>;                    #Putting the file handle into an array
    close $fileName;                        #closing the file handle
    
    my $characters = join('', @tempArr);    #making it a big string
    $characters =~ s/\s//g;                 #removing white spaces & tabs
    
    if ($characters =~ m/$sequence.{5}/i) {
        print "$&\n";
    }
    else {
        print "Does not contain the sequence.";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a program that will go through a bunch of tumblr
I have been trying to create a program using perl www::Mechanize that will follow
I am trying to create a program that will read in a text file
I am trying to create a program that will get data directly from socket
I am trying to create a program that will be passed input data from
I am trying to create a program that will do some simple calculations, but
I am trying to create a program which will read a file and check
I am trying to create a program that will set up a crontab to
I am trying to create a program that will open a text file with
I'm trying to create a program for Android which will read in text, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.