#!/usr/bin/perl -w # use warnings!!! use it use it use it # basic operations in perl ###### WARM UP ############ # warm up: use strict definition of variables # use my libraries (or your libraries) use strict; use lib '/work/cvogel/scripts_perl/Library_own/'; use lib '/work/cvogel/scripts_perl/Library/'; use lib '/usr/bin/perl/'; use lib '/work/cvogel/alf1/'; use General; # print everything at once $|=1; # tell user how to use the program, and how many arguments to provide die "perl_101.pl \n" unless $ARGV[0]; # rename input variable (@ARGV is argument array) my $file = $ARGV[0]; ####### REAL STUFF ########## # open file and read in every line # take first element in each line as key to a hash, store second element as value of hash, push third element into array # define the hash and array for data storage my %DATA; # hash key = first element, value = second element my @MORE_DATA; # array of third elements ######## go through each line of file open (FILE, $file) or die "Can't open $file\n"; while ( my $line = ) { next if length(chomp $line) == 0; # accept this line as is # this line chops of every nasty end-of-line character of line, and it skips empty (zero length) lines print "\nREADING LINE $line\n"; my @entries_of_line = split("\t", $line); # split line into array print "Number of entries in line\t", scalar(@entries_of_line), "\n"; $DATA{$entries_of_line[0]} = $entries_of_line[1]; # put first and second element of array into the hash %DATA push(@MORE_DATA, $entries_of_line[2]); # add third element of entries_of_line to array print "Done with line\n"; } # while ( my $line = ) { close FILE; ######### 'analyze' your data storage variables and produce some output print "\n\### ANALYSIS ###\n"; # 1. analyze hash --------------------------- print "Analyzing hash DATA\n"; print "Number of keys\t", scalar(keys(%DATA)), "\n"; # how many keys in hash? print "Number of values\t", scalar(values(%DATA)), "\n"; # how many values in hash? # put all keys from hash %DATA into array and print array with delimiter '---' my @keys = keys(%DATA); print "All keys\t", join("---", @keys), "\n"; # print each key and corresponding value from hash print "\tPrinting all HASH entries\n"; foreach my $entry_in_hash ( keys(%DATA) ) { print "key\t$entry_in_hash\tvalue\t", $DATA{$entry_in_hash}, "\n"; } # foreach my $entry_in_hash ( keys(%DATA) ) { # 2. analyze array ------------------- print "\nAnalyzing array MORE_DATA\n"; print "Length (number of entries)\t", scalar(@MORE_DATA), "\n"; # two ways of going through array # a. similar to above, go through each entry: print "Printing all entries -- take ONE\n"; my $count = 0; # have a little counter along with it foreach my $entry ( @MORE_DATA ) { print "entry\t$entry\tcounter\t$count\n"; $count++; # increase count by one } # foreach my $entry ( @MORE_DATA ) { # b. go from first to last element of array: print "\n"; print "Printing all entries -- take TWO\n"; for ( my $position = 0; $position <= $#MORE_DATA; $position++ ) { print "position $position\tentry in MORE_DATA\t$MORE_DATA[$position]\n"; } #