Advertisement
Guest User

Slightly updated menu

a guest
Mar 21st, 2017
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 27.52 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6.  
  7. my $startYr = 1998; # custom input
  8. my $endYr = 2015;    # from menu
  9.  
  10. my @cfhs = openCrimeFiles($startYr, $endYr);    #open criminal data files from 1998-2000
  11. my @geos = ("","Canada","Newfoundland and Labrador","St. John's Newfoundland and Labrador","Prince Edward Island","Nova Scotia","Halifax Nova Scotia","New Brunswick","Saint John New Brunswick","Quebec","Saguenay Quebec","Québec Quebec","Sherbrooke Quebec","Trois-Rivières Quebec","Montréal Quebec","Ottawa-Gatineau Quebec part","Ontario","Ottawa-Gatineau Ontario/Quebec","Ottawa-Gatineau Ontario part","Toronto Ontario","Hamilton Ontario","St.Catharines-Niagara Ontario","Kitchener-Cambridge-Waterloo Ontario","London Ontario","Windsor Ontario","Sudbury Ontario","Thunder Bay Ontario","Kingston Ontario","Manitoba","Winnipeg Manitoba","Saskatchewan","Regina Saskatchewan","Saskatoon Saskatchewan","Alberta","Calgary Alberta","Edmonton Alberta","British Columbia","Vancouver British Columbia","Victoria British Columbia","Abbotsford-Mission British Columbia","Yukon","Northwest Territories","Nunavut","Moncton New Brunswick","Peterborough Ontario","Brantford Ontario","Guelph Ontario","Barrie Ontario","Kelowna British Columbia");
  12. my @vios = ("","Total all violations","Total all Criminal Code violations (including traffic)","Total all Criminal Code violations (excluding traffic)","Total violent Criminal Code violations","Homicide","Murder first degree","Murder second degree","Manslaughter","Infanticide","Total other violations causing death","Criminal negligence causing death","Other related violations causing death","Attempted murder","Sexual assault level 3 aggravated","Sexual assault level 2 weapon or bodily harm","Sexual assault level 1","Total sexual violations against children","Sexual interference","Invitation to sexual touching","Sexual exploitation","Luring a child via a computer","Assault level 3 aggravated","Assault level 2 weapon or bodily harm","Assault level 1","","Total other assaults","Unlawfully causing bodily harm","Criminal negligence causing bodily harm","Other assaults","Total firearms use of discharge pointing","Discharge firearm with intent","Using firearm in commission of offence","Pointing a firearm","Total robbery","Robbery","Robbery to steal a firearm","Forcible confinement or kidnapping","Total abduction","Abduction under the age 14 not parent or guardian","Abduction under the age 16","Removal of children from Canada","Abduction under the age 14 contravening a custody order","Abduction under age 14 by parent or guardian","Extortion","Criminal harassment","Uttering threats","Indecent/Harassing communications","Total other violent violations","Conspire to commit murder","Other sexual violations","Incest","Corrupting morals of a child","Anal intercourse","Bestiality commit or compel person","Voyeurism","Trap likely to or causing bodily harm","Hostage-taking","Trafficking in persons","Intimidation of a justice system participant or a journalist","Intimidation of a non-justice participant","Explosives causing death or bodily harm","Arson disregard for human life","Other violent violations","Total property crime violations","Total breaking and entering","Breaking and entering","Breaking and entering to steal a firearm","Total possession of stolen property","Total theft of motor vehicle","","","Total theft over \$5000 (non-motor vehicle)","Theft over \$5000","Theft over \$5000 from a motor vehicle","Shoplifting over \$5000","Total theft under \$5000 (non-motor vehicle)","Theft \$5000 or under","Theft \$5000 or under from a motor vehicle","Shoplifting \$5000 or under","Fraud","Total mischief","Mischief","Mischief to religious property motivated by hate","Arson","Counterfeiting","Total weapons violations","Offensive weapons explosives","Offensive weapons prohibited","Offensive weapons restricted","Firearm transfers or serial numbers","Other offensive weapons","Weapons trafficking","Weapons possession contrary to order","Possession of weapons","Unauthorized importing or exporting of weapons","Firearms documentation or administration","Unsafe storage of firearms","Child pornography","Total prostitution","Bawdy house","Living off the avails of prostitution of a person under age 18","Procuring","Obtains or communicates with a person under age 18 for purpose of sex","Other prostitution","Disturb the peace","Total administration of justice violations","Fail to comply with order","Escape or helps to escape from lawful custody","Prisoner unlawfully at large","Fail to appear","Breach of probation","Other violations against the administration of law and justice (Part IV Criminal Code)","Total other violations","Betting house","Gaming house","Other violations related to gaming and betting","Indecent acts","Corrupting morals","Obstruct public or peace officer","Trespass at night","Utter threats to property or animal","Advocating genocide","Public incitement of hatred","Unauthorized recording of a movie","Offences against public order (Part II Criminal Code)","Property or service for terrorist activity","Freezing of property disclosure or audit","Participate in activity of terrorist group","Facilitate terrorist activity","Commission or instructing to carry out terrorist activity","Harbour or conceal terrorist","Hoax terrorism","Firearms and other offensive weapons (Part III Criminal Code)","Sexual offences public morals and disorderly conduct (Part V Criminal Code)","Invasion of privacy (Part VI Criminal Code)","Disorderly houses gaming and betting","Offences against the person and reputation (Part VIII Criminal Code)","Offences against rights of property (Part IX Criminal Code)","Fraudulent transactions relating to contracts and trade (Part X Criminal Code)","Willful and forbidden acts in respect of certain property (Part XI Criminal Code)","Offences relating to currency (Part XII Criminal Code)","Proceeds of crime (Part XII.2 Criminal Code)","Attempts conspiracies accessories (Part XIII Criminal Code)","Instruct offence for criminal organization","Commit offence for criminal organization","Participate in activities of criminal organization","All other Criminal Code (includes Part XII.1 Criminal Code)","Total Criminal Code traffic violations","Total impaired driving","Impaired operation causing death","Impaired operation (drugs) causing death","Impaired operation causing bodily harm","Impaired operation (drugs) causing bodily harm","Impaired operation of motor vehicle vessel or aircraft","Impaired operation (drugs) vehicle vessel aircraft","Impaired operation failure to provide breath sample","Failure to comply or refusal (drugs)","Impaired operation failure to provide blood sample","Failure to provide blood sample (drugs)","Total other Criminal Code traffic violations","Dangerous operation causing death","Dangerous operation causing bodily harm","Dangerous operation of motor vehicle vessel or aircraft","Dangerous operation evading police causing death","Dangerous operation evading police causing bodily harm","Dangerous operation of motor vehicle evading police","Total fail to stop or remain","Driving while prohibited","Other Criminal Code traffic violations","Causing death by criminal negligence while street racing","Causing bodily harm by criminal negligence while street racing","Dangerous operation causing death while street racing","Dangerous operation causing bodily harm while street racing","Dangerous operation of motor vehicle while street racing","Total Federal Statute violations","Possession cannabis","Possession cocaine","Total possession other Controlled Drugs and Substances Act drugs","Heroin possession","Other Controlled Drugs and Substances Act possession","Methamphetamines (crystal meth) possession","Methylenedioxyamphetamine (ecstasy) possession","Total cannabis trafficking production or distribution","Cannabis trafficking","Cannabis importation and exportation","Cannabis production","Total cocaine trafficking production or distribution","Cocaine trafficking","Cocaine importation and exportation","Cocaine production","Total other Controlled Drugs and Substances Act drugs trafficking production or distribution","Heroin trafficking","Other Controlled Drugs and Substances Act trafficking","Methamphetamines (crystal meth) trafficking","Methylenedioxyamphetamine (ecstasy) trafficking","Heroin importation and exportation","Other Controlled Drugs and Substances Act importation and exportation","Methamphetamines (crystal meth) importation and exportation","Methylenedioxyamphetamine (ecstasy) importation and exportation","Heroin production","Other Controlled Drugs and Substances Act production","Methamphetamines (crystal meth) production","Methylenedioxyamphetamine (ecstasy) production","Youth Criminal Justice Act","Total other Federal Statutes","Bankruptcy Act","Income Tax Act","Canada Shipping Act","Canada Health Act","Customs Act","Competition Act","Excise Act","Total Immigration and Refugee Protection Act","Firearms Act","National Defence Act","Other federal statutes","","","Kidnapping","Forcible confinement","Sexual Exploitation of a person with a disability","Break and enter to steal a firearm from a motor vehicle","Identity theft","Identity fraud","Total other Criminal Code violations","Total assaults against a peace officer","Total forcible confinement or kidnapping","Total trafficking in stolen property","Altering removing or destroying Vehicle Identification Number (VIN)","Precursor or equipment (crystal meth ecstasy)","Total drug violations","Making sexually explicit material available to children","Mischief in relation to cultural property","Mischief relating to war memorials","Communicating to provide sexual services for consideration","Stopping or impeding traffic for the purpose of offering providing or obtaining sexual services for consideration","Obtaining sexual services for consideration","Obtaining sexual services for consideration from person under 18 years","Receive material benefit from sexual services","Receive material benefit from sexual services provided by a person under 18 years","Procuring","Procuring - person under 18 years","Advertising sexual services","Parent or guardian procuring sexual activity: person procured under the age of 16 years","Parent or guardian procuring sexual activity","Householder permitting illegal sexual activity: person under the age of 16 years","Householder permitting prohibited sexual activity","Leaving Canada to participate in activity of terrorist group","Leaving Canada to facilitate terrorist activity","Leaving Canada to commit offence for terrorist group","Leaving Canada to commit offence that is terrorist activity","Concealing person who carried out terrorist activity that is a terrorism offence for which that person is liable to imprisonment for life","Concealing person who carried out terrorist activity that is a terrorism offence for which that person is liable to any punishment other than life","Concealing person who is likely to carry out terrorist activity","Recruitment of members by a criminal organization","Total commodification of sexual activity violations","Nonconsensual distribution of intimate images","Making or distribution of child pornography","Common Bawdy House","Advocating/Promoting terrorism offence");
  13. my @stas = ("","Actual incidents","Rate per 100000 population","Percentage change in rate","Total cleared","Cleared by charge","Cleared otherwise","Total persons charged","Rate total persons charged per 100000 population aged 12 years and over","Total adult charged","Rate adult charged per 100000 population aged 18 years and over","Total youth charged","Rate youth charged per 100000 population aged 12 to 17 years","Total youth not charged","Rate youth not charged per 100000 population aged 12 to 17 years");
  14.  
  15. my $question_type = "";
  16. my $question_choice = "";
  17. my $var_x = "";
  18. my $var_y = "";
  19. my $var_z = "";
  20. my $geo_code = "";
  21. my $geo_code_2 = "";
  22. my $start_year = "";
  23. my $end_year = "";
  24.  
  25. if(@cfhs) {
  26.     while ($question_type ne '0') {
  27.         $question_type = "";
  28.         print "-------------------\n-----MAIN MENU-----\n-------------------\n";
  29.         print "Enter 1 for Correlation questions,\nEnter 2 for String/Table questions,\nEnter 3 for Numberical questions,\nEnter 4 for percent change/rate questions,\nEnter 0 to quit.\n";
  30.         print "What type of question do you want to ask? ";
  31.         $question_type = <STDIN>;
  32.         cls();
  33.         chomp $question_type;
  34.        
  35.         if($question_type eq '1') {
  36.             print "1. Is there a positive or negative correlation between x and y?\n";
  37.             print "Which question would you like to choose? ";
  38.             $question_choice = <STDIN>;
  39.             chomp $question_choice;
  40.             cls();
  41.            
  42.             if($question_choice eq '1') {
  43.                 print "Enter x (violation): ";
  44.                 $var_x = <STDIN>;
  45.                 chomp $var_x;
  46.                 print "Enter y (violation): ";
  47.                 $var_y = <STDIN>;
  48.                 chomp $var_y;
  49.                 print "Enter geo code: ";
  50.                 $geo_code = <STDIN>;
  51.                 chomp $geo_code;
  52.                 print "Enter start year: ";
  53.                 $start_year = <STDIN>;
  54.                 chomp $start_year;
  55.                 print "Enter end year: ";
  56.                 $end_year = <STDIN>;
  57.                 chomp $end_year;
  58.                
  59.                 print correlation1($geo_code,$var_x,$var_y,$start_year,$end_year);
  60.             }
  61.         } elsif($question_type eq '2') {
  62.             print "You entered $question_type\n";
  63.             print "1. Is x more popular than y in location z?\n";
  64.             print "2. List the xth most popular violations in location z.\n";
  65.             print "3. List the xth least popular violation in location z.\n";
  66.             print "4. When compared, is area x or area y safer in regards to crime z?\n";
  67.            
  68.             print "Which question would you like to choose? ";
  69.             $question_choice = <STDIN>;
  70.             chomp $question_choice;
  71.             cls();
  72.            
  73.             if($question_choice eq '1') {
  74.                 print "Enter x (violation): ";
  75.                 $var_x = <STDIN>;
  76.                 chomp $var_x;
  77.                 print "Enter y (violation): ";
  78.                 $var_y = <STDIN>;
  79.                 chomp $var_y;
  80.                 print "Enter z (statistics code): ";
  81.                 $var_z = <STDIN>;
  82.                 chomp $var_z;
  83.                 print "Enter geo code: ";
  84.                 $geo_code = <STDIN>;
  85.                 chomp $geo_code;
  86.                 print "Enter start year: ";
  87.                 $start_year = <STDIN>;
  88.                 chomp $start_year;
  89.                 print "Enter end year: ";
  90.                 $end_year = <STDIN>;
  91.                 chomp $end_year;
  92.                
  93.                 print strTab1($geo_code, $var_x, $var_y, $var_z, $start_year, $end_year);
  94.             }
  95.             if($question_choice eq '2') {
  96.                 print "Enter x: ";
  97.                 $var_x = <STDIN>;
  98.                 chomp $var_x;
  99.                 print "Enter geo code: ";
  100.                 $geo_code = <STDIN>;
  101.                 chomp $geo_code;
  102.                 print "Enter start year: ";
  103.                 $start_year = <STDIN>;
  104.                 chomp $start_year;
  105.                 print "Enter end year: ";
  106.                 $end_year = <STDIN>;
  107.                 chomp $end_year;
  108.                
  109.                 print strTab2($geo_code, $var_x, $start_year, $end_year);
  110.             }
  111.             if($question_choice eq '3') {
  112.                 print "Enter x: ";
  113.                 $var_x = <STDIN>;
  114.                 chomp $var_x;
  115.                 print "Enter geo code: ";
  116.                 $geo_code = <STDIN>;
  117.                 chomp $geo_code;
  118.                 print "Enter start year: ";
  119.                 $start_year = <STDIN>;
  120.                 chomp $start_year;
  121.                 print "Enter end year: ";
  122.                 $end_year = <STDIN>;
  123.                 chomp $end_year;
  124.                
  125.                 print strTab3($geo_code, $var_x, $start_year, $end_year);
  126.             }
  127.             if($question_choice eq '4') {
  128.                 print "Enter 1st geo code: ";
  129.                 $geo_code = <STDIN>;
  130.                 chomp $geo_code;
  131.                 print "Enter 2nd geo code: ";
  132.                 $geo_code_2 = <STDIN>;
  133.                 chomp $geo_code_2;
  134.                 print "Enter x (violation): ";
  135.                 $var_x = <STDIN>;
  136.                 chomp $var_x;
  137.                 print "Enter start year: ";
  138.                 $start_year = <STDIN>;
  139.                 chomp $start_year;
  140.                 print "Enter end year: ";
  141.                 $end_year = <STDIN>;
  142.                 chomp $end_year;
  143.                
  144.                 print strTab4($geo_code, $geo_code_2, $var_x, $start_year, $end_year);
  145.             }
  146.         } elsif($question_type eq '3') {
  147.             print "1. How many actual incidents of x were there?\n";
  148.             print "2. What is a city's contribution to that province's crime total?\n";
  149.             print "3. Does province x have more/less crime A than province y?\n";
  150.            
  151.             print "Which question would you like to choose? ";
  152.             $question_choice = <STDIN>;
  153.             chomp $question_choice;
  154.             cls();
  155.            
  156.             if($question_choice eq '1') {
  157.                 print "Enter x (violation): ";
  158.                 $var_x = <STDIN>;
  159.                 chomp $var_x;
  160.                 print "Enter geo code: ";
  161.                 $geo_code = <STDIN>;
  162.                 chomp $geo_code;
  163.                 print "Enter start year: ";
  164.                 $start_year = <STDIN>;
  165.                 chomp $start_year;
  166.                 print "Enter end year: ";
  167.                 $end_year = <STDIN>;
  168.                 chomp $end_year;
  169.             }
  170.             if($question_choice eq '2') {
  171.                 print "Enter x (violation): ";
  172.                 $var_x = <STDIN>;
  173.                 chomp $var_x;
  174.                 print "Enter geo code (of city): ";
  175.                 $geo_code = <STDIN>;
  176.                 chomp $geo_code;
  177.                 print "Enter start year: ";
  178.                 $start_year = <STDIN>;
  179.                 chomp $start_year;
  180.                 print "Enter end year: ";
  181.                 $end_year = <STDIN>;
  182.                 chomp $end_year;
  183.             }
  184.             if($question_choice eq '3') {
  185.                 print "Enter x (violation): ";
  186.                 $var_x = <STDIN>;
  187.                 chomp $var_x;
  188.                 print "Enter geo code (of 1st province): ";
  189.                 $geo_code = <STDIN>;
  190.                 chomp $geo_code;
  191.                 print "Enter geo code (of 2nd province): ";
  192.                 $geo_code_2 = <STDIN>;
  193.                 chomp $geo_code_2;
  194.                 print "Enter start year: ";
  195.                 $start_year = <STDIN>;
  196.                 chomp $start_year;
  197.                 print "Enter end year: ";
  198.                 $end_year = <STDIN>;
  199.                 chomp $end_year;
  200.             }
  201.         } elsif($question_type eq '4') {
  202.             print "1. What is the change in percent in x violation from year y to year z?\n";
  203.             print "2. What was the rate of crime x in location y?\n";
  204.            
  205.             print "Which question would you like to choose? ";
  206.             $question_choice = <STDIN>;
  207.             chomp $question_choice;
  208.             cls();
  209.            
  210.             if($question_choice eq '1') {
  211.                 print "Enter x (violation): ";
  212.                 $var_x = <STDIN>;
  213.                 print "Enter 1st geo code: ";
  214.                 $geo_code = <STDIN>;
  215.                 print "Enter 2nd geo code: ";
  216.                 $geo_code_2 = <STDIN>;
  217.                 print "Enter start year: ";
  218.                 $start_year = <STDIN>;
  219.                 print "Enter end year: ";
  220.                 $end_year = <STDIN>;
  221.             }
  222.             if($question_choice eq '2') {
  223.                 print "Enter x (violation): ";
  224.                 $var_x = <STDIN>;
  225.                 chomp $var_x;
  226.                 print "Enter geo code: ";
  227.                 $geo_code = <STDIN>;
  228.                 chomp $geo_code;
  229.                 print "Enter start year: ";
  230.                 $start_year = <STDIN>;
  231.                 chomp $start_year;
  232.                 print "Enter end year: ";
  233.                 $end_year = <STDIN>;
  234.                 chomp $end_year;
  235.             }
  236.            
  237.         }
  238.         $question_choice = "";
  239.         $var_x = "";
  240.         $var_y = "";
  241.         $var_z = "";
  242.         $geo_code = "";
  243.         $geo_code_2 = "";
  244.         $start_year = "";
  245.         $end_year = "";
  246.     }
  247. } else {
  248.     print "Invalid range entered.\n";
  249. }
  250.  
  251.  
  252. sub cls { #clear the screen
  253.     print "\033[2J";
  254.     print "\033[0;0H";
  255. }
  256.  
  257. sub openCrimeFiles {
  258.     my ($startYr, $endYr, $stat) = @_;
  259.     my @cfhs;
  260.     if($startYr>=1998 && $endYr<=2015) {    #if years are within 1998-2015 range
  261.         for(my $yr=$startYr; $yr<=$endYr; $yr++) {
  262.             open(my $fh, "<", "criminalData/c".$yr.".csv");
  263.             push(@cfhs, $fh);
  264.         }
  265.     }
  266.     return @cfhs;
  267. }
  268.  
  269. sub printFilesWithinRange { # Example function on how to loop through all files within the range & how to read their values
  270.     my ($startYr, $endYr) = @_;
  271.     my $returnString = "";
  272.  
  273.     for(my $yrIndex=0; $yrIndex<=($endYr-$startYr); $yrIndex++) {
  274.         my $currYear = $cfhs[$yrIndex];
  275.         while (<$currYear>) {
  276.             my @lineVals = split(',', $_);
  277.             $returnString .= "$lineVals[0] | $lineVals[1] | $lineVals[2] | $lineVals[3]"; # prints to $returnString all 4 fields for every line of every file within the inputted range.
  278.         }
  279.         $returnString .= "\n-------------\n";
  280.     }
  281.  
  282.     return $returnString;
  283. }
  284.  
  285. # Is there a negative or positive correlation between x & y?
  286. sub correlation1 {
  287.     my ($geo, $v1, $v2, $startYr, $endYr) = @_;
  288.     my $returnString = "";
  289.    
  290.     for(my $yrIndex=0; $yrIndex<=($endYr-$startYr); $yrIndex++) {
  291.         my $currYear = $cfhs[$yrIndex];
  292.         my $result1 = "";
  293.         my $result2 = "";
  294.  
  295.         while(<$currYear>) {
  296.             my @lineVals = split(',', $_);
  297.  
  298.             if($lineVals[0] eq $geo && $lineVals[1] eq $v1 && $lineVals[2] eq "3" && $lineVals[3] ne "_\n") {
  299.                 $result1 = $lineVals[3];
  300.             }
  301.             if($lineVals[0] eq $geo && $lineVals[1] eq $v2 && $lineVals[2] eq "3" && $lineVals[3] ne "_\n") {
  302.                 $result2 = $lineVals[3];
  303.             }
  304.         }
  305.         if($result1 eq "" || $result2 eq "") {
  306.             $returnString .= "".($yrIndex+$startYr).": Not enough data\n";
  307.         } else {
  308.             $result1 =~ s/\R//g; # Remove \n from strings
  309.             $result1*=1;     # Convert string to number
  310.             $result2 =~ s/\R//g;
  311.             $result2*=1;
  312.  
  313.             $returnString .= "".($yrIndex+$startYr).": ";
  314.  
  315.             if($result1 > $result2) {
  316.                 $returnString .= "Positive relationship. $vios[$v1] increased by more than $vios[$v2] in $geos[$geo].";
  317.             } elsif($result2 > $result1) {
  318.                 $returnString .= "Negative relationship. $vios[$v1] increased by less than $vios[$v2] in $geos[$geo].";
  319.             } else {
  320.                 $returnString .= "Neutral relationship. $vios[$v1] and $vios[$v2] changed by the same amount in $geos[$geo].";
  321.             }
  322.             $returnString .= "\n";
  323.         }
  324.     }
  325.     return $returnString;
  326. }
  327.  
  328. #Is x greater/more popular than y
  329. sub strTab1 {
  330.     my ($geo, $v1, $v2, $sta, $startYr, $endYr) = @_;
  331.     my $returnString = "";
  332.  
  333.     for(my $yrIndex=0; $yrIndex<=($endYr-$startYr); $yrIndex++) {
  334.         my $currYear = $cfhs[$yrIndex];
  335.         my $result1 = "";
  336.         my $result2 = "";
  337.  
  338.         while(<$currYear>) {
  339.             my @lineVals = split(',', $_);
  340.             if($lineVals[0] eq $geo && $lineVals[1] eq $v1 && $lineVals[2] eq $sta && $lineVals[3] ne "_\n") {
  341.                 $result1 = $lineVals[3];
  342.             }
  343.             if($lineVals[0] eq $geo && $lineVals[1] eq $v2 && $lineVals[2] eq $sta && $lineVals[3] ne "_\n") {
  344.                 $result2 = $lineVals[3];
  345.             }
  346.         }
  347.         if($result1 eq "" || $result2 eq "") {
  348.             $returnString .= "".($yrIndex+$startYr).": Not enough data\n";
  349.         } else {
  350.             $result1 =~ s/\R//g; # Remove \n from strings
  351.             $result1*=1;     # Convert string to number
  352.             $result2 =~ s/\R//g;
  353.             $result2*=1;
  354.  
  355.             $returnString .= "".($yrIndex+$startYr).": ";
  356.  
  357.             if($result1 > $result2) {
  358.                 $returnString .= "$vios[$v1] is greater/more popular than $vios[$v2] in $geos[$geo].";
  359.             } elsif($result2 > $result1) {
  360.                 $returnString .= "$vios[$v1] is smaller/less popular than $vios[$v2] in $geos[$geo].";
  361.             } else {
  362.                 $returnString .= "$vios[$v1] and $vios[$v2] are equally popular/large in $geos[$geo].";
  363.             }
  364.             $returnString .= "\n";
  365.         }
  366.     }
  367.     return $returnString;
  368. }
  369.  
  370. #What was the xth most popular crime in location geo
  371. sub strTab2 {
  372.     my ($geo, $xth, $startYr, $endYr) = @_;
  373.     my $returnString = "";
  374.  
  375.     for(my $yrIndex=0; $yrIndex<=($endYr-$startYr); $yrIndex++) {
  376.         my $currYear = $cfhs[$yrIndex];
  377.         my @notSorted;
  378.         my @sorted;
  379.         my $crimesWithData = 0;
  380.  
  381.         while(<$currYear>) {
  382.             my @lineVals = split(',', $_);
  383.  
  384.             if($lineVals[0] eq $geo && $lineVals[2] eq "1" && $lineVals[3] ne "_\n") {
  385.             $crimesWithData++;
  386.             $lineVals[3] =~ s/\R//g;
  387.             $lineVals[1] =~ s/\R//g;
  388.             my @elem = ($lineVals[3],$lineVals[1]); # incidents, #vioCode
  389.             push @notSorted, \@elem;
  390.             }
  391.         }
  392.  
  393.         if($xth<0 || $xth>($crimesWithData-1)) {
  394.             $returnString .= "In the year ".($yrIndex+$startYr)." there was no $xth th most popular crime.\n";
  395.         } else {
  396.             @sorted = sort {$b->[0] <=> $a->[0] } @notSorted;
  397.  
  398.             $returnString .= "In ".($yrIndex+$startYr)." ";
  399.             $returnString .= "".$vios[$sorted[$xth][1]]." was the $xth th most popular crime in $geos[$geo] with $sorted[$xth][0] incidents.\n";
  400.         }
  401.     }
  402.     return $returnString;
  403. }
  404.  
  405. sub strTab3 {
  406.     my ($geo, $xth, $startYr, $endYr) = @_;
  407.     my $returnString = "";
  408.  
  409.     for(my $yrIndex=0; $yrIndex<=($endYr-$startYr); $yrIndex++) {
  410.         my $currYear = $cfhs[$yrIndex];
  411.         my @notSorted;
  412.         my @sorted;
  413.         my $crimesWithData = 0;
  414.  
  415.         while(<$currYear>) {
  416.             my @lineVals = split(',', $_);
  417.  
  418.             if($lineVals[0] eq $geo && $lineVals[2] eq "1" && $lineVals[3] ne "_\n") {
  419.                 $crimesWithData++;
  420.                 $lineVals[3] =~ s/\R//g;
  421.                 $lineVals[1] =~ s/\R//g;
  422.                 my @elem = ($lineVals[3],$lineVals[1]); # incidents, #vioCode
  423.                 push @notSorted, \@elem;
  424.             }
  425.         }
  426.  
  427.         if($xth<0 || $xth>($crimesWithData-1)) {
  428.             $returnString .= "In the year ".($yrIndex+$startYr)." there was no $xth th least popular crime.\n";
  429.         } else {
  430.             @sorted = sort {$b->[0] <=> $a->[0] } @notSorted;
  431.  
  432.             $returnString .= "In ".($yrIndex+$startYr)." ";
  433.             $returnString .= "".$vios[$sorted[($crimesWithData-1)-$xth][1]]." was the $xth th least popular crime in $geos[$geo] with $sorted[($crimesWithData-1)-$xth][0] incidents.\n";
  434.         }
  435.     }
  436.     return $returnString;
  437. }
  438.  
  439. #When compared, is area x or area y safer in regards to crime z?
  440. sub strTab4 {
  441.     my ($geo1, $geo2, $v, $startYr, $endYr) = @_;
  442.     my $returnString = "";
  443.  
  444.     for(my $yrIndex=0; $yrIndex<=($endYr-$startYr); $yrIndex++) {
  445.         my $currYear = $cfhs[$yrIndex];
  446.         my @notSorted;
  447.         my @sorted;
  448.         my $rateGeo1 = "";
  449.         my $rateGeo2 = "";
  450.  
  451.         while(<$currYear>) {
  452.             my @lineVals = split(',', $_);
  453.             $lineVals[3] =~ s/\R//g;
  454.  
  455.             if($lineVals[0] eq $geo1 && $lineVals[1] eq $v && $lineVals[2] eq "2" && $lineVals[3] ne "_\n") {
  456.                 $rateGeo1 = $lineVals[3];
  457.                 $rateGeo1*=1;
  458.             }
  459.             if($lineVals[0] eq $geo2 && $lineVals[1] eq $v && $lineVals[2] eq "2" && $lineVals[3] ne "_\n") {
  460.                 $rateGeo2 = $lineVals[3];
  461.                 $rateGeo2*=1;
  462.             }
  463.         }
  464.  
  465.         if($rateGeo1 eq "" || $rateGeo2 eq "") {
  466.             print "In the year ".($yrIndex+$startYr)." there was not enough data to make a comparison.\n";
  467.         } else {
  468.             if($rateGeo1 > $rateGeo2) {
  469.                 $returnString .= "In the year ".($yrIndex+$startYr)." $vios[$v]'s rate of occurence per 100,000 was $rateGeo1 in $geos[$geo1] and $rateGeo2 in $geos[$geo2]. $geos[$geo2] was safer.\n";
  470.             } elsif($rateGeo1 < $rateGeo2) {
  471.                 $returnString .= "In the year ".($yrIndex+$startYr)." $vios[$v]'s rate of occurence per 100,000 was $rateGeo1 in $geos[$geo1] and $rateGeo2 in $geos[$geo2]. $geos[$geo1] was safer.\n";
  472.             }
  473.         }
  474.     }
  475.     return $returnString;
  476. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement