Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

hash problems

by Anonymous Monk
on Jul 08, 2004 at 10:42 UTC ( [id://372742]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, I am new to hashes etc and wondered if you could help. I have two arrays placed in a hash: one contains numbers, the other contains + and - signs related to each number. I have a third array not in the hash which i am using to compare to the numbers in the hash and extract the corresponding sign when they match. I know that hashes are mixed up (e.g. not in insertion order), but when my number matches, not only would I like to extract the corresponding sign, but i would also like to extract the next sign in the sequence e.g $i and $i+1 - this is the part that i'm confused about. Here's my code below - hope you can help !
my %hash = map { $numbers[$_] => $signs[$_]} 0 .. $#numbers; while (($key, $value) = each (%hash)) { if (@new_numbers) { for (my $i=0; $i<@new_numbers; $i++) { if ($key == $new_numbers[$i]) { print "$value \t $new_numbers[$i]"; # th +is prints the sign and the number it matches to } } }

Replies are listed 'Best First'.
Re: hash problems
by murugu (Curate) on Jul 08, 2004 at 11:09 UTC

    May be I have misunderstood the question,

    I have made some variations in ur code. Instead of using hashes i have used arrays.

    @numbers=(1,2,3,4); @signs=('+','-','*','+'); @thirdarray=(2,3); my @new = map { [$numbers[$_],$signs[$_]]} 0 .. $#numbers; foreach my $i (0..$#new) { print $new[$i+1]->[0],$new[$i+1]->[1]."\n" if (grep{/^$new[$i]->[0]$/} +@thirdarray); }

    Cheers.

      thanks murugu, i think i want something like this but i have tried it and it doesn't quite do it. from your example, the desired output would be:
      2-+ 3++
      How could i modify your code to do this? Many many thanks
Re: hash problems
by PerlingTheUK (Hermit) on Jul 08, 2004 at 11:13 UTC
    If I understand yout Problem right this might do:
    my $ptr; for ( my $i = 0; $i < @numbers; $i++ ){ $ptr->{$i}->{ number } = $numbers[$i]; $ptr->{$i}->{ sign } = $signs[$i]; }
    This is a reference for a hash. You can now get your values back as:
    while (@new_numbers){ my $i = $_; if ( defined $ptr->{$i} ){ print $ptr->{$i}->{ sign }.$ptr->{$i}->{ number }."\t".$i; } }
    This is not tested but should work. It uses references to a hash. Alternatively you might want to make $i part of the keys:
    $hash{ $numbers[$i].":".$i } = $signs[$i];
    But it would be much more complicated sorting the keys out. Hope this is what you wanted.
Re: hash problems
by Roy Johnson (Monsignor) on Jul 08, 2004 at 14:04 UTC
    Do you just want to
    print "$value \t $new_numbers[$i] $new_numbers[$i+1]\n";
    ? You would want to modify your for loop range (as you should, anyway) so that $i+1 doesn't go beyond the actual array. Instead of $new_numbers[$i+1], you might want $hash{$key+1}. It's hard to tell what you expect your code to do. What is @new_numbers?

    We're not really tightening our belts, it just feels that way because we're getting fatter.
Re: hash problems
by perldeveloper (Scribe) on Jul 08, 2004 at 11:11 UTC
    If the order matters, then you'll have to use arrays. Better assign two keys to your $hash, one for an array of numbers (say @{$hash->{numbers}}) and one for an array of signs (@{$hash->{signs}}). Then look within the numbers array (@{$hash->{numbers}}, find the corresponding $i (that is $hash->{numbers}->[$i]) and extract the two signs: $hash->{signs}->[$i] and $hash->{signs}->[$i + 1].

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found