Dalin has asked for the wisdom of the Perl Monks concerning the following question:

Hey all, I am trying to rename a variable within the code so that when the user makes a numeric selection, the selection number is appended to the variable name which will reference a file name held in an array element. Then I want to pass the variable along to a sub. Here is the code:
sub mod_ulist { my @user_files = </usr/local/squid/etc/aclfiles/*.acl>; $file1 = "$user_files[0]"; $file2 = "$user_files[1]"; $file3 = "$user_files[2]"; $file4 = "$user_files[3]"; $file5 = "$user_files[4]"; $file6 = "$user_files[5]"; my $mail_flag = "1"; $oldformat = $~ = "MOD_ULIST"; open ($~); system 'clear'; write; print "Please choose a file to modify: "; my $choice = <STDIN>; chomp ($choice); $ufile = '$file' . "$choice"; file_mod($ufile,$mail_flag); }
What happens is that I get the variable name and not the info contained in the variable. Also, Is there a better (less typing) way of referencing the array elements? Thanks in advance, Bradley
Where ever there is confusion to be had... I'll be there.

Replies are listed 'Best First'.
Re: Changing a varaibale name within the program?
by Sifmole (Chaplain) on Jun 26, 2001 at 23:38 UTC
    I first saw the list of $file1, $file2, $file3... And thought "This person needs an array."; Then I saw that you had and array and were for some reason pulling the array into single variables.

    If I was doing this I might attack it as such.. (not tested)...

    my @user_files = <longfilename>; unshift(@user_files, 'blank'); # skipping to the $ufile line # Make sure $choice is numeric and # within the range in user_files if ( ($choice =~ /^\d+$/) && ($choice > 0) && ($choice <= scalar(@user_files)) ) { file_mod($user_files[$choice], $mail_flag); } else { # Error condition, choice outside of range }
    In cases where you are tempted to name things foo1, foo2, foo3... you should take a second look at using an array, because it will likely serve you well. In this case you already were, but then you went awry and started making pointless variables.
    Using an array makes your code more expandable, maintainable, and easier to make more robust.

      there's a small bug in your code, Sifmole:

      ($choice <= scalar(@user_files)) # should be ($choice < scalar(@user_files))
      as you are now asking for the number of array elements after you unshifted the blank element, e.g.
      0 1 2 blank file1 file2
      and scalar(@user_files) = 3 but $user_files[3] is undefined!

      -- Hofmator

      I'm a little confused on the "blank" in the unshift statement? Should I try "blank" or should this be an actual var of some kind. Sorry for all of the newbie questions. Bradley
      Where ever there is confusion to be had... I'll be there.
        Perl arrays start with an index of 0. That is, the first element of @items can be accessed as $items[0]. Adding a blank element to the start of the array means that if the user requests file 1, you can just say @user_files[$choice] instead of $user_files[$choice - 1].

        I'd rather do $choice - 1, but either way, add a comment to the code noting your choice and why you're doing it:

        # @user_files starts at zero, user choice starts at 1 process($user_files[$choice - 1]);
        That'll make life much easier for the maintainer.