[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] a better tab-delimited test parser




	After looking in vain for a pm that implemented a tab-delimited  file
parser, I found I had to write it myself, and so I started jotting code
together. And even though I 'd like to claim my place with those who
deserve to be subscribed to this list, I could not do it in a fast,
terse and elegant way.

	Now that the job's done (it's Perl, isn't it? the job's done!), I'm
into the fun of finding a --proper-- way to do it. Does anyone want to
jump in?

	Maybe it's too easy? 




martin

#!/usr/bin/perl -w

use strict;
use Data::Dumper;

print Dumper parse_line(q{^1^,180,1,^0-0170^,^ASIDERO, MITAD
DERECHA^,^^,45.62,20.00,0.00,0.00,^^,0,0.00}, ',', '\^');


sub parse_line {
	my $line		= shift;
	my $delimiter	= shift || qq{\t};
	my $quote		= shift || q{"};
	
	# doing it quick and dirty, assume no escaping of quotes
	
	# split on quotes
	my @quoted = split($quote, $line);
	
	# now for the second part ... 

	# I assume that every even element was quoted, 
	# so I'll leave it like it is... 
	my @result = ();
	for (my $n=0 ; $n<@quoted ; $n++){
		# its an even element, was quoted, don't touch it... 
		if ($n%2){
			push @result, $quoted[$n];
			next;
		}
		
		# before trimming, skip cases of empty str or just commas
		# that can only happen at the beggining/end of the array
		# and in between quoted elements.
		next if $quoted[$n] eq '';
		next if $quoted[$n] eq $delimiter;
		
		# trim leading and trailing $delimiters to ease out 
		# any transition to/from quoted elements to unquoted elements
		$quoted[$n] =~ s/^$delimiter//;
		$quoted[$n] =~ s/$delimiter$//;
		
		push (@result, split($delimiter, $quoted[$n]));
					
	}
	
	return \@result;
}

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe