Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Get same position value in 2nd array

by Phinix (Acolyte)
on Dec 18, 2012 at 17:37 UTC ( [id://1009435]=perlquestion: print w/replies, xml ) Need Help??

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

Good morning monks. In the project I am working on, I can see the need soon to set up such a reference system. I would just use hashes, but the look-up values themselves are actually variables made up of a scalar and a string of text appended to them and I'm not sure how that can be set up.

Basically I want a loop to check for each value in an array, if each scalar is greater than zero. If so, I want to do something with the corresponding value in a 2nd array.

If there is a way to do this with hashes that would be cool too. I might be able to re-combine the scalar and string values to re-populate the 2nd array before processing my loop.

Replies are listed 'Best First'.
Re: Get same position value in 2nd array
by AnomalousMonk (Archbishop) on Dec 18, 2012 at 18:08 UTC

    What frozenwithjoy said, but here's a first pass at something like what "... use hashes, but the look-up values themselves are actually variables made up of a scalar and a string of text appended to them ..." might look like:

    >perl -wMstrict -le "my $foo = 'Bar'; ;; my %hash = ( 'BarBaz' => 'Quux' ); ;; print $hash{ $foo . 'Baz' }; " Quux

    Update: Likewise for the OP second paragraph (but a hash might be better here):

    >perl -wMstrict -le "my @ra1 = ( 0, 1, 2, 0, 3, -1); my @ra2 = qw(eeew ok fine yech great bad); ;; RA1: for my $i (0 .. $#ra1) { next RA1 unless $ra1[$i] > 0; do_something_with($ra1[$i], $ra2[$i]); } ;; sub do_something_with { print qq{$_[0]: '$_[1]'}; } " 1: 'ok' 2: 'fine' 3: 'great'
Re: Get same position value in 2nd array
by BrowserUk (Patriarch) on Dec 18, 2012 at 19:03 UTC

    It is hard to see why you are having a problem with this:

    #! perl -slw use strict; my @first = map int( -5 + rand 10 ), 1 .. 20; my @second = 1 .. 20; for my $i ( 0 .. $#first ) { if( $first[ $i ] > 0 ) { ## do something with $second[ $i ]; print "first[ $i ] = $first[ $i ]; second[ $i ] == $second[ $i + ]"; } } __END__ C:\test>junk38 first[ 2 ] = 2; second[ 2 ] == 3 first[ 3 ] = 4; second[ 3 ] == 4 first[ 7 ] = 2; second[ 7 ] == 8 first[ 8 ] = 2; second[ 8 ] == 9 first[ 9 ] = 3; second[ 9 ] == 10 first[ 10 ] = 4; second[ 10 ] == 11 first[ 12 ] = 1; second[ 12 ] == 13 first[ 18 ] = 1; second[ 18 ] == 19 first[ 19 ] = 1; second[ 19 ] == 20

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: Get same position value in 2nd array
by jethro (Monsignor) on Dec 18, 2012 at 18:52 UTC

    From your sentence that starts with "Basically" I would conclude that you have 3 values per item, a scalar aka number(A), a string(B) and another value(C). I would guess A has duplicate values (0 for example)? Does B have duplicate values? Does the combination of A and B have duplicate values?

    What is the operation that you have to do the most? Checking for values greater than zero? Or searching for C given A and/or B ?

    These questions are all important. For example if B has no duplicate values and you most often have to search for C given B you might just use B as key and a subhash or subarray with A and C as data:

    $h{$B}= {'number'=>$A,'value'=>$C}; print $h{$B}{value}; if ($h{$B}{number}>0) { ...

    Or if checking for values greater than zero is your main activity and most of the scalars(A) are zero then it might even be sensible to split the data into two arrays (or hashes), one for all data with A==0 and another for the data with A>0.

Re: Get same position value in 2nd array
by blue_cowdawg (Monsignor) on Dec 18, 2012 at 20:50 UTC
        Basically I want a loop to check for each value in an array, if each scalar is greater than zero. If so, I want to do something with the corresponding value in a 2nd array.

    perhaps the answer to your homework looks like:

    | | handwaving here... starting with @ry1 and @ry2 being fully populated +. for($i=0;$i<=($#ry1 < $#ry2? $#ry1 : $#ry2){ if ($ry1[$i] != 0 ) { $ry[$i] = <do something> # edit accordingly } }
    I'm not too sure what your first paragraph is about, it confuses me.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Get same position value in 2nd array
by frozenwithjoy (Priest) on Dec 18, 2012 at 17:51 UTC
    Hi Phinix, Can you please edit your post to include concrete examples of what you are trying to do as well as the code for what you've tried so far? That would make it a lot easier to understand your goal and approach. Thanks.
Re: Get same position value in 2nd array
by muba (Priest) on Dec 22, 2012 at 01:10 UTC

    A good way to approach a problem like this, and you don't know how, is to think how you would do it in real life and then tell the computer to to that exact same thing.

    So we need a simile here.

    You have, in front of you, a partly filled in crosswords puzzle. This is array 1. Your friend, whom you're on the phone with, has the exact same crosswords puzzle, but still entirely blank. This is array 2. What you and your friend want, is to solve this crossword puzzle over the phone but you want to be "on the same page", so the two of you have decided that you will give him all the answers you've already found.

    How would you do this? You would walk through your answers and using their numbers as a reference, you relay the words you've found. So what you would say would sound like this: "One horizontal is monkey. Two vertical is bisshop. Fourty-six horizontal is mountain."

    In other words, as you iterate over array 1, you keep track of the "position" you're at, and then use that to do the corresponding position in array 2 &mdash in this case, tell your friend to fill in the value you have at that position.

    Which is exactly what BrowserUk and blue_cowdawg are doing above, here and here.

Re: Get same position value in 2nd array
by Phinix (Acolyte) on Dec 21, 2012 at 17:02 UTC

    Perhaps I can clarify a bit. Consider the following code:

    use strict; my @numbers = qw/1 2 3 4 5/; my %wht = qw/number1 $numbers[0] number2 $numbers[1] number3 $numbers[ +2] number4 $numbers[3] number5 $numbers[4]/; while ( my($key, $value) = each(%wht) ) { if ($value ne '0') { print "$key = $value\n" } }

    My goal is to have the if statement print the string contained in the key position followed by the actual value (numerical in this example) of the array value in the values position. So, for the first table entry, it should print "number 1 = 1". However, what the above produces is instead "number1 = $numbers[0]", etc.

    So, how can I do this sort of lookup on a hash key and work with the actual value of the variable in the value position, not just it's string identifier?

    This is not "homework." I am simply unfamiliar with the syntax, and documentation on this sort of operation is extremely sparse.
Re: Get same position value in 2nd array
by Phinix (Acolyte) on Dec 21, 2012 at 03:30 UTC

    I have decided to attempt this with hashes, however am now confused as to why my loop doesn't evaluate the table in order. For example, this code:

    use strict; my %table = qw/test0 0 test1 1 test2 2 test3 3 test4 4 test5 5 test6 6 + test7 7/; while ( my($key, $value) = each(%table) ) { if ($value ne '0') { print "$key = $value\n" } }

    ...produces the following result:

    test5 = 5

    test7 = 7

    test1 = 1

    test4 = 4

    test6 = 6

    test3 = 3

    test2 = 2

      I have decided to attempt this with hashes

      You know you want ordered results, but decide to use an inherently unordered data structure; that's like wanting to travel to Vegas and buying a boat.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

      Sure its evaluated in order :) it is not the order you want, but thats just how hashes work

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1009435]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-19 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found