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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:05:24+00:00 2026-06-09T16:05:24+00:00

I wrote a perl script to count the occurrences of a character in a

  • 0

I wrote a perl script to count the occurrences of a character in a file.
So far this is what I have got,

#!/usr/bin/perl -w

use warnings;
no warnings ('uninitialized', 'substr');

my $lines_ref;
my @lines;
my $count;

sub countModule()
{
my $file = "/test";
open my $fh, "<",$file or die "could not open $file: $!";
my @contents = $fh;
my @filtered = grep (/\// ,@contents);
return \@filtered;
}

@lines = countModule();
#@lines = $lines_ref;
$count = @lines;
print "###########\n $count \n###########\n";

My test file looks like this:

10.0.0.1/24
192.168.10.0/24
172.16.30.1/24

I am basically trying to count the number of instances of “/”

This is the output that I get:

###########
 1
###########

I am getting 1 instead of 3, which is the number of occurrences.
Still learning perl, so any help will be appreciated..Thank you!!

  • 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-09T16:05:25+00:00Added an answer on June 9, 2026 at 4:05 pm

    Close, but a few issues:

    use strict;
    use warnings;
    
    sub countModule
    {
        my $file = "/test";
        open my $fh, "<",$file or die "could not open $file: $!";
        my @contents = <$fh>;  # The <> brackets are used to read from $fh.
        my @filtered = grep (/\// ,@contents);
        return @filtered;      # Remove the reference.
    }
    
    my @lines = countModule();
    my $count = scalar @lines;  # 'scalar' is not required, but lends clarity.
    print "###########\n $count \n###########\n";
    

    Each of the changes I made to your code are annotated with a #comment explaining what was done.

    Now in list context your subroutine will return the filtered lines. In scalar context it will return a count of how many lines were filtered.

    You did also mention find the occurrences of a character (despite everything in your script being line-oriented). Perhaps your counter sub would look like this:

    sub file_tallies{
        my $file = '/test';
        open my $fh, '<', $file or die $!;
        my $count;
        my $lines;
        while( <$fh> ) {
            $lines++;
            $count += $_ =~ tr[\/][\/];
        }
        return ( $lines, $count );
    }
    
    my( $line_count, $slash_count ) = file_tallies();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#!/usr/bin/perl use strict; use warnings; sub paragraph { open my $file, <, dict.txt or
I have a Perl script I wrote for my own personal use that fetches
I wrote like this Perl script.( /tmp/file is fuge) system(ssh host1 'rsync -av /tmp/file
I have an interesting problem. I wrote the following perl script to recursively loop
I have to write a Perl script to convert an XML file into a
I wrote the following Perl script (below) in order to create simple XML file.
I am reading a file using perl script. This file consists of strings with
I wrote a simple perl script but i am getting this runtime error: Can't
I'm writing a perl script that reads a file into an array. I wrote
I have a perl script exist in the follwoing path (/home/Leen/Desktop/Tools/bin/tool.pl) Every time I

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.