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

Hi Everybody

I've been writing a script to automate the averaging of a set of values from text files, before performing some transformations on them. In order to do this I have needed to define lists containing the identity of the parts of the text file I'm wanting to average (in this case, the residue numbers for protein domains). Initally I set up several seperate lists (@domain_A, @domain_B...) and to test whether a residue was in that list used the vec command as below

my $A; my $i; my @domain_A = (1..119); for (@domain_A) { vec($A,$_,1) = 1 } for ( $i = -100 ; $i <= 2000 ; $i++ ) { if (vec($A,$i,1)){ print "\t $i recognised as part of the 1st domain using the ol +d method\n"; } }
This worked well. However, to make the script easier to apply to other problems, I am changing to using a multidimensional array. However performing a similar search no longer works and I'm not sure why, or if i can continuing using the vec command. Any advice would be much appreciated, as this is thoroughly confusing me.
my @domains = ( [1..119], [325..648], [649..879], [880..1110], [1111..1229], [1230..1354] ); my $i; my @total_coords = (); for ( $i = 0 ; $i <= 5 ; $i++ ) { push @total_coords, ( [0,0,0] ); my $scratch = "domains$i" ; for (@{$domains[$i]}) { vec($scratch,$_,1) = 1} } for ( $i = -100 ; $i <= 2000 ; $i++ ) { if (vec("domains$i",$i,1)){ print "\t $i recognised as part of the 1st domain using the ne +w method\n"; } }
Thanks in advance

Ben

Replies are listed 'Best First'.
Re: Using vec to search an array
by tlm (Prior) on Jun 15, 2005 at 13:28 UTC

    If I understand your problem correctly, it sounds formally similar to the one discussed in this thread. I think Roy Johnson's solution may be of particular interest to you.

    the lowliest monk

Re: Using vec to search an array
by monarch (Priest) on Jun 15, 2005 at 13:32 UTC
    From perldoc -f vec it says:

    vec EXPR,OFFSET,BITS Treats the string in EXPR as a bit vector made up of elements +of width BITS, and returns the value of the element specified by OFFSET as an unsigned integer.

    You're doing nasty stuff with references to variable names, which would flail with use warnings so you code like this at your own peril.

    First, line 16, change from

    for (@{$domains[$i]}) { vec($scratch,$_,1) = 1}
    to
    for (@{$domains[$i]}) { vec($$scratch,$_,1) = 1}

    Next, the ending for loop, should written as follows:

    for ( $j = 0; $j <= 5; $j++ ) { my $scratch = "domains$j"; for ( $i = -100 ; $i <= 2000 ; $i++ ) { if (vec($$scratch,$i,1)){ print "\t $i recognised as part of the 1st domain using the ne +w method\n"; } } }
    Note the second for loop, and the use of the scratch variable.

    I would never personally code like this, but hopefully it is of some assistance to you.

    Update: fixed a mis typed code tag.

Re: Using vec to search an array
by Tomtom (Scribe) on Jun 15, 2005 at 13:30 UTC
    I'm not sure it has something to do with it, but you're assigning $scratch the value "domains$i", and then, in the last loop, in the vec you're getting "domain$i", without the 's'.
      Sorry, that was an unrelated typo. I've updated the code, Thanks,

      Ben