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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:43:31+00:00 2026-05-22T11:43:31+00:00

Perl is continuing to surprise me. I have a code which takes an input

  • 0

Perl is continuing to surprise me. I have a code which takes an input from the command line and checks if it is in a file. I have a file like this:

ls

date

pwd

touch

rm

First i read this file as

open(MYDATA,"filename") or die "Can not open file\n";
@commandlist = <MYDATA>;
chomp @commandlist;
close MYDATA;

the argument is in $commandname variable. To check if it is correct i printed to screen.

print $commandname."\n";

it works well. then i write the code.

$count = @commandlist;
for($i=0;$i < $count;$i++)
{
    print $commandname;
    print $commandlist[$i];
    print "\n";
    if($commandname eq $commandlist[$i])
    {
        print "equal\n";
    }
}

and it does not print ‘equal’. but it should do becaues $commandname variable has the value ‘ls’ which is in the file. i also print the value of $commandname and $commandlist[$i] to see if “visibly” they are equal and i get the output:

ls
lsls
lsdate
lspwd
lstouch
lsrm

here i see that they got the same value but why never eq operator evaluates to zero.
Additionally to get this task done, I have tried various methods all of which come to be useless like making a hash from the array and using exists.
I am struggling for this seemingly easy problem for a day but i just dont get it.
Thanks in advance

EDIT:
when i change the above loop as below

$count = @commandlist;
for($i=0;$i < $count;$i++)
{
    print $commandlist[$i];
    print $commandname;
    print "\n";
    if($commandname eq $commandlist[$i])
    {
        print "equal\n";
    }
}

I got an output like.

ls
ls
lste
lsd
lsuch
ls

it seems like for some reason it overwrites some characters.

EDIT:

my whole script is like:

#reading file code, i posted above
while(<>)
chomp($_);
$commandname = $_;
if($commandname eq "start"){
##something here
} elsif ($commandname eq "machines"){
##something here
} else {

    $count = @commandlist;
    for($i=0;$i < $count;$i++)
    {
        print $commandlist[$i];
        print $commandname;
        print "\n";
        if($commandname eq $commandlist[$i])
        {
            print "equal\n";
        }
    }

}
  • 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-22T11:43:32+00:00Added an answer on May 22, 2026 at 11:43 am

    A bit change in the code would result in what you are looking for, “chomp” the string from array before you put it for comparison. Here it is

    chomp $commandlist[$i];
    
    if($commandname eq $commandlist[$i])
    {
        print "equal\n";
    }
    

    EDIT: as per perldoc chomp when you are chomping a list you should parenthesis. So, in your case … instead simply saying

    chomp @commandlist 
    

    make it like

    chomp(@commandlist)
    

    FINAL EDIT: I tried this and worked fine. Give it a try

    $commandname = $ARGV[0];
    
    open(MYDATA,"chk.txt") or die "Can not open file\n"; 
    @commandlist = <MYDATA>; 
    chomp(@commandlist);
    close MYDATA; 
    
    print $commandname."\n"; 
    
    $count = @commandlist;
    print $commandname; 
    for($i=0;$i < $count;$i++) 
     {
    
     print $commandlist[$i]; 
     print "\n"; 
     if($commandname eq $commandlist[$i]) 
     {   
      print "equal\n";   
     } 
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a perl script which when run from the command line generates a
I have some Perl code that executes a shell script for multiple parameters, to
Perl and PHP do this with backticks. For example, $output = `ls`; Returns a
Perl seems to be killing my array whenever I read a file: my @files
I have a Perl program that needs to run about half a dozen programs
I'm working on a page which accepts file uploads. In theory, I can detect
I am using Vim to read through a lot of C and Perl code
I am new to Perl and I have to do some automation at my
I would like to write a Perl function that gets a GFF3 filename and
I have a linux filter to extract all lines from an xcode project that

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.