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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking vars from flatfile 2

Status
Not open for further replies.

JimJx

Technical User
Feb 16, 2001
202
US
Hi all,

Just had a project dumped in my lap and was wondering if anyone had any ideas on this....

Everything was going great, until I found out I needed to read in the flatfile, and any null fields need to be changed to None or something similar, then rewrite the file....

I really don't want to run through a bunch of if statements checking for a null value and changing it to None. I know there should be a way of doing it using a foreach, but am not sure how to get started on it.

Any ideas, suggestions, comments, etc greatly appreciated.
Jim
 
Hi Jim,
it would be helpful if you had some sample code, or a few lines from your flat file table

Cheers

troy
 
Like this:

Code:
open(FILE,&quot;<path/to.file&quot;);
@content = <FILE>;
close(FILE);

@variable = split(/\|/, $content);

The delimiter is: |

Each record will then be: $variable[NUMBER]

Hope this helps! :) AIM: XCalvin1984
Email: calvin@puremadnezz.com
MSN: xcloud2000x@hotmail.com
Web Site: Yahoo Messenger: xcloud2000x

Contact me for any programming help and whatnot. I'll try my best to help! :)
 
Hi Troy and Calvin,

I read in the data like this:

open (DATA, &quot;< companies.txt&quot;) || die &quot;Can't open companies.txt!\n$!\n&quot;;
@list=<DATA>;
close (DATA);
chomp (@list);

foreach $list(@list) {
($company,$category,$logo,$street1,$street2,$city,$state,$zip,$desc1,$desc2,$desc3,$image1,$image2,$image3,$url,$contact,$length) = split(/\|/,$list);

#Do whatever with the data

}

Most of these variables will contain data, sometimes several lines. Some of the vars will contain nothing. I need to take the vars that are empty, and change them to some default value, such as 'None' for example, but am not quite sure about how to loop through each var and check it without doing a series of 17 'if' statements......

Thanks for the quick replies.
Jim


 
The easiest way is just to do a search-and-replace on the whole record for null fields (i.e two consecutive delimiters, or starting or ending character is a delimiter). This should do that:
Code:
open (DATA, &quot;< companies.txt&quot;) || die &quot;Can't open companies.txt!\n$!\n&quot;;
    @list=<DATA>;
close (DATA);
chomp (@list);

foreach $list(@list)  {
   $list =~ s/^\|/none|/;     # null first field
   $list =~ s/\|$/|none/;     # null last field
   $list =~ s/\|\|/|none|/g;  # null field
   #Do whatever with the data
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Excellent everyone! :)

Thanks for all of the replies, I love the way everyone jumps in with their nickels worth of advice here (Was 2 cents worth, but with inflation..... :) )

Once again, thanks all,
Jim
 
Another way:
Code:
open( DATA, $data );
@vals = map { $_ || 'None' } split( /|/, <DATA>, -1);
chomp($vals[$#vals]);
close( DATA );
I'm not in front of UNIX at the moment, but I think this should work ;-) Cheers, Neil
 
Now that I am in front of UNIX, my code is FULL of errors. Please ignore my code, and use Tracy's clean (and bug free) solution.
Cheers Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top