Array problem, I think
Y'all, I'm new to programming in perl, and relatively new to programming at all, so I apologize if this is a little hard to follow. I've got oodles of data. It looks like this: 0 @F1@ FAM 1 HUSB @I13@ 1 WIFE @I14@ 1 CHIL @I8@ 0 @F2@ FAM 1 HUSB @I10@ 1 WIFE @I8@ 1 CHIL @I11@ 1 CHIL @I12@ etc. The first numeric portion (either 0 or 1) is irrelevant, the second portion either a family number (@F1@, @F2@, etc.) OR relationship (HUSB, WIFE, or CHIL), and the third portion is an ID number (@I13@, @I14@, @I8@, etc.). What I need to do is create a series of unnamed arrays and reference them hash elements. In other words, I need to do the equivalent to this: $individuals{"F1"}[0] = "I13"; $individuals{"F1"}[1] = "I14"; $individuals{"F1")[2] = "I8"; $individuals{"F2")[0] = "I10"; $individuals{"F2")[1] = "I8"; $individuals{"F2")[2] = "I11"; $individuals{"F2")[3] = "I12"; I'm familiar with substr, split, m//, s///, etc.; I'm just not sure how to programmatically create the data structure. Any help or suggestions to point me in a direction would be appreciated. Thank you. Regards, Doug Cacialli --- Douglas Cacialli - Data Manager / Data Analyst Sleep and Depression Research Laboratory University of Rochester Medical Center 300 Crittenden Blvd. - Box PSYCH Rochester, New York 14642 Phone: (585)273-3309 Fax: (585)506-0287 NOTE NEW FAX NUMBER --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Array problem, I think
One additional thing: The data exists in an array, where each line of raw data is a scalar string within the array. -Original Message- From: Cacialli, Doug Sent: Monday, November 11, 2002 6:18 PM To: '[EMAIL PROTECTED]' Subject: Array problem, I think Y'all, I'm new to programming in perl, and relatively new to programming at all, so I apologize if this is a little hard to follow. I've got oodles of data. It looks like this: 0 @F1@ FAM 1 HUSB @I13@ 1 WIFE @I14@ 1 CHIL @I8@ 0 @F2@ FAM 1 HUSB @I10@ 1 WIFE @I8@ 1 CHIL @I11@ 1 CHIL @I12@ etc. The first numeric portion (either 0 or 1) is irrelevant, the second portion either a family number (@F1@, @F2@, etc.) OR relationship (HUSB, WIFE, or CHIL), and the third portion is an ID number (@I13@, @I14@, @I8@, etc.). What I need to do is create a series of unnamed arrays and reference them hash elements. In other words, I need to do the equivalent to this: $individuals{"F1"}[0] = "I13"; $individuals{"F1"}[1] = "I14"; $individuals{"F1")[2] = "I8"; $individuals{"F2")[0] = "I10"; $individuals{"F2")[1] = "I8"; $individuals{"F2")[2] = "I11"; $individuals{"F2")[3] = "I12"; I'm familiar with substr, split, m//, s///, etc.; I'm just not sure how to programmatically create the data structure. Any help or suggestions to point me in a direction would be appreciated. Thank you. Regards, Doug Cacialli --- Douglas Cacialli - Data Manager / Data Analyst Sleep and Depression Research Laboratory University of Rochester Medical Center 300 Crittenden Blvd. - Box PSYCH Rochester, New York 14642 Phone: (585)273-3309 Fax: (585)506-0287 NOTE NEW FAX NUMBER --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Array problem, I think
Hi, Doug, :) On Mon, 11 Nov 2002, Cacialli, Doug wrote: > I'm new to programming in perl, and relatively new to programming at > all, so I apologize if this is a little hard to follow. Wasn't hard at all! You described the problem very succinctly. > I've got oodles of data. It looks like this: > 0 @F1@ FAM > 1 HUSB @I13@ > 1 WIFE @I14@ > 1 CHIL @I8@ > 0 @F2@ FAM > 1 HUSB @I10@ > 1 WIFE @I8@ > 1 CHIL @I11@ > 1 CHIL @I12@ > The first numeric portion (either 0 or 1) is irrelevant, the second > portion either a family number (@F1@, @F2@, etc.) OR relationship > (HUSB, WIFE, or CHIL), and the third portion is an ID number (@I13@, > @I14@, @I8@, etc.). > What I need to do is create a series of unnamed arrays and reference > them hash elements. In other words, I need to do the equivalent to > this: > $individuals{"F1"}[0] = "I13"; > $individuals{"F1"}[1] = "I14"; > $individuals{"F1")[2] = "I8"; > $individuals{"F2")[0] = "I10"; > $individuals{"F2")[1] = "I8"; > $individuals{"F2")[2] = "I11"; > $individuals{"F2")[3] = "I12"; Is this what you *really* want to do? I notice that you lose the "familial relationship" when you simply push all the members of a family onto an array. Don't you care about which of the [0], [1], or [2] is the HUSB, WIFE, or CHIL? ;) You could easily keep that data as well, if you wanted to. If we do solve it the way you indicate, then you asking to treat the value for the hash key "F1" (family) as an array reference. Then you simply push() all of the family IDs onto the array as you find them. Here's what I came up with to do this: #!/usr/bin/perl use strict; use warnings; my $data = '0 @F1@ FAM 1 HUSB @I13@ 1 WIFE @I14@ 1 CHIL @I8@ 0 @F2@ FAM 1 HUSB @I10@ 1 WIFE @I8@ 1 CHIL @I11@ 1 CHIL @I12@ '; my @lines = split /\n/, $data; my %individuals = (); my $curr_fam; foreach( @lines ) { s/@//g; my ($placeholder, $relation, $id) = split " "; # $id eq "FAM" indicates the start of a new family, with the second # field ($relation) holding the family ID. $curr_fam = $relation, next if $id eq "FAM"; # Line must be an individual. Push the ID on to the array for the # current family. push @{ $individuals{$curr_fam} }, $id; } I hope that makes sense. The last line is the only one that's really interesting. I do exactly what I described above - use the *value* in %individuals associated with the key $curr_fam and treat the value as an array reference, hence the "@{" stuff that precedes it. Here's the code to print out the hash: foreach my $fam ( keys %individuals ) { foreach my $ind ( 0 .. $#{ $individuals{$fam} } ) { print "\$individuals{$fam}[$ind] = \"", $individuals{$fam}[$ind], "\"\n"; } } You know, it might make more sense if you organized your structure like this: $individuals{"F1"}{"HUSB"} = "I10"; $individuals{"F1"}{"WIFE"} = "I8"; $individuals{"F1"}{"CHILD"} = ("I11", "I12"); That way you retain the information on the relationships between the various family members. Ooh, ooh, ooh, I get to say it: "I'll leave that as an exercise to the reader." I hope that helps, ---Jason -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Array problem, I think
This solution works well and is clean, if you are curious about some of the *magic* he is performing in his foreach I would suggest reading up on the special variable $_ for those of us experienced in perl it isn't as daunting to just throw a foreach (@array) in the code and know that it is going to work on $_ so that something like s/regex/replace/g; which appears to just hang in space isn't skipped, but is again working on that $_ that mysteriously jumped into the code. I prefer explicitly giving the temp variable a name so that I know (and more importantly the people after me know) what the hell I was doing and to what, but to each his own. http://danconia.org Jason Tiller wrote: Hi, Doug, :) On Mon, 11 Nov 2002, Cacialli, Doug wrote: I'm new to programming in perl, and relatively new to programming at all, so I apologize if this is a little hard to follow. Wasn't hard at all! You described the problem very succinctly. I've got oodles of data. It looks like this: 0 @F1@ FAM 1 HUSB @I13@ 1 WIFE @I14@ 1 CHIL @I8@ 0 @F2@ FAM 1 HUSB @I10@ 1 WIFE @I8@ 1 CHIL @I11@ 1 CHIL @I12@ The first numeric portion (either 0 or 1) is irrelevant, the second portion either a family number (@F1@, @F2@, etc.) OR relationship (HUSB, WIFE, or CHIL), and the third portion is an ID number (@I13@, @I14@, @I8@, etc.). What I need to do is create a series of unnamed arrays and reference them hash elements. In other words, I need to do the equivalent to this: $individuals{"F1"}[0] = "I13"; $individuals{"F1"}[1] = "I14"; $individuals{"F1")[2] = "I8"; $individuals{"F2")[0] = "I10"; $individuals{"F2")[1] = "I8"; $individuals{"F2")[2] = "I11"; $individuals{"F2")[3] = "I12"; Is this what you *really* want to do? I notice that you lose the "familial relationship" when you simply push all the members of a family onto an array. Don't you care about which of the [0], [1], or [2] is the HUSB, WIFE, or CHIL? ;) You could easily keep that data as well, if you wanted to. If we do solve it the way you indicate, then you asking to treat the value for the hash key "F1" (family) as an array reference. Then you simply push() all of the family IDs onto the array as you find them. Here's what I came up with to do this: #!/usr/bin/perl use strict; use warnings; my $data = '0 @F1@ FAM 1 HUSB @I13@ 1 WIFE @I14@ 1 CHIL @I8@ 0 @F2@ FAM 1 HUSB @I10@ 1 WIFE @I8@ 1 CHIL @I11@ 1 CHIL @I12@ '; my @lines = split /\n/, $data; my %individuals = (); my $curr_fam; foreach( @lines ) { s/@//g; my ($placeholder, $relation, $id) = split " "; # $id eq "FAM" indicates the start of a new family, with the second # field ($relation) holding the family ID. $curr_fam = $relation, next if $id eq "FAM"; # Line must be an individual. Push the ID on to the array for the # current family. push @{ $individuals{$curr_fam} }, $id; } I hope that makes sense. The last line is the only one that's really interesting. I do exactly what I described above - use the *value* in %individuals associated with the key $curr_fam and treat the value as an array reference, hence the "@{" stuff that precedes it. Here's the code to print out the hash: foreach my $fam ( keys %individuals ) { foreach my $ind ( 0 .. $#{ $individuals{$fam} } ) { print "\$individuals{$fam}[$ind] = \"", $individuals{$fam}[$ind], "\"\n"; } } You know, it might make more sense if you organized your structure like this: $individuals{"F1"}{"HUSB"} = "I10"; $individuals{"F1"}{"WIFE"} = "I8"; $individuals{"F1"}{"CHILD"} = ("I11", "I12"); That way you retain the information on the relationships between the various family members. Ooh, ooh, ooh, I get to say it: "I'll leave that as an exercise to the reader." I hope that helps, ---Jason -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Array problem, I think
Hi, Wiggins, :) On Mon, 11 Nov 2002, Wiggins d'Anconia wrote: > This solution works well and is clean, if you are curious about some > of the *magic* he is performing in his foreach I would suggest > reading up on the special variable $_ for those of us experienced in > perl it isn't as daunting to just throw a foreach (@array) in the > code and know that it is going to work on $_ so that something like > s/regex/replace/g; which appears to just hang in space isn't > skipped, but is again working on that $_ that mysteriously jumped > into the code. I prefer explicitly giving the temp variable a name > so that I know (and more importantly the people after me know) what > the hell I was doing and to what, but to each his own. Good call, dude. Sorry about that, Doug! I'm normally pretty anal about using descriptive variable names and obvious grammar/punctuation ('m_' anyone?). Wiggins is right - one of the frustrating things about learning Perl is that there are so many shortcuts and seemingly non-intuitive things (like, what does '$!' mean?) that reading the code of an expert can be daunting, and I don't by *any* *means* consider myself an expert. My background and training has always been geared towards programs that are expressive and immediately understandable as opposed to elegant but terse. Then again, there are some Perl idioms that are just too useful to leave on the cutting room floor. While these can be easily abused, I think they have their place, too. When used judiciously, they can actually *clear* *out* some of the line-noise and extraneous text that sometimes inhibits understanding in other languages. Fine line, though. ---Jason -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Array problem, I think
No need to apologize. Agreed. http://danconia.org Jason Tiller wrote: Hi, Wiggins, :) On Mon, 11 Nov 2002, Wiggins d'Anconia wrote: This solution works well and is clean, if you are curious about some of the *magic* he is performing in his foreach I would suggest reading up on the special variable $_ for those of us experienced in perl it isn't as daunting to just throw a foreach (@array) in the code and know that it is going to work on $_ so that something like s/regex/replace/g; which appears to just hang in space isn't skipped, but is again working on that $_ that mysteriously jumped into the code. I prefer explicitly giving the temp variable a name so that I know (and more importantly the people after me know) what the hell I was doing and to what, but to each his own. Good call, dude. Sorry about that, Doug! I'm normally pretty anal about using descriptive variable names and obvious grammar/punctuation ('m_' anyone?). Wiggins is right - one of the frustrating things about learning Perl is that there are so many shortcuts and seemingly non-intuitive things (like, what does '$!' mean?) that reading the code of an expert can be daunting, and I don't by *any* *means* consider myself an expert. My background and training has always been geared towards programs that are expressive and immediately understandable as opposed to elegant but terse. Then again, there are some Perl idioms that are just too useful to leave on the cutting room floor. While these can be easily abused, I think they have their place, too. When used judiciously, they can actually *clear* *out* some of the line-noise and extraneous text that sometimes inhibits understanding in other languages. Fine line, though. ---Jason -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Array problem, I think
> 0 @F1@ FAM > 1 HUSB @I13@ > 1 WIFE @I14@ > 1 CHIL @I8@ > 0 @F2@ FAM > 1 HUSB @I10@ > 1 WIFE @I8@ > 1 CHIL @I11@ > 1 CHIL @I12@ > etc. $/ = undef; for (split /\n0/, <>) { ($key) = /\@(..)/; $hash{$key} = [ /\@(\w+)\@$/gm ]; } > $individuals{"F1"}[0] = "I13"; > $individuals{"F1"}[1] = "I14"; > $individuals{"F1")[2] = "I8"; > $individuals{"F2")[0] = "I10"; > $individuals{"F2")[1] = "I8"; > $individuals{"F2")[2] = "I11"; > $individuals{"F2")[3] = "I12"; untested. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Array problem, I think
On Mon, Nov 11, 2002 at 06:17:58PM -0500, Cacialli, Doug wrote: > I've got oodles of data. It looks like this: > > 0 @F1@ FAM > 1 HUSB @I13@ > 1 WIFE @I14@ > 1 CHIL @I8@ > 0 @F2@ FAM > 1 HUSB @I10@ > 1 WIFE @I8@ > 1 CHIL @I11@ > 1 CHIL @I12@ > etc. [ snip problem ] > I'm familiar with substr, split, m//, s///, etc.; I'm just not sure how to > programmatically create the data structure. Any help or suggestions to > point me in a direction would be appreciated. Thank you. I'd suggest heading off to CPAN and picking up Gedcom.pm. http://search.cpan.org/author/PJCJ/Gedcom-1.11/ But then, I'm biased ;-) -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]