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

Hello, Hoping you can help me here.

I'm trying to write a perl script however am I lots of difficulty with a particular sub routine which is essentially grepping a log files, depending on which exist. I'm starting to test with just two log files, but there will be about 8 to select from in the end so hence the usage of subs.

Anyway, I' attempting to check if a 12 Log exists, if it does then assign that scalar to be used later in the loop as the array:

sub GetDecUser { if (-e $LOGFILE12) { $LFILE = \@arr12; # if the 12 log exist, use it } else { $LFILE = "\@arr10"; # $LFILE = @arr10; # Tried this and this just seems to loop throu +gh the whole log } print "$LFILE\n"; # this prints @arr10, so the scalar is getting po +pulated for $dec ("$LFILE") # Use what ever log was found in the above if s +tatement #for $dec (@arr10) # this works fine, but its not using the assignm +ent! { if ($dec =~ /declared users/) # match this { print "TESTING we found it: $dec\n"; # Nothing outputted + when I try this. } } }
Many Thanks (yes I know I can do this easily in shell, but am trying to learn some perl)

Replies are listed 'Best First'.
Re: Assign scalar to array
by cdarke (Prior) on May 18, 2010 at 15:26 UTC
    Your code would be easier to read if you used consistent indentation - honest! It will also help you if you always use strict; and use warnings;
    The use of quotes is part of your problem. The \ is an operator, and \ has no effect inside double quotes. Update: that's rubbish, of course it has an effect, what was I thinking?

    Here is my version, with some variables added to make it work:
    use strict; use warnings; sub GetDecUser { my $LFILE; my $LOGFILE12 = $0; my @arr12 = ('The','quick','brown','declared users'); my @arr10 = qw(The quick brown fox); if (-e $LOGFILE12) { $LFILE = \@arr12; # if the 12 log exists, use it } else { $LFILE = \@arr10; } print "$LFILE\n"; for my $dec (@$LFILE) { if ($dec =~ /declared users/) # match this { print "TESTING we found it: $dec\n"; } } } GetDecUser();

      The \ is an operator, and \ has no effect inside double quotes.

      That's not true. It most definitely had an effect (escaped the "@" to prevent interpolation). Just not the desired one.

        Dear Monks,

        This is working great now! Many thanks for your patience and assistance

Re: Assign scalar to array
by ikegami (Patriarch) on May 18, 2010 at 15:04 UTC

      Thank you for you're reply, but this did not produce any different results

        First, start by using use strict; use warnings;. You also have extra quotes in $LFILE = "\@arr10"; use strict; would have caught that.

        use strict; use warnings; my $LOGFILE12 = '...'; sub GetDecUser { my $LFILE = -e $LOGFILE12 ? \@arr12 : \@arr10; return grep /declared users/, @$LFILE; } print "$_\n" for GetDecUser();
Re: Assign scalar to array
by jwkrahn (Abbot) on May 18, 2010 at 17:02 UTC

    It looks like you should be using a Hash of Arrays or an Array of Arrays instead of twelve separate scalars and twelve separate arrays.