Say you had a text file containing product information in the format:
product_code,name,colour,size,price
The following code will read the information into memory in an easily retrievable form
open(FILE,"filename.txt");
chomp(@lines=<FP>);
close (FP);foreach $i (@lines) {
($prod_code, $name, $colour, $size, $price)=split(/,/,$i);
$name{$prod_code}=$name;
$colour{$prod_code}=$colour;
$size{$prod_code}=$size;
$price{$prod_code}=$price;
}
$code="PROD101";
print "Product $code is a $name{$code} in colour $colour{$code}, of size $size{$code} costing $price{$code}<Br>";
I live and eat flat files (preferrably pipe ¦ delimited flat files ¦ never was a csv fan).
I get what you were doing there with tip (KISS). I tend to do my flat files differently to make for easier sorting.
I push the flat file (@new) it over into a hash - like this example from a update to stickymail's address book:
foreach $item (@new) {
($tser, $type, $ifolder, $adrname, $atimeposted, $adrrealname, $adremail, $adrphone, $adrfax, $adrurl, $adrcontact, $adrnote, $adricon) = split(/\¦/,$item);
$t = $atimeposted . "¦" . $ser; #default to date
$t = $ser if ($sortfield eq "serial");
$t = "$adrname¦$atimeposted¦$ser" if ($sortfield eq "from");
$t = "$adrrealname¦$adrname¦$ser" if ($sortfield eq "realname");
$t = "$adremail¦$adrname¦$adrrealname¦$ser" if ($sortfield eq "email");
$t = "$adrurl¦$adrname¦$adrrealname¦$ser" if ($sortfield eq "url");
$t =lc($t);
$HITS{"$t"} = $item;
}
Now to spit them out in sorted fashion is as easy as:
foreach $key (sort keys %HITS) {
$line=$HITS{$key};
($tser,$type,$ifolder,$adrname,$atimeposted...) = split(/\¦/,$line);
}
Sure makes updating easier.
1) Open your Excel or other spreadsheet file with StarOffice and Go to save as.
2) Select "File Type Text/CSV"
3) Unselect the "Automatic file name extension box" and rename the file foo.pipe or whatever instead of foo.csv... why not?
4) Click Save
5) Delete whatever is in the "Field Delimiter" box and just type ¦ (pipe)
6) Delete the "Text Delimiter" quotes and leave that blank (if you want).
7) Click OK.
Now you should have
foo1¦foo2¦foo3
goo¦2¦choo
roo¦7¦koo
...
Another trick I found was that if you open a text file you should save it in StarOffice or Excel format temporarily because if you don't you're not allowed to do things like 'insert column' and stuff like that. Then just follow the above procedure to re-save it as a .pipe file. I don't think that Micro$oft allows you to do this so CHEERS OpenOffice!