[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] misc notes




Here's the fruits of our labor...



# ===============================================================

#!/usr/bin/perl

#unless (open INP_MASTER, "</home/q8050_12/perl/master.in"){
unless (open INP_MASTER, "<master.in"){
die "Cannot open the master input file.";
}

unless (open OUT_MASTER, ">master.out") {
die "Cannot open the master output file.";
}

unless (open INP_TRANS, "<trans.in") {
die "Cannot open the transaction input file.";
}

# Here's all the lenghts of all of our fields in master.in:
my @master_field_lengths = (
   10, 25, 15, 25, 15, 15, 15, 15, 15, 15, 15, 15, 100, 15, 5, 15
);

# ------------------------------------
# Step 1: Read master.in into memory.
# ------------------------------------
my %master;   # <-- we're going to slurp the entire file into this hash
while (<INP_MASTER>) {
   my $line = $_;
   chomp $line;
   my @columns;     # <-- each line of data will go in here temporarily
   foreach my $length (@master_field_lengths) {
      my $column = substr $line, 0, $length, undef;
      $column =~ s/\s+$//;
      push @columns, $column;
   }
   # Debug info:
   #print "I just read this into memory:\n";
   #print join "|", @columns;
   #print "\n";

   # Bug: Check for dupes in master.in before memorizing this!
   # Remember this line of data for later.
   $master{$columns[0]} = [ @columns ];
}

# ------------------------------------
# Step 2: Read trans.in and do stuff
# ------------------------------------
my %counts;
while (<INP_TRANS>) {
   my $line = $_;
   chomp $line;
   $line =~ s/[^ -~]//g;  # Remove non-printable characters!
   print "[$line]\n";
   my $code = substr $line, 0, 2;
   my $key  = substr $line, 2, 10;
   $key =~ s/\s+$//;
   my $data = substr $line, 12;
   if ($code eq "00") {
      print "   Deleting $key.\n";
      delete $master{$key};
   } elsif ($code eq "01") {
      if (defined $master{$key}) {
         print "   ERROR! You attempted to add key '$key', ";
         print "but that key already exists.\n";
      } else {
         print "   Adding $key.\n";
         $master{$key} = [$key];
      }
   } elsif ($code eq "AA") {
print " Changing location for $key from '$master{$key}[1]' to '$data'.\n";
      $master{$key}[1] = $data;
   } elsif ($code eq "AB") {
print " Changing model number for $key from '$master{$key}[2]' to '$data'.\n";
      $master{$key}[2] = $data;
   } elsif ($code eq "AC") {
print " Changing the memory configuration for $key from '$master{$key}[3]' to '$data'.\n";
      $master{$key}[3] = $data
   }


   $counts{$code} = $counts{$code} + 1;
}

print "\nHere are your counts:\n";
foreach $x (sort keys %counts) {
   print "$x: $counts{$x}\n";
}

# ------------------------------------
# Step 3: Dump %master out to master.out
# ------------------------------------
my $format;
foreach my $length (@master_field_lengths) {
   $format = $format . '%-' . $length . 's';
}

foreach $key (sort keys %master) {
   my $dataref = $master{$key};
   my @data = @$dataref;
   printf OUT_MASTER $format, @data;
   print OUT_MASTER "\n";
}


close INP_MASTER;
close OUT_MASTER;
close INP_TRANS;



# ===============================================================



[jhannah-mac:~/Desktop/tmp] jhannah% more j4.pl

my $one = 0;
my $two = 0;
my $three = 0;

for (1..10) {
   my $num = int(rand(3)) + 1;
   print "$num\n";

   if ($num == 1) {
      $one = $one + 1;
   } elsif ($num == 2) {
      $two = $two + 1;
   } elsif ($num == 3) {
      $three = $three + 1;
   }
}

print "Ones: $one\n";
print "Twos: $two\n";
print "Threes: $three\n";



[jhannah-mac:~/Desktop/tmp] jhannah% more j5.pl

