in reply to Assigning a variable to a Hash key reference

(yes, I know it needs to be cleaned up) My script works, but I'm trying to move stuff around so that the variables can be accessed outside the block. Here's what I have now, that works:
...open the file... my %menuhash1 = (); my @fields1 = split(/\t/, <FILE>); chomp @fields1; my @records1; my $prodlisting; my %ref1; while(<FILE>) { chomp; my @row1 = split(/\t/); my %data1; @data1{@fields1} = @row1; chomp @row1; push @records1, \%data1; } close (FILE); foreach my $ref1 ( sort {(-1,0,1)[rand 3]} @records1 ){ my $thumbgif = "thumb.gif"; my $menudir = "$ref1->{'dir'}"; $menudir = "$menudir/"; my $thumb = "$webdir\/$menudir$thumbgif"; print "<img align=\"center\" border=\"0\" src=\"$thumb\">"; etc...
I'm trying to move the "my's" outside the block. $menudir is the problem.

Maybe I just need a nap.

Replies are listed 'Best First'.
Re: Re: Assigning a variable to a Hash key reference
by jarich (Curate) on May 28, 2002 at 07:02 UTC
    I'm trying to move the "my's" outside the block. $menudir is the problem.

    my $thumbgif = "thumb.gif"; # no need to redeclare this every time. my $menudir; foreach my $ref (sort {(-1, 0, 1)[rand 3]} @records) { $menudir = $ref->{dir}; my $thumb = "$webdir/$menudir/$thumbgif"; print qq{<img align="center" border="0" src="$thumb">}; }

    Is that all you wanted? $menudir should never have been a problem to move outside the block. However please note that if you use what I've written $menudir after the block will contain only the value that it was set to on the last iteration.

    As such, unless you specifically care about it's last value it really should be localised to that block.

    Hope this helps.

    jarich