APM: Regular Expression Question

Tim Peoples tim at toolman.org
Wed Aug 16 09:12:51 PDT 2006



Ahhh... in that case, I'd think you really want a split-map-join 
construct like so:

    foreach my $piece (@pieces) {
        $cnt = 6;
        print join("\t", map { ($cnt-- > 0) ? "\"$_\"" : $_ }
    split(/\t/, $piece)), "\n";;
    }

Tim.

Barron Snyder (CE CEN) wrote:
> Here is some sample data (input):
> FL	999-NO_SUBTEAM	Actuals	ASSETS	FY2006	6	19,416.86
> FL	DP-999	Actuals	150000	FY2006	6	19,416.86
> FL	DP-999	Actuals	WIP	FY2006	6	19,416.86
> FL	DP-999	Actuals	TOT_PPE	FY2006	6	19,416.86
> FL	DP-999	Actuals	LT_ASSET	FY2006	6	19,416.86
> FL	DP-999	Actuals	ASSETS	FY2006	6	19,416.86
> FL	NON_MARGIN	Actuals	510000	FY2006	6	11,866.97
> FL	NON_MARGIN	Actuals	SUPP_PKG	FY2006	6
> 11,866.97
>
> And here is what it should end up like (output):
> "FL"	"999-NO_SUBTEAM"	"Actuals"	"ASSETS"	"FY2006"
> "6"	19,416.86
> "FL"	"DP-999"	"Actuals"	"150000"	"FY2006"
> "6"	19,416.86
> "FL"	"DP-999"	"Actuals"	"WIP"	"FY2006"	"6"
> 19,416.86
> "FL"	"DP-999"	"Actuals"	"TOT_PPE"	"FY2006"
> "6"	19,416.86
> "FL"	"DP-999"	"Actuals"	"LT_ASSET"	"FY2006"
> "6"	19,416.86
> "FL"	"DP-999"	"Actuals"	"ASSETS"	"FY2006"
> "6"	19,416.86
> "FL"	"NON_MARGIN"	"Actuals"	"510000"	"FY2006"
> "6"	11,866.97
> "FL"	"NON_MARGIN"	"Actuals"	"SUPP_PKG"	"FY2006"
> "6"	11,866.97
>
> All values except those in the final column should be wrapped in
> double-quotes and tabs should separate the values.
>
> My solution does it like this:
> ...
> foreach my $piece (@pieces) {
>    	my @strings = split(/\t/, $piece);
>    	print DATA_OUT "\"", join ("\"\t\"", $strings[0], $strings[1],
> $strings[2], $strings[3], $strings[4], $strings[5]), "\"\t",
> $strings[6], "\n";
> }
> ...
>
> But as I mentioned, in my effort to learn more about Perl, I thout there
> may be a more elegant way using regular expressions.
>
> Thanks,
>             
> Barron Snyder
>
> -----Original Message-----
> From: austin-bounces+barron.snyder=wholefoods.com at pm.org
> [mailto:austin-bounces+barron.snyder=wholefoods.com at pm.org] On Behalf Of
> Zach Vonler
> Sent: Wednesday, August 16, 2006 10:42 AM
> To: austin at pm.org
> Subject: Re: APM: Regular Expression Question
>
> On 8/16/06, Jay Flaherty <jayflaherty at gmail.com> wrote:
>   
>> $piece =~ s/\t{0,3}/\"\t\"/g;
>>     
>
> There are two problems with this one, the first being that you have
> the ability to match on a null string, and the second being that
> whatever does get matched is replaced by only a single "\t".
>
> If the number of fields you want to modify is in $count, something like
>
> $repl = "\\\"\\t\\\"" x $count;
> $piece =~ s/\t{$count,$count}/$repl/;
>
> might get you most of the way there.  Note of course that it does not
> modify inputs with fewer than $count fields.
>
> Later,
> Zach
> _______________________________________________
> Austin mailing list
> Austin at pm.org
> http://mail.pm.org/mailman/listinfo/austin
> _______________________________________________
> Austin mailing list
> Austin at pm.org
> http://mail.pm.org/mailman/listinfo/austin
>
>   

-- 
 _______________________________________________________________________
                                                      Timothy E. Peoples
                                                   Have Camel, Will Code
                                                         tim at toolman.org




This message and any attachments are intended only for the use of the addressee and may contain information that is privileged and confidential. If the reader of the message is not the intended recipient or an authorized representative of the intended recipient, you are hereby notified that any dissemination of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by e-mail and delete the message and any attachments from your system.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/austin/attachments/20060816/0a687f8a/attachment-0001.html 


More information about the Austin mailing list