[resolved] Trimming whitespace from every element in an array using tperlrow

One Star

[resolved] Trimming whitespace from every element in an array using tperlrow

Hi,

I am tired of typing @output_row[0] = trim(@output_row[0]);
@output_row[1] = trim(@output_row[1]);
ect...

for every element in my array. How can I write code that will apply the trim function to every column using the @output_row array, so I only have to type this once? I'm trying a foreach loop but I just can't seem to figure it out (the output is always a one column dataset, with the value of the column = 4).

Thanks,

Michael
Community Manager

Re: [resolved] Trimming whitespace from every element in an array using tperlrow

Hello
Perl does not have a built-in trim function. You need use tPerl component to define a trim function first, eg:
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}

Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] Trimming whitespace from every element in an array using tperlrow

Thanks Shong,

I'm sorry, I was unclear. I have built the trim function--What I'd like to do is apply it to all columns in an array without needing to write a line of code invoking the function for each column.

I was hoping something like @output_row = trim(@input_row) in a tperlrow node would work, but it doesn't. I've tried building a subroutine using a foreach loop and failed there as well.

Any idea how to accomplish this?

Thanks,

Michael
Community Manager

Re: [resolved] Trimming whitespace from every element in an array using tperlrow

Hello
but it doesn't. I've tried building a subroutine using a foreach loop and failed there as well.

Yes, if there are so many columns, it is a big work to write each line of code invoking the function for each column. Using a foreach loop could be a good solution.
for example:
on tPerlRow:
for ($count = 0; $count <=3; $count++) {            #suppose there are 4 columns
@output_row[$count] = trim(@input_row[$count]);
}

Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] Trimming whitespace from every element in an array using tperlrow

Thanks! that worked great.

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

Have you checked out Talend’s 2019 Summer release yet?

Find out about Talend's 2019 Summer release

Blog

Talend Summer 2019 – What’s New?

Talend continues to revolutionize how businesses leverage speed and manage scale

Watch Now

6 Ways to Start Utilizing Machine Learning with Amazon We Services and Talend

Look at6 ways to start utilizing Machine Learning with Amazon We Services and Talend

Blog