in reply to Re: Link a hash and an array
in thread Link a hash and an array

That is close, but not exactly what I want to do.

I did not put enough detail in my starting post.

I have an array of values, the first one of which indicates what sort of data is in the array. I also have a set of arrays that indicate what fieldnames should be associate with the array.

Think of the array as sometimes being name and address, and sometimes name and phone number. I want to read $ARRAY[0] and then decide how the hash will be created. What I tried to do that does not work is this:

@Address=qw(type firstname lastname streetaddress city zip); @Phone=qw(type name phonenumber); %Stuff; sub ReadStuff{ if ($@_[0]='addr') {%Stuff(@Address)=@_}; elsif ($@_[0]='phone'){%Stuff(@Phone)=@_}; ...do something useful... } @data=(addr, bob, smith, 1234 main st, anytown, 20500); &ReadStuff(@data); @data=(phone, bob, smith, 212-555-1212) &ReadStuff(@data)
But, of course, that does not work exactly like that, so I am trying to get close. Skip

Replies are listed 'Best First'.
Re: Re: Re: Link a hash and an array
by revdiablo (Prior) on Mar 11, 2004 at 22:54 UTC
    But, of course, that does not work exactly like that

    It would probably help if you used syntactically valid Perl code. If you did, you might be surprised to find that what you have works fine. Here I've gone through the trouble to port it for you:

    #!/usr/bin/perl use strict; # safety checks use warnings; # useful hints use Data::Dumper; # notice the use of 'my' to declare a lexical scope my @Address=qw(type firstname lastname streetaddress city zip); my @Phone=qw(type name phonenumber); sub ReadStuff { # instead of using funny global variables, scope the # hash within the subroutine, and return a reference at # the end my %Stuff; # $_[0] is the first element of the array @_ # eq is the string equality test # @h{@k} = @v is the syntax for hash slices if ($_[0] eq 'addr') { @Stuff{@Address} = @_ } elsif ($_[0] eq 'phone') { @Stuff{@Phone} = @_ } # return a reference (as mentioned before) return \%Stuff; } # let's quote our list elements this time my @data1 = ('addr', 'bob', 'smith', '1234 main st', 'anytown', '20500 +'); my $stuff1 = ReadStuff(@data1); my @data2 = ('phone', 'bob', 'smith', '212-555-1212'); my $stuff2 = ReadStuff(@data2); # dump the results from both runs print Dumper $stuff1; print Dumper $stuff2;

    Update: while this code does work, it doesn't quite do what your original question asks. That said, what you have here is probably exactly what I would suggest -- that is, keep the data in one place, and a list of keys in the other to tie it together. Using the structures this code creates, you could, for example, do $Stuff1->{$Address[4]} to get the field from $Stuff1 that corresponds to the key at index 4 from @Address. (whew! try saying that 5 times fast!)

      Thank you.

      I am currently crawling through the replies and making sense of them. I had to spend a couple of hours with use strict and use warnings turned on to clean up my lousy syntax.

      Obviously I am a bit of a novice (to Perl, I have been whacking keys and writing bad code for about fifteen years). Yes, I have the Llama, access to a Camel, and the Cheeta.

      Thank you all.

      Thank you very much for the recodeing. I have found it very edifying. It does work, and work quite well, and I am adding it to my mental perl toolset. But it does not do exactly what I need to do.

      What I need to do is store the data in an array and access it from a hash. I need to be able to make a change using the a hash variable and have it affect the array variable.

      You have shown me how to copy the data from an array into a hash using key names from another array. A valuable function, but not what I need.

      Thank you,

      Skip

        But it does not do exactly what I need to do.

        Yeah, that's what I pointed out in my Update. But as I said there, I think it might be a bad idea to keep the actual data in two places. Even using references, there's the chance that the references get stale and you're pointing to different data. I would strongly suggest picking either the hash, or the array, and keep the data there. Then, in the other one, store nothing but indexes (unique keys) that show where to get the data.

        Perhaps an example will make it more obvious. Here I will store the actual data in arrays, but have a lookup table to map hash keys to array indexes:

        my %index = (firstname => 0, lastname => 1, address => 2, town => 3, zip => 4); my @array1 = ("Bob", "Smith", "1234 Main St", "Anytown", "20500"); my @array2 = ("Joe", "Blow", "4321 Street Ln", "Notown", "20050"); print "First name (from array1): $array1[ $index{firstname} ]\n"; print "Address (from array2): $array2[ $index{address} ]\n";

        Notice that we can fairly easily access the data via key name, but we don't have to worry about making sure two disperate structures stay in sync. The only thing we have to worry about is the keys-to-indexes map getting out of date -- but hopefully that is a rare problem.

        Another approach that you might find interesting is to just use an array, and have constants to store the array indexes. This would give the advantage of not quite as verbose a syntax. Here's an example of that:

        use constant { FIRSTNAME => 0, LASTNAME => 1, ADDRESS => 2, TOWN => 3, ZIP => 4 }; my @array1 = ("Bob", "Smith", "1234 Main St", "Anytown", "20500"); my @array2 = ("Joe", "Blow", "4321 Street Ln", "Notown", "20050"); print "First name (from array1): $array1[ FIRSTNAME ]\n"; print "Address (from array2): $array2[ ADDRESS ]\n";
Re: Re: Re: Link a hash and an array
by QM (Parson) on Mar 11, 2004 at 21:30 UTC
    Maybe like this (without any error checking):
    our @Address=qw(type firstname lastname streetaddress city zip); our @Phone=qw(type name phonenumber); our %Types = ( "addr" => \@Address, "phone" => \@Phone ); our %Stuff; sub ReadStuff{ @Stuff{@{$Types{$_[0]}}} = @_; #...do something useful... }

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      @Stuff{@{$Types{$_[0]}}} = @_;

      This syntax is interesting. I have now copied it without fully understanding it. This part:

      @{$Types{$_[0]}}

      Seems to indicate, "Pull me the contents of this array element, and return it to me as an array". Am I correct?

      The outer curly braces intrige me. I am sure that I read somewhere that the syntax should be:

      @$Types{$_[0]}

      Which I tried with little success. The braced version does work, but I hate to do things by magic. What do they mean?

      Again, Thank you,

      Skip

        Sorry, I should have broken it down for you.

        @Stuff{@{$Types{$_[0]}}} = @_;
        Working from the inside out. $_[0] is the 0th element of @_. If its value is "addr", then the value of $Types{"addr"} is \@Address (a reference to the global @Address).

        @{\@Address} takes the reference and "returns" the array it points to. @Stuff{@Address}, known as a "hash slice", is assigned the values in the @_ array, and is equivalent to this:

        @Stuff{@Address} = qw(addr bob smith '1234 main st' anytown 20500);
        A hash slice is just shorthand for this:
        $Stuff{type} = 'addr'; $Stuff{firstname} = 'bob'; ... $Stuff{zip} = '20500';

        [I hope that was clear]

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of