Link to home
Start Free TrialLog in
Avatar of walw
walw

asked on

Storing items in the Hash's Array

Hello, lets say there is a list like this

My   : Computer
Your : Speakers
My   : Mouse
My   : Keyboard
Your : Laptop
Your : Apple

I am trying to store them into a hash, where the key would be "My" and "Your", and whatever the input might be.
The input is coming from standand input, and the keys and items will always be different.. I am just unfamiliar with the Perl syntax that will allow me to keep on storing the items once I know the key

This is what I have so far:

while (<>){
   chomp;
   @line = split(m/\s/);               # this will let me get My, :, and Computer
   $hash{$line[0]} = $line[2];         # but I need to modify this line, for it to store it into an array
}

Anyone can help me?:)  Thanks in advance!



Edit: After it reads it in, it should be in lex. order too, like this


My : Computer, Keyboard, Mouse
Your : Apple, Laptop, Speakers
Avatar of prady_21
prady_21

while(<>) {
chomp;
($var1,$var2)=(split(\s));
if ( !exists $hash{$var1} ) {
   $hash{$var1}{'all'}->[0] = 1;
   $hash{$var1}{'all'}->[1] = $var2;
}
else {
   $value = $hash{$var1}{'all'}->[0];
   $hash{$var1}{'all'}->[$value] = $var2;
}

This creates another level of hashes, but is working perectly fine with my code.
If there is anything do let me know
 
while(<>) {
chomp;
($var1,$var2)=(split(\s));
if ( !exists $hash{$var1} ) {
   $hash{$var1}{'all'}->[0] = 1;
   $hash{$var1}{'all'}->[1] = $var2;
}
else {
   $value = $hash{$var1}{'all'}->[0];
   $hash{$var1}{'all'}->[$value] = $var2;
}

This creates another level of hashes, but is working perectly fine with my code.
If there is anything do let me know
 
oops!!!!! i am sorry
i forgot one more line
add this line at the beginning of else condition
$hash{$var1}{'all'}->[0]++;

i am sorry abt that
ASKER CERTIFIED SOLUTION
Avatar of biglug
biglug

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tintin
If you want an array of hashes, use something like (as details in perldsc):

while (<>) {
  chomp;
  push @aoh, { split(/:/) };
}
prady 21: I'm not sure what yours is doing ..

1. I can't get it to compile .. firstly you need to  put /\s/ inside the split (which doesn't need parens around it)

2. Your Split will give you 'My' and ' : Computer'

3. You then test to see if the hash key 'My' exists, if it doesn't, we set $hash{My}{all}[0] to 1 .. what's that for? There's no mention of 'all' in the question.

4. We then set $hash{My}{all}[1] to ' : Computer'

5. If we already have $hash{My}, then we increment $hash{My}{all}[0], So $hash{My}{all}[0] contains a count of nouns associated with the person.

6. Now we set $value to whatever was in $hash{My}{all}[0] (which we'll say is 2)

7. Then we set $hash{My}{all}[2] to the next noun (which we'll say is ' : Mouse'

OK, so lets see what we have at the end of walw's example data:

$hash{My}{all}[0] == 3
$hash{My}{all}[1] eq ' : Computer'
$hash{My}{all}[2] eq ' : Mouse'
$hash{My}{all}[3] eq ' : Keyboard'
$hash{Your}{all}[0] == 3
$hash{My}{all}[1] eq ' : Speakers'
$hash{My}{all}[2] eq ' : Laptop'
$hash{My}{all}[3] eq ' : Apple'

Which doesn't seem to answer wolw's question at all!  Instead we have a needlessly complex data structure containing useless data!

You say "This creates another level of hashes, but is working perectly fine with my code.", however it doesn't. Please don't claim to answer a question with a 'tested' answer, that won't even compile!
Prady: That data structure should have been:
$hash{My}{all}[0] == 3
$hash{My}{all}[1] eq ' : Computer'
$hash{My}{all}[2] eq ' : Mouse'
$hash{My}{all}[3] eq ' : Keyboard'
$hash{Your}{all}[0] == 3
$hash{Your}{all}[1] eq ' : Speakers'
$hash{Your}{all}[2] eq ' : Laptop'
$hash{Your}{all}[3] eq ' : Apple'
Avatar of walw

ASKER

Thanks for the help guys. The syntax gave me the guidelines for my task to perform, and I was able to figure the rest out myself.

Thanks everyone for their input
mr biglug
sorry abt that
well the truth was that only part of the answer was tested and it was working( split wasnt included).
and for your kind information it did compile

well what i am trying to do is store the no of elements in all->{0} and then store the elements in [1] and so on.

and for the "all" part, i gracefully accept that it was a mistake, which i think our programmer could just have deleted by himself
it isnt any complex and no useful data if the all part will be removed
and finally if you have any data , u can definitely ask me
prady 21:
It's still bad form to store the number of elements inside the array .. especially when its just used to put an element in there. Use push(). If you need to know how many elements are there, use scalar(@{$hash{My}})