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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:43:53+00:00 2026-05-30T00:43:53+00:00

I create a txt file that have 3 element and i wrote this code

  • 0

I create a txt file that have 3 element and i wrote this code :

my $in_file1 = 'file.txt';
open DAG,$in_file1;
my @shell=<DAG>;
close DAG;
chomp(@shell);
foreach my $shell(@shell){
 # and etc code 

and i want if the number of element is 0 do something and if 1 do other thing and if 2 … . for example

if (@shell[0]) print "hi"; if(@shell[1]) print "bye" if(@... 

what am i going to do ? what is the best and simplest way for doing this ? Thanks .

  • 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-30T00:43:56+00:00Added an answer on May 30, 2026 at 12:43 am

    One of the best ways to do work based on a value is a hash/redirect table, especially if you need to do that sort of work more than once in a program. This involves creating a hash whose keys are the selector values, and values are references to subroutine doing work.

    In your case, you are doing based on # of words, so a lookup array is a good way to go:

    sub bye { print "bye"; }
    my @actions = (
        sub {  },            # do nothing for 0. Using anonymous sub
        sub { print "hi" },  # print "hi" for 1
        \&bye,               # for 2 - illustrate how to use reference to existing sub
    );
    use File::Slurp; # To read the file
    my @lines = read_file("my_file");
    for (my $index = 0; $index < @lines; $index++) {
         &{ $actions[$index] }($lines[$index]); 
         # Call the sub stored in an array in correct place
         # Pass it the line value as argument if needed.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that looks like this: ftp://url1/files1.tar.gz dir1 ftp://url2/files2.txt dir2 .... many
I have a text ( .txt ) file that contains Java code! I want
I have rjecnik.txt file that looks like this mate sime, jure stipica gujo, prvi
I have this small program and it needs to create a small .txt file
i have a task assigned that i have to create a text file (notepad)
I have a txt file that contains data which I need to input to
How can I create a .txt file using flex 3? And I want to
Create a file called Valid[File].txt and stick some text in it. Start powershell and
I have a filelist.txt file and I created a file called clear.php to clear
I have an XML document as follows: <directory> <file><monitored>0</monitored> <xferStatus>1</xferStatus> <name>test1.txt</name> <size>7</size> <created>03/31/10 11:30:02

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.