arrays - Building a Perl program to acess a set of data -
i have write perl program able read social security baby names specific year, , have person able enter name, , told weather male or female, how many people born name, , falls in rank.
so far have been able separate data 2 separate arrays based on gender, have no idea go here.
#!/usr/local/bin/perl use strict; use warnings; open (file, "ssbn1898.txt"); print <file>; close (file); @m_array; @f_array; open (my $input, "<", 'ssbn1898.txt'); while ( <$input> ) { chomp; ( $name, $gender ) = split ( /,/ ); if ( $gender = "m" ) { push ( @m_array, $name ); } else { push ( @f_array, $name ); } } close ( $input ); print 'm: ' . join("\t", @m_array) . "\n"; print 'f: ' . join("\t", @f_array) . "\n"; http://www.ssa.gov/cgi-bin/popularnames.cgi
this data working with.
so far have 2 arrays each gender. pending tasks are
- input: allow users input name
- output: male or female
- output: number of people name
to take input user do
my $userinput = <stdin>; then might need chomp $userinput , check whether $userinput in array 1 or array 2. have use grep or loops. doing you'd able find whether name belongs array male names or females.
if (grep /$userinput/, @male_names) { print "found $userinput in male list\n"; } using loop below find total number of people name:
foreach (@male_names){ $counter++ if $userinput eq $_; } ps: grep returns list, if use scalar find number of matches, don't have go loops.
#!/usr/bin/perl use strict; use warnings; @male_names = qw(raj rohan john jim tony raj rohan jim jim); @female_names = qw(natasha neha neha jasmine rita rosy); $matches; $userinput = <stdin>; chomp ($userinput); if ( $matches = grep /$userinput/, @male_names ) { print "found $userinput in male list, count $matches \n"; } elsif ( $matches = grep /$userinput/, @female_names ) { print "found $userinput in female list, count $matches \n"; } else{ print "did not find name"; }
Comments
Post a Comment