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

Flat File w/ delimiters - Quick Question 2

Status
Not open for further replies.

Alphabin

Programmer
Dec 8, 2002
119
CA
Hi,

I have data in a flat file seperated by delimiters.

Ex:

ID09000|Name|Name02|23

I'm using the following code in order to split all this and put it into an array:

Code:
	my @field=split(/\|/,$record);
	chomp $record;

Now if I want to use the value (for comparaison etc..) in the $field[3] which is "23" it's not working because the $field[3] is interpreted as "23 " (with space)

Thank you in advance
 
That may depend more on the comparison function you are using.

for example:
"23 " == 23 returns true, and 18 <= " 0 " returns false.

gt, ge, lt, le, eq, ne are the string comparators
>, >=, <, <=, =, != are the corresponding numeric comparators, which will do their best to convert their operands into numbers.

Brad Gunsalus
bardley90@hotmail.com
 
No. It's not cause by the use of the wrong comparison operator. When I simply display the value in the field, I get "23 " (with a space after the value)
 
one other thing i noticed, you're chomping after you separate your line! not sure where the space would come from though.

if all else fails, you may just have to resort to a good old
$field[3] =~ s/\s+$//

Brad Gunsalus
bardley90@hotmail.com
 
you may be able to tweak the expression for the delimiter in the split statement.... something like:
Code:
chomp $record;
my @field=split(/\s+\|\s+/,$record);

I think that says the delimiter is any amount of white space followed by a pipe followed by any amount of white space. That should trim leading and trailing white space from the values........ unless I'm wrong. ;-)

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Code:
$record="ID09000|Name|Name02|23";
my @field=split(/\|/,$record);
print "[$field[3]]\n";

get "23"
chomp has no effect ($record has already been split)
unless there is a newline at the end of $record.

either way, no "23 "
 
Hi - Are we missing something here?
$record is being Chomped after $field[3] is assigned.
<code>
my @field=split(/\|/,$record);
chomp $field[3];
</code>
Keith
 
quick and dirty...

add zero to the value - this will 'force' it to a number


Kind Regards
Duncan
 
Thank you to all for your help.

After looking closely at the Flat File I've noticed that a whitespace was added at the end of each line. The whitespace was generated by the "exporting tool" in Oracle.
So now everything is working perfectly..

Thank you again...
 
This may already be resolved, but, after the sloppy post above, I needed to do a little better job.

My regex for the split expression was pretty poorly thought through. This works a little better. It removes any leading or trailing spaces on the values in the fields while it splits them.

Code:
#!/usr/bin/perl
@lines = ('ID09000|Name03|Name02|23 ',
	'ID09004| 4Name|Name04 |24',
	' ID09005|5Name| Name05|25');

foreach $line (@lines)
    {
    chomp $line; # superfluous in this example
    $line =~ s/\s*(.*)/$1/g; # remove any leading space.
    @fields = split /\s*\|\s*/, $line; # split fields
    foreach (@fields) { print "->$_<-\n";}
    print "\n";
    }

The output looks like....
Code:
->ID09000<-
->Name03<-
->Name02<-
->23 <-

->ID09004<-
->4Name<-
->Name04<-
->24<-

->ID09005<-
->5Name<-
->Name05<-
->25<-



AudioPro, it appears you have '<' and '>' for your code tags. I thing the TGML expects []. ..... unless I'm wrong, again.

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Cheers goBoating
I have searched the FAQ etc. for a description of all the code / quote syntax but been unable to find the info.
Code:
Ah that Darned syntax
 
audiopro,

There is a link just below the text box that you type your post in. It is in the row labelled 'Step 2 Options' and says 'Process TGML'. That's it.

It is not obvious that that link provides the TMGL syntax or even what TGML is. But, that's it.


'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top