?
Solved

Storing items in the Hash's Array

Posted on 2003-03-16
10
Medium Priority
?
214 Views
Last Modified: 2010-03-05
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
Comment
Question by:walw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 3

Expert Comment

by:prady_21
ID: 8148943
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
 
LVL 3

Expert Comment

by:prady_21
ID: 8148944
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
 
LVL 3

Expert Comment

by:prady_21
ID: 8149035
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.

 
LVL 1

Accepted Solution

by:
biglug earned 225 total points
ID: 8149167
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
 
LVL 48

Expert Comment

by:Tintin
ID: 8149203
If you want an array of hashes, use something like (as details in perldsc):

while (<>) {
  chomp;
  push @aoh, { split(/:/) };
}
0
 
LVL 1

Expert Comment

by:biglug
ID: 8149231
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
 
LVL 1

Expert Comment

by:biglug
ID: 8149238
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
 

Author Comment

by:walw
ID: 8149317
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
 
LVL 3

Expert Comment

by:prady_21
ID: 8149404
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
 
LVL 1

Expert Comment

by:biglug
ID: 8149436
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question