my %counts;

for (1..1000) {
   my $num = int(rand(9)) + 1;
   print "$num ";

   $counts{$num} = $counts{$num} + 1;
}

print "\nHere are your counts:\n";
foreach $key (sort keys %counts) {
   print "$key: $counts{$key}\n";
}


[jhannah-mac:~/Desktop/tmp] jhannah% more j6.pl

my @master_field_lengths = (

   10, 25, 15, 25, 15, 15, 15, 15, 15, 15, 15, 15, 100, 15, 5, 15

);


my $format;
foreach my $length (@master_field_lengths) {
   $format = "$format $length ";
   print "$format\n";
}

#  %-10s%-25s%-15s...


   $format = $format . " $length ";
   $format .= " $length ";



   $format = $format . '%-' . $length . 's';


[jhannah-mac:~/Desktop/tmp/tmp] jhannah% more j.pl
#!/usr/bin/perl

my %hash = (
  "monthone" => 'Jan',
  "monthtwo" => 'Feb',
  3 => 'Mar'
);

print "    $hash{'monthone'}         $hash{3}       \n";
$hash{3} = "March Madness";
print "    $hash{'monthone'}         $hash{3}       \n";


my @array = (
   "Jun", "Jul", "Aug"
);

print "   $array[0]       $array[2]       \n";
$array[2] = "Augusto!";
print "   $array[0]       $array[2]       \n";

print "$array[1]     $hash{'monthtwo'}  \n";
$hash{'Jay'} = "Hannah";
$hash{3} = "Three";
print "$hash{'Jay'}   $hash{3}   $array[2] \n";


[jhannah-mac:~/Desktop/tmp/tmp] jhannah% more j2.pl

my %hash = (
   'chair'   => 'pizza',
   'monitor' => 'screen',
   'TV'      => 'football'
);

my $key;
foreach $key (keys %hash) {
   print "KEY: $key           VALUE: $hash{$key}\n";
}

delete $hash{chair};
if (defined $hash{chair}) {
   print "There is a chair!\n";
} else {
   print "There is NO chair!\n";
}




delete $hash{'TV'};
$hash{'Jay'} = "Motorcycle";


[jhannah-mac:~/Desktop/tmp/tmp] jhannah% more j3.pl


if ($a) {
   # ...do something...
} elsif ($b) {
   # ...do something...
   # ...do something...
   if ($x) {
      print "Blah!\n";
   } else {
      if ($y) {
         print "yargh!\n";
         if ($myval) {
            die "Can't do that!";
         }
      }
   }
} elsif ($c) {
   # ...do something...
} else {
   # ...do something...
}


[jhannah-mac:~/Desktop/tmp/tmp] jhannah% more j4.pl

$var = "Jay";


print "blah\n";
print 'blah\n';
print "\n";

print "$var\n";
print '$var\n';
print "\n";
print "  '$var'  \n";



[jhannah-mac:~/Desktop/tmp/tmp] jhannah% more prog.pl
#!/usr/bin/perl

open (IN, "infile.txt");
open (OUT, ">outfile.txt");

while (<IN>) {
   my $line = $_;
   print OUT "I just read this line: $line";
}


--------------


$a FALSE
$b TRUE
$x FALSE
$y TRUE
$myval FALSE

my $string = "Jay Hannah";
$string =~ s/a/A/;

"JAy HAnnAh";

Regular expressions

$string =~ s/   /    /;

\s    a space
\s+   one or more spaces
$     end of string

[abc]   a or b or c
[^abc]  NOT a or b or c
[ -~]   ASCII characters -space- through ~
[^ -~]  NOT ACII -space- through ~


perldoc perl


perldoc perlre

s/a/b/;    Change one a to b
s/a/b/g;   Change ALL as to bs
s/a/b/i;   Change as or As to bs
s/a/b/gi;  Change ALL as or As to bs
s/a/b/ig;  Change ALL as or As to bs