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

  • SEARCH
  • Home
  • 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

Related Questions

I am reading a csv file using opencsv. I am ignoring the first line
Am writing an application for iphone to read a text file using NSData. NSFileHandle
Possible Duplicate: Reading the last n lines from a huge text file I have
I need to connect to an open Excel 2003 file using .NET 3.5 It
I need to read some filenames from an xml config file using xmlstarlet with
Perl is continuing to surprise me. I have a code which takes an input
I have got some XML files that contain comments above the nodes. When I
All right guys and gals it's time for the age old question, how do
Here is the result of a profiled simulation run of my MATLAB program. I
If I have few csv files as follows: a,1,2,3 type, max, min, avg b,4,5,6

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.