Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

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
0
walw
Asked:
walw
1 Solution
 
prady_21Commented:
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
 
0
 
prady_21Commented:
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
 
0
 
prady_21Commented:
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
biglugCommented:
foreach $line(<DATA>) {
   chomp $line;
   my ($who,$what) = $line =~m/(\w+)\W*(\w*)$/i;
   push(@{$hash{$who}}, $what);
}

Which will make a hash of lists. Eg:
$hash{My}[0] eq 'Computer'
$hash{My}[1] eq 'Mouse
$hash{My}[2] eq 'Keyboard'
$hash{Your}[0] eq 'Speakers'
$hash{Your}[1] eq 'Laptop'
$hash{Your}[2] eq 'Apple'

Then if you need the string:
foreach $key (sort keys %hash) {
   printf("%s: %s\n", $key , join(', ',sort @{$hash{$key}}));
}

Which prints:
My: Computer, Keyboard, Mouse
Your: Apple, Laptop, Speakers

0
 
TintinCommented:
If you want an array of hashes, use something like (as details in perldsc):

while (<>) {
  chomp;
  push @aoh, { split(/:/) };
}
0
 
biglugCommented:
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!
0
 
biglugCommented:
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'
0
 
walwAuthor Commented:
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
0
 
prady_21Commented:
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
0
 
biglugCommented:
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}})
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now