in reply to Creating a Menu List

Since you have no numbers next to the lines, we have to guess. I suspect

$user = $file;

to be the culprit, since you just clobber the $user variable whose value you just read from STDIN and carefully chomped, with an undefined value: the my $file above is defined only for the foreach context. use strict would catch that. And so,

chdir("$hdir/$user") || die "Cant Change to directory $hdir/$user: $!\ +n";

causes the the warning to be thrown. Where's $hdir defined?

Anyways, that should be

my $number = <STDIN>; chomp $number; die "Invalid selection\n" if $number =~ /\D/; die "Number given too big\n" if $number > scalar(@ulist); my $user = $ulist[$number]; chdir("$hdir/$user") || die "Cant Change to directory $hdir/$user: $!\ +n";

Replies are listed 'Best First'.
Re^2: Creating a Menu List
by muizelaar (Sexton) on Sep 16, 2008 at 14:07 UTC
    Hi shmem
    Thanks for your response it was really helpful.
    I thought I would send you all my code with your suggestion attached
    what is, I think is happening is its starting from the first entry in the array which is 0.
    However no matter which selection I use it can’t seem to change to that user directory which does exist.
    Any further help would be greatly appreciated.
    #!/usr/bin/perl -w use Cwd; my $dir = getcwd; my $userhome = $ENV{HOME}; my $hdir = "/export/home"; my $userlist = "/$userhome/etc/userlist.cfg"; open(MYFILE, "$userlist") || die "Cant open File $userlist: $!\n"; my @ulist=<MYFILE>; foreach my $file (@ulist) {eval { print "\n"; $n++; print $n; print " $file\n"; }} print " Please select which user you would like to...\n"; my $number = <STDIN>; chomp $number; die "Invalid Selection\n" if $number =~ /\D/; die "Number given is too big\n" if $number > scalar(@ulist); my $user = $ulist[$number]; chdir("$hdir/$user") || die "Cant Change to directory $hdir/$user: $!\ +n"; &first; sub first { system("su - $user -c whatsrunning"); } sub second { print "Would you like to see the options again?\n"; my $answer = <STDIN>; chomp $answer; if ($answer eq "Y" || $answer eq "y" || $answer eq "yes") { system("su - $user -c whatsrunning"); &second; } else {exit 0; }} &second;

      As already stated, it would be wise to add a use strict to the beginning of your script. You'll then have to add a my $n = 0 before the foreach loop.

      I think you're going to have to either decrement $number, since your menu starts at one, or instead start your menu at zero.

        Hi
        After playing around a while i have managed to get it working.
        Thankyou for all your help