Help!
I was trying to extract fields from a file that was an address book.
When I exported the data to file, it came out with a seperate field on each line.
Sample data:
---------------
ExcludeFromView: D
$PublicAccess: 1
OrgTable: E0
Principal:
AssignedTo:
PhoneLabel_1: Office Phone
PhoneLabel_3: Office Fax
PhoneLabel_5: Cell Phone
PhoneLabel_2: Home Phone
PhoneLabel_4: Home Fax
PhoneLabel_6: Pager
OfficeStreetAddress: 12345 this street
OfficeCity: Madison
OfficeState: WI
StreetAddress:
City:
State:
etc...
I thought, that's ok perl can fix this! And it can.
I got it to go through and convert it into a csv type file.
But there are a lot of fields that I don't want/need.
So I wrote a bunch of "if" statments to drop those fields.
This works.
code version 1
----------------
open (RITE, ">add-rite.txt"
|| die("Could not read file!"
;
open (FILE, "addressnotes.txt"
|| die("Could not read file!"
;
while(<FILE>){
my($fldName,$fldContents) = split(/\s+/, $_,2);
chomp($fldContents);
if($fldName=~ /PhoneLabel |1:/){
print "\n" # newline for new record
}
if($fldName=~ /ExcludeFromView/){
next;
}
if($fldName=~ /PublicAccess/){
next;
}
if($fldName=~ /OrgTable/){
next;
}
if($fldName=~ /Principal/){
next;
}
if($fldName=~ /AssignedTo/){
next;
}
if($fldName=~ /PhoneLabel/){
next;
}
if($fldName=~ /AreaCodeFromLoc/){
next;
}
print "$fldContents,";
}
close (FILE);
close (RITE)
#----------------------------------------
But then I thought, if I put all the fields I don't want in a list, then I could get rid of all those "if" statements.
so I rewrote it to cycle through a list. Right now I have it printing the list to screen so I can see what it's doing.
This too, works. But when it reads a field that isn't in the list, it still process it as if it was in the list.
What am I doing wrong? It doesn't seem that far off from right.
Here's code 2 with list;
#--------------------------
open (RITE, ">add-rite.txt"
|| die("Could not read file!"
;
open (FILE, "addressnotes.txt"
|| die("Could not read file!"
;
@MYLIST=("PhoneLabel ExcludeFromView PublicAccess OrgTable Principal AssignedTo AreaCodeFromLoc"
;
LINE: while(<FILE>){
my($fldName,$fldContents) = split(/\s+/, $_,2);
chomp($fldContents);
if($fldName=~ /PhoneLabel |1:/){
print "\n"
}
for ($i=0; $i < @MYLIST; $i++){
if ($MYLIST[$i] = $fldName){
print "$MYLIST[$i]";
next LINE;
}
print "$fldContents,";
}
close (FILE);
close (RITE)
#--------------------------------
Can any body help?
I was trying to extract fields from a file that was an address book.
When I exported the data to file, it came out with a seperate field on each line.
Sample data:
---------------
ExcludeFromView: D
$PublicAccess: 1
OrgTable: E0
Principal:
AssignedTo:
PhoneLabel_1: Office Phone
PhoneLabel_3: Office Fax
PhoneLabel_5: Cell Phone
PhoneLabel_2: Home Phone
PhoneLabel_4: Home Fax
PhoneLabel_6: Pager
OfficeStreetAddress: 12345 this street
OfficeCity: Madison
OfficeState: WI
StreetAddress:
City:
State:
etc...
I thought, that's ok perl can fix this! And it can.
I got it to go through and convert it into a csv type file.
But there are a lot of fields that I don't want/need.
So I wrote a bunch of "if" statments to drop those fields.
This works.
code version 1
----------------
open (RITE, ">add-rite.txt"
open (FILE, "addressnotes.txt"
while(<FILE>){
my($fldName,$fldContents) = split(/\s+/, $_,2);
chomp($fldContents);
if($fldName=~ /PhoneLabel |1:/){
print "\n" # newline for new record
}
if($fldName=~ /ExcludeFromView/){
next;
}
if($fldName=~ /PublicAccess/){
next;
}
if($fldName=~ /OrgTable/){
next;
}
if($fldName=~ /Principal/){
next;
}
if($fldName=~ /AssignedTo/){
next;
}
if($fldName=~ /PhoneLabel/){
next;
}
if($fldName=~ /AreaCodeFromLoc/){
next;
}
print "$fldContents,";
}
close (FILE);
close (RITE)
#----------------------------------------
But then I thought, if I put all the fields I don't want in a list, then I could get rid of all those "if" statements.
so I rewrote it to cycle through a list. Right now I have it printing the list to screen so I can see what it's doing.
This too, works. But when it reads a field that isn't in the list, it still process it as if it was in the list.
What am I doing wrong? It doesn't seem that far off from right.
Here's code 2 with list;
#--------------------------
open (RITE, ">add-rite.txt"
open (FILE, "addressnotes.txt"
@MYLIST=("PhoneLabel ExcludeFromView PublicAccess OrgTable Principal AssignedTo AreaCodeFromLoc"
LINE: while(<FILE>){
my($fldName,$fldContents) = split(/\s+/, $_,2);
chomp($fldContents);
if($fldName=~ /PhoneLabel |1:/){
print "\n"
}
for ($i=0; $i < @MYLIST; $i++){
if ($MYLIST[$i] = $fldName){
print "$MYLIST[$i]";
next LINE;
}
print "$fldContents,";
}
close (FILE);
close (RITE)
#--------------------------------
Can any body help?