• Index
  •  » Talend Open Studio for Data Integration » Usage, Operation
  •  » [resolved] Trimming whitespace from every element in an array using tperlrow

#1 2010-01-26 21:46:00

mvucovich
Member
Registered: 2009-09-01
Posts: 30

[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

Offline

#2 2010-01-27 04:32:47

shong
Talend team
Registered: 2007-08-29
Posts: 10309
Website

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:

Code:

sub trim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}

Best regards

          shong


Uploaded Images


Email:shong@talend.com
Choose Talend, Enjoy Talend!
New & Event: Talend Help Center
Talend-->the leader of open source data management and application integration solutions!

Offline

#3 2010-02-01 18:34:07

mvucovich
Member
Registered: 2009-09-01
Posts: 30

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

Offline

#4 2010-02-03 07:57:38

shong
Talend team
Registered: 2007-08-29
Posts: 10309
Website

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:

Code:

for ($count = 0; $count <=3; $count++) {            #suppose there are 4 columns
@output_row[$count] = trim(@input_row[$count]);
}

Best regards

          shong


Email:shong@talend.com
Choose Talend, Enjoy Talend!
New & Event: Talend Help Center
Talend-->the leader of open source data management and application integration solutions!

Offline

#5 2010-02-03 17:00:19

mvucovich
Member
Registered: 2009-09-01
Posts: 30

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

Thanks!  that worked great.

Offline

  • Index
  •  » Talend Open Studio for Data Integration » Usage, Operation
  •  » [resolved] Trimming whitespace from every element in an array using tperlrow

Board footer

Powered by FluxBB