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 243749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:56:50+00:00 2026-05-11T20:56:50+00:00

I am reading a file using perl script. This file consists of strings with

  • 0

I am reading a file using perl script. This file consists of strings with different characters and I am supposed to identify strings containing the character ‘X’. I want to know how should I (1) print this string (containing ‘X’) and also (2) write this string to another file (3) count the number of ‘X’ characters in the whole file. The script below prints the whole file again. Any suggestions?

#!/use/bin/perl
use strict;
use warnings;

open (FILE, "/home/user/Desktop/infile.phy") || die "cant open file\n";
my @body = <FILE>;
close (FILE);
my $count= 0;
my $string = '';
foreach $_(@body){
    if ($_ =~ m/[X]/){
        print "$_";
        $count++;
        print $count;
    }
    else {
        print ;
    }
}
exit;
  • 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-05-11T20:56:50+00:00Added an answer on May 11, 2026 at 8:56 pm

    Since this is code review, let’s go one by one:

    #!/use/bin/perl
    

    That shebang line is most likely a typo. It should probably be

    #!/usr/bin/perl
    

    or whatever which perl returns on your system.

    use strict;
    use warnings;
    

    Good.

    open (FILE, "/home/user/Desktop/infile.phy") || die "cant open file\n";
    

    No need for package global filehandles when you can use lexical filehandles. The 3-argument form of open is preferable these days. Also, the error message should indicate the file which you could not open:

    my $filename = '/home/user/Desktop/infile.phy';
    open my $input, '<', $filename
        or die "Cannot open '$filename' for reading: $!";
    
    my @body = <FILE>;
    

    You are slurping the file into an array. That is completely unnecessary in this case.

    my $count  = 0;
    my $string = '';
    

    Declare and initialize (if necessary) any variables in the smallest possible scope.

    my $count;
    

    The variable $string is not used anywhere else in your code.

    foreach $_(@body){
    

    This is silly. for uses $_ if no loop variable is specified. It is easier to keep things straight if you instead specify a lexical loop variable.

    for my $line ( @body ) {
    

    However, I do not think you should slurp the file.

            if ($_ =~ m/[X]/){
    

    That results in a successful match if the line contains an X. So, it is equivalent to /X/. However, that will not tell you the word that contained the ‘X’. For that, you need to decide what a word is and do your matching at the word level.

    With all that in mind, consider the following script. I have made a simplifying assumption regarding what I consider to be a word. You should be able to build on this to satisfy all the requirements:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $filename = "$ENV{TEMP}/test.txt";
    open my $input, '<', $filename
        or die "Cannot open '$filename' for reading: $!";
    
    my $count;
    
    while ( my $line = <$input> ) {
        my @words = grep { /X/ } split /\b/, $line;
        $count += @words;
        print join(', ', @words), "\n";
    }
    
    print "$count\n";
    
    __END__
    

    UPDATE: If you do not care about finding the words within each line that have one or more X characters, the while loop would be simplified:

    while ( <$input> ) { 
        $count += (my @matches = /(X)/g );
        print if @matches;
    }
    

    by using $_. That, however, is probably inefficient (given that we are saving each matched X character). In this case, tr works best:

    my ($count, $n);
    $n = tr/X// and $count += $n and print while <$input>;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 167k
  • Answers 167k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take a look at this MSDN article on styling -… May 12, 2026 at 1:34 pm
  • Editorial Team
    Editorial Team added an answer What you've written isn't threadsafe. In fact, you've stumbled onto… May 12, 2026 at 1:34 pm
  • Editorial Team
    Editorial Team added an answer Yes, if you hash the IP address with MD5 or… May 12, 2026 at 1:34 pm

Related Questions

Perl is really good for writing the kind of string/file parsing programs that I
I am working on a log parsing script using Perl. I am reading the
Please note - I am not looking for the right way to open/read a
I have a textfile encoded in UTF-16. Each line contains a number of columns

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.