in reply to More Flat-File Database Questions
#!/usr/local/bin/perl -w
#===========================================================
# Get/Organize Parm & Variables
#===========================================================
if ($ARGV[0]) { $parm = $ARGV[0];}
else { print "Prameter missing.... \n";
die "Program Terminated... \n";
}
chomp($parm);
@parm_list = split(/\|/,$parm);
$a = @parm_list;
if ($a != 2) { die "Number of parameters not satisfied\n";}
else { ($first_item_num,
$last_item_num)= @parm_list;
}
$first_item_num =~ s/^\s+|\s+$//g; #trim
$last_item_num =~ s/^\s+|\s+$//g;
$seq = 0; # create a variable
%cat_key_hash =(); # init hashes
%cat_dtl_hash =();
#=======================================================================================#
# M A I N P R O G R A M
#=======================================================================================#
open_files();
load_hash();
print_list();
close_files();
#=======================================================================================#
# E N D O F P R O G R A M #
#=======================================================================================#
sub open_files{
$datadir = "/dir";
open (INP, "<$datadir/$flat_file")|| die "Couldn't open Flat file, $!\n";
open (OUTRPT, ">$datadir/$file_rpt");
}
#=======================================================================================#
sub load_hash{
while(<INPEDI>) {
chomp;
$inp_str = $_ ;
## Let inp_str =>CAT_NUM DESCRIPTION PRICE
($inp_cat_num,
$inp_desc,
$inp_price) = split(/\|/,$inp_str);
if ($cat_key_hash{$inp_cat_num}) { $seq = $cat_key_hash{$f_cust_po}
$seq ++
($cat_key_hash{$inp_cat_num}) = $seq;
# concatenate sequence number with catalog number
# to make the key to be unique
($cat_dtl_hash{$inp_cat_num.$seq}) = $inp_str;
}
elsif
if ($cat_key_hash{$inp_cat_num}) { $seq = 1;
($cat_key_hash{$inp_cat_num}) = $seq;
($cat_dtl_hash{$inp_cat_num.$seq}) = $inp_str;
}
} #end while
} #end sub
#=======================================================================================#
sub print_list{
# order your key list
@key_list = sort {$a <=> $b} (keys %cat_key_hash);
foreach $key (@klist){
if (( $key >= first_cat_num) and ( $key <= $last_cat_num))
{ $seq = $cat_key_hash{$key}; #find how many records for that catalog#
for (i=1, i <=$seq, i++) { #print all the details
# You may split and format the output
$dtl_key = $key.$i; # create key
print "$cat_dtl_hash{$dtl_key}\n";
} # end for
} #end if
} # end print_list
#=======================================================================================#