RE: Hash of arrays
Daniel Gross wrote, on Thursday, April 03, 2003 08:45 : > push @{$dirHash{$prefix}}, $item; : Seems to work. It doesn't generate a syntax error. So, value elements : within a hash are always references? : : BTW, in your opinion would this also work when $dirHash{$prefix} is : undefined (i.e. when no key was defined yet?) Ought to. I think I've done it, but I can't remember for sure. If it doesn't, you can always test: if ($dirHash{$prefix}) { push ... ## as above } else { $dirHash{$prefix} = [$item]; # create the arrayref } Good luck, Joe == Joseph P. Discenza, Sr. Programmer/Analyst mailto:[EMAIL PROTECTED] Carleton Inc. http://www.carletoninc.com 574.243.6040 ext. 300fax: 574.243.6060 Providing Financial Solutions and Compliance for over 30 Years * Please note that our Area Code has changed to 574! * ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Hash of arrays
Daniel Gross wrote: > Seems to work. It doesn't generate a syntax error. So, value elements > within a hash are always references? no, but they are always scalars :) > BTW, in your opinion would this also work when $dirHash{$prefix} is > undefined (i.e. when no key was defined yet?) perl has a feature called autovivification, that usually Does The Right Thing to create whatever you are trying to create. Here is a tested full example from your original post: #!/usr/bin/perl use strict; use warnings; my @dirArray = qw( abcdStuff1 acdeStuff1 abcdStuff2 Stuff2 acdeStuff2 Stuff1 ); my %dirHash = (); foreach my $item(@dirArray) { my $prefix = substr($item, 0,4); push @{$dirHash{$prefix}}, $item; } use Data::Dumper; print Data::Dumper->Dump([EMAIL PROTECTED], \%dirHash], [qw(*dirArray *dirHash)]); -- Alan F. Dickey - Interaction and Realization http://www.intac.com/~afdickey mailto:[EMAIL PROTECTED] VOX: 908-273-3232 Cell: 908-334-0932 ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Hash of arrays
Thanks, Seems to work. It doesn't generate a syntax error. So, value elements within a hash are always references? BTW, in your opinion would this also work when $dirHash{$prefix} is undefined (i.e. when no key was defined yet?) Thanks Daniel -Original Message- From: Joseph P. Discenza [mailto:[EMAIL PROTECTED] Sent: Thursday, April 03, 2003 8:36 AM To: Daniel Gross; [EMAIL PROTECTED] Subject: RE: Hash of arrays Daniel Gross wrote, on Thursday, April 03, 2003 08:34 : I running into problems again: This time I'd like to fill a hash with : arrays, however, the following push command generates a syntax error. : Any idea why : : My %dirHash = (); : : foreach my $item(@dirArray) { : $prefix = substr($item, 0,4); : push @dirHash{$prefix}, $item; : } Try this (untested): push @{$dirHash{$prefix}}, $item; Remember, it's a hash of array *references*; you have to de-reference the scalar that's in the hash in order to act on it as an array. Joe == Joseph P. Discenza, Sr. Programmer/Analyst mailto:[EMAIL PROTECTED] Carleton Inc. http://www.carletoninc.com 574.243.6040 ext. 300fax: 574.243.6060 Providing Financial Solutions and Compliance for over 30 Years * Please note that our Area Code has changed to 574! * ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Hash of arrays
"Daniel Gross" <[EMAIL PROTECTED]> writes: > I running into problems again: This time I'd like to fill a hash > with arrays, however, the following push command generates a > syntax error. Any idea why > My %dirHash = (); > foreach my $item(@dirArray) { > $prefix = substr($item, 0,4); > push @dirHash{$prefix}, $item; > } You can't push onto a hash. The syntax @dirHash{$prefix} is a hash slice. Q.E.D. I'm not quite sure what you're trying to do. Is it that you may have multiple items per prefix, and you want them all in the same hash element? If so, what you want is more like the following. Caution - cold code follows: foreach my $item (@dirArray) { $prefix = substr ($item, 0, 4); push @{$dirHash{$prefix}}, $item; } Tom Wyant This communication is for use by the intended recipient and contains information that may be privileged, confidential or copyrighted under applicable law. If you are not the intended recipient, you are hereby formally notified that any use, copying or distribution of this e-mail, in whole or in part, is strictly prohibited. Please notify the sender by return e-mail and delete this e-mail from your system. Unless explicitly and conspicuously designated as "E-Contract Intended", this e-mail does not constitute a contract offer, a contract amendment, or an acceptance of a contract offer. This e-mail does not constitute a consent to the use of sender's contact information for direct marketing purposes or for transfers of data to third parties. Francais Deutsch Italiano Espanol Portugues Japanese Chinese Korean http://www.DuPont.com/corp/email_disclaimer.html ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Hash of arrays
Daniel Gross wrote, on Thursday, April 03, 2003 08:34 : I running into problems again: This time I'd like to fill a hash with : arrays, however, the following push command generates a syntax error. : Any idea why : : My %dirHash = (); : : foreach my $item(@dirArray) { : $prefix = substr($item, 0,4); : push @dirHash{$prefix}, $item; : } Try this (untested): push @{$dirHash{$prefix}}, $item; Remember, it's a hash of array *references*; you have to de-reference the scalar that's in the hash in order to act on it as an array. Joe == Joseph P. Discenza, Sr. Programmer/Analyst mailto:[EMAIL PROTECTED] Carleton Inc. http://www.carletoninc.com 574.243.6040 ext. 300fax: 574.243.6060 Providing Financial Solutions and Compliance for over 30 Years * Please note that our Area Code has changed to 574! * ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Hash of arrays
Hello all, I running into problems again: This time I'd like to fill a hash with arrays, however, the following push command generates a syntax error. Any idea why My %dirHash = (); foreach my $item(@dirArray) { $prefix = substr($item, 0,4); push @dirHash{$prefix}, $item; } Thanks Daniel ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
creating array reference to one array of a hash of arrays
# I have a program that gets a hash from an API call. # the format of the hash is that each key corresponds to an # array. The following code snippet illustrates the data: $href->{One} = [1,2,3,4]; print ref($href)."\n"; print ref($href->{One})."\n"; # I would like to simplify the syntax to use a simple array # format by creating a new reference to the array to allow me # to use the conventional syntax ie: $aref[0] $aref = \@{$href->{One}}; print "$href->{One}=$aref\n"; # the output indicates that it did this... # but this next line does not give the expected results print "$href->{One}[0]=$aref[0]\n"; print "$href->{One}[0]=$aref->[0]\n"; #1. can I create a reference to this array that will allow me to # access it without the -> operator? #2. is there a simple explanation why I still need the -> operator? #Howard Jares #University of Houston #Due to the current budget constraints, the light at the end of the tunnel will be turned off until further notice. ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: hash of arrays
- Original Message - From: "Peter Eisengrein" <[EMAIL PROTECTED]> > That just adds a field to my output, like so: > > 7943641|ARRAY(0x1b9efb8)|| > > > Perhaps I'm contructing it wrong? I'm doing it like this... > > @_=$features{$dn}; > push(@_,$line[1]); > $features{$dn}=\@_; > That's kind of scary, first you're saying the key in %features has an array as content, then you're saying it has an array ref. It's likely you had an array ref, pulled back an array with one entry (an array ref) and then referenced back to that in the %features hash, avoiding doing that will make life less complicated. Look at something like this : my %features = (); foreach (1..10) { my @array = (1,2,3); $features{$_} = \@array; } foreach my $dn (sort {$a <=> $b} keys(%features)) { print("\n$dn | "); push(@{ $features{$dn} },"testline"); foreach (0..$#{ $features{$dn} }) { my $dat = $features{$dn}->[$_]; print("$dat |"); } } !c ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: hash of arrays
BINGO! Thanks Rob. > -Original Message- > From: Hanson, Robert [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, January 16, 2002 10:58 > To: 'Peter Eisengrein' > Subject: RE: hash of arrays > > > Hmmm... > > If $features{$dn} is a reference to an array you dereference > it like this... > > @{$features{$dn}} > > And you can also push onto it like this... > > push @{$features{$dn}}, 'foo'; > > So I would rewrite the code to sent to look like this... > > push(@{$features{$dn}},$line[1]); > > Rob > > > -Original Message- > From: Peter Eisengrein [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, January 16, 2002 10:45 AM > To: Hanson, Robert; Perl-Win32-Users Mailing List (E-mail) > Subject: RE: hash of arrays > > > That just adds a field to my output, like so: > > 7943641|ARRAY(0x1b9efb8)|| > > > Perhaps I'm contructing it wrong? I'm doing it like this... > > @_=$features{$dn}; > push(@_,$line[1]); > $features{$dn}=\@_; > > > > > > -Original Message- > > From: Hanson, Robert [mailto:[EMAIL PROTECTED]] > > Sent: Wednesday, January 16, 2002 10:43 > > To: 'Peter Eisengrein'; Perl-Win32-Users Mailing List (E-mail) > > Subject: RE: hash of arrays > > > > > > > > This is what you want... > > > > foreach my $dn (keys %features) > > { > > my @features = @{$features{$dn}}; > > > > print "$dn|"; > > foreach (@features) > > { > > print "$_\|"; > > } > > print "\n"; > > } > > > > Rob > > > > > > -Original Message----- > > From: Peter Eisengrein [mailto:[EMAIL PROTECTED]] > > Sent: Wednesday, January 16, 2002 10:33 AM > > To: Perl-Win32-Users Mailing List (E-mail) > > Subject: hash of arrays > > > > > > I know this has been asked and answered so please forgive me, > > but I left my > > camel at home and can't seem to find it with perldoc... > > > > I have constructed a hash of arrays in %features but can't > > get the data > > back. I'm trying: > > > > # > > foreach my $dn (keys %features) > > { > > my @features=$features{$dn}; > > > > print "$dn|"; > > foreach (@features) > > { > > print "$_\|"; > > } > > print "\n"; > > } > > # > > > > but I get something like: > > > > 8204932|ARRAY(0x1b9efb8)| > > > > > > How do I dereference it? > > > > ___ > > Perl-Win32-Users mailing list > > [EMAIL PROTECTED] > > http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users > > > ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: hash of arrays
That just adds a field to my output, like so: 7943641|ARRAY(0x1b9efb8)|| Perhaps I'm contructing it wrong? I'm doing it like this... @_=$features{$dn}; push(@_,$line[1]); $features{$dn}=\@_; > -Original Message- > From: Hanson, Robert [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, January 16, 2002 10:43 > To: 'Peter Eisengrein'; Perl-Win32-Users Mailing List (E-mail) > Subject: RE: hash of arrays > > > > This is what you want... > > foreach my $dn (keys %features) > { > my @features = @{$features{$dn}}; > > print "$dn|"; > foreach (@features) > { > print "$_\|"; > } > print "\n"; > } > > Rob > > > -Original Message- > From: Peter Eisengrein [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, January 16, 2002 10:33 AM > To: Perl-Win32-Users Mailing List (E-mail) > Subject: hash of arrays > > > I know this has been asked and answered so please forgive me, > but I left my > camel at home and can't seem to find it with perldoc... > > I have constructed a hash of arrays in %features but can't > get the data > back. I'm trying: > > # > foreach my $dn (keys %features) > { > my @features=$features{$dn}; > > print "$dn|"; > foreach (@features) > { > print "$_\|"; > } > print "\n"; > } > # > > but I get something like: > > 8204932|ARRAY(0x1b9efb8)| > > > How do I dereference it? > > ___ > Perl-Win32-Users mailing list > [EMAIL PROTECTED] > http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users > ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users