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 .
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: