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

I can't seem to get this right:
while(<FILE>) { chomp; my @row = split(/\t/); my %data; @data{@fields} = @row; chomp @row; push @records, \%data; } close (FILE); my $menudir = "($ref(@records)->{ven})";##WRONG
Hopefully you can tell what I'm trying to do. How do I assign the value 'ven' to a variable? Thanks.

Replies are listed 'Best First'.
Re: Assigning a variable to a Hash key reference
by mattriff (Chaplain) on May 28, 2002 at 02:54 UTC
    Hopefully you can tell what I'm trying to do.
    Maybe it's just me, but I'm not sure at all what you're attempting to do with that last line. Could you explain a bit more?

    On a very minor note: unless you've changed the value of $/ somewhere earlier in your script, the chomp @row line in your while() loop probably isn't doing anything.

    Actually, you could get rid of @rows altogether and just assign the output of the split to @data{@fields}, but whether you should is a somewhat 'religious' issue that has no bearing on the question you are actually asking. :)

    UPDATE: Is $menudir supposed to be an array reference of the value of key 'ven' from each of the hash references? If so, you might try something like:

    my $menudir = [map { $_->{ven} } @records];

    - Matt Riffle

Re: Assigning a variable to a Hash key reference
by malaga (Pilgrim) on May 28, 2002 at 03:54 UTC
    (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.
      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

Re: Assigning a variable to a Hash key reference
by Aristotle (Chancellor) on May 28, 2002 at 16:44 UTC
    "($ref(@records)->{ven})"
    You realize that
    1. $ref(@records) is using round brackets which indicate a subroutine call,
    2. you cannot call a subroutine on a CODEREF that way if that was what you are trying to do (it would be $ref->(@records)),
    3. and that it won't actually evaluate to a subroutine call inside quotes.
    I think you need to explain some more.

    Makeshifts last the longest.