Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract from a list and a file 2

Status
Not open for further replies.

JustKIDn

MIS
May 6, 2002
386
US
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 &quot;\n&quot; # 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 &quot;$fldContents,&quot;;
}

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 &quot;if&quot; 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, &quot;>add-rite.txt&quot;) || die(&quot;Could not read file!&quot;);
open (FILE, &quot;addressnotes.txt&quot;) || die(&quot;Could not read file!&quot;);

@MYLIST=(&quot;PhoneLabel ExcludeFromView PublicAccess OrgTable Principal AssignedTo AreaCodeFromLoc&quot;);

LINE: while(<FILE>){

my($fldName,$fldContents) = split(/\s+/, $_,2);
chomp($fldContents);
if($fldName=~ /PhoneLabel |1:/){
print &quot;\n&quot;
}
for ($i=0; $i < @MYLIST; $i++){
if ($MYLIST[$i] = $fldName){
print &quot;$MYLIST[$i]&quot;;
next LINE;
}

print &quot;$fldContents,&quot;;
}

close (FILE);
close (RITE)
#--------------------------------

Can any body help?
 
It could be you're using the wrong equality symbol in the if inside the for loop.

It reads:
if ($MYLIST[$i] = $fldName){

Did you really mean to say:
if ($MYLIST[$i] eq $fldName){


Perl is happily assigning $fldName to $MYLIST[$i], then evaluating the assignment as true.
 
When I used eq it never equated to true.
I see your point, that $fldName is assigning it's contents to $MYLIST[$i].

But what do I do?

Maybe I'm using $MYLIST[$i] wrong or maybe I'm creating the list wrong?

Thanks for the help!
 
Second problem.

You're initializing @MYFILE thus:
@MYLIST=(&quot;PhoneLabel ExcludeFromView PublicAccess OrgTable Principal AssignedTo AreaCodeFromLoc&quot;);

That will create an array with a single entry. $MYFILE[0] = &quot;PhoneLabel ExcludeFromView PublicAccess OrgTable Principal AssignedTo AreaCodeFromLoc&quot;. $MYFILE[<greater than 0>] will be uninitialized.

You might want to initialize @MYFILE thus:
@MYLIST= (&quot;PhoneLabel&quot;,&quot;ExcludeFromView&quot;,&quot;PublicAccess&quot;,&quot;OrgTable&quot;,&quot;Principal&quot;,&quot;AssignedTo&quot;,&quot;AreaCodeFromLoc&quot;);

or thus:
@MYLIST= qw(PhoneLabel ExcludeFromView PublicAccess OrgTable Principal AssignedTo AreaCodeFromLoc);
 
@MYLIST= qw(PhoneLabel ExcludeFromView PublicAccess OrgTable Principal AssignedTo AreaCodeFromLoc);

That seems to work better but it still doesn't work.
I think it's the for loop.
if the field in the list isn't the next one then it fails.

I used a for loop because I couldn't get it to work with foreach

Any ideas?

BTW what does qw mean?
 
try:

for ($i=0; $MYLIST[$i]; $i++)
{
if ($MYLIST[$i] eq $fldName){
print &quot;$MYLIST[$i]&quot;;
next LINE;
}

qw() stands for quote words.
instead of @array = ('this', 'that', 'theother');
you can do @array = (this that theother);
lets you be just a little more lazy.
 
How complete is your presented code-snippet? I also notice there is a missing close-brace in your code there.



 
yeah sorry about that... GIGO... he seems to be missing one too i just cut and pasted....

i meant;
for ($i=0; $MYLIST[$i]; $i++)
{
if ($MYLIST[$i] eq $fldName)
{
print &quot;$MYLIST[$i]&quot;;
next LINE;
}
}

it doesn't really matter since his code should work (well if he adds that brace that is)
 
Don't you hate it when someone says &quot;Never mind I figured it out!&quot;, and then they don't tell you how they resolved it?

me too!

Never mind I figured it out.

Thanks for the advice! some of it I used in this code and some of it I'll use later.

Here's what it looks like. I used a foreach.
Part of the problem I had was getting the split to work right so the foreach would work.
It sure is helpful having a Perl IDE to work in. It helps you to see what your values are and step through the code line by line.

#--------------------------------------------------------------

open (RITE, &quot;>add-rite.txt&quot;) || die(&quot;Could not read file!&quot;);
open (FILE, &quot;addressnotes.txt&quot;) || die(&quot;Could not read file!&quot;);

@MYLIST= qw(ExcludeFromView $PublicAccess OrgTable Principal AssignedTo AreaCodeFromLoc PhoneLabel_1 PhoneLabel_2 PhoneLabel_3 PhoneLabel_4 PhoneLabel_5 PhoneLabel_6);

LINE: while(<FILE>){
my($fldName,$fldContents) = split(/\:\s+/, $_,2);
# Remove colon and split on space into two fields
chomp($fldContents); # Remove newline
if($fldName=~ /PhoneLabel |1:/){
print &quot;\n&quot;; # new record
}
foreach $MYLIST (@MYLIST){
if ($MYLIST eq $fldName){
next LINE;
}
}
print &quot;$fldContents,&quot;;
}
close (FILE);
close (RITE)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top