Re: hash

[email protected] (Bobby)
Newsgroups perl.beginners
Message-ID <[email protected]>
This is what i'm trying to accomplish. I want to be able to print the equivalent EU to US sizes. For example in the flat file data, the US size 10 has the equivalent of Euro 34, US 11=35 Euro. My idea was to put the US sizes in a hash (%US) and the Euro in another hash (%EU) ; use the pid as the key in both hashes, loop through and compare if key (pid) exists in both then print the equivalent sizes $US{$us_size} - $EU{euro_size}. It seems easy enough, but i think my syntax or use of hash is incorrect. 

pid|us_size|euro_size
1|10|34
2|11|35
3|12|37
4|13|38

Rob Dixon <[email protected]> wrote: Bobby wrote:
> 
> Rob Dixon  wrote:
>>
>> Bobby wrote:
>>>
>>> I have a flat file that contains a pid, us_size and euro_size. I want to
>>> create read in the file and create one hash for the us_size (%US) and the
>>> other for the euro_size (%EURO). Then i want to do a print statement if the
>>> pid value in the us_size hash is equal to pid value in the euro_size
>>> hash...maybe IF ($US{$pid} = $EURO{$pid}) {print statement...}.
>>>
>>> The part where i'm stuck on is how to assign the data into a hash and do the
>>> comparison, could one of you help me with the Perl's syntax or point in the
>>> right direction?
>>>
>>> pid|us_size|euro_size
>>> 1|10|34
>>> 2|11|35
>>> 3|12|37
>>> 4|13|38
>> 
>> Another question that skips the /problem/! Are you aware that you can store
>> multiple values - even another hash or array - as the value of a hash element?
>> 
>> At a guess, you need to be able to access a US and EU size for a given PID, and
>> I would write a loop like this
>> 
>>   my %pidsizes;
>> 
>>   while () {
>>     chomp;
>>     my ($pid, $us, $eu) = split /\|/;
>>     $pidsizes{$pid}{US} = $us;
>>     $pidsizes{$pid}{EU} = $eu;
>>   }
>> 
>> Does that help? Are the PIDs in your file unique? Is the missing Euro size for
>> PID 4 a mistake or a special case?
> 
> Yes the pids are unique. 
> 
> I've tried the comparison below but didn't worked, so what do you think is wrong with it?
> 
> if ($pidsizes{$pid}{US} = $pidsizes{$pid}{EU}){
>         print "$pidsizes{US}{$us}";

(Please bottom-post your responses to this group, so that extended threads
remain comprehensible. Thank you.)

You are misunderstanding my intention as well as the structure of the hash. All
of the data in $pidsizes{$pid} belong to that one PID. There is no such thing as
$pidsizes{US}, and no need to ever compare the values of two hash elements to
correlate the data.

As I said, I am certain that your analysis of the problem is at fault. You
should try to explain what you need to do in general terms, without using Perl
code; then maybe we can offer some help towards a solution.

Rob

-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.