×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Perl Help with Concatenation

Perl Help with Concatenation

Perl Help with Concatenation

(OP)
Hi All,

I need help with Perl Concatenation based on a condition.

In the example file below, the condition is as long as the ID is the same the values of fields need to be concatenated. The expected output will show what I mean.

My Input file is attached.

I want output in the below format

UC "TS_206" /DATA=(ACTION_ORDER=["1","2","3","4"],ACTION_DATE=["26/07/15","26/07/15","26/07/15","26/07/15"],ACTION_COMMENT=["Created","InProgress","Tested","Completed"])
UC "TS_450" /DATA=(ACTION_ORDER=["1","2","3"],ACTION_DATE=["02/08/15","03/08/15","03/08/15"],ACTION_COMMENT=["Created","InProgress","Rejected"])

Please help with the looping syntax.

Many thanks in advance.

RE: Perl Help with Concatenation

Hi

CODE --> command line

master # perl -nae 'sub p{print"UC \"$l\" /DATA=(",join(",",map{"$h[$_]=[".join(",",map{"\"$_\""}@{$s[$_]})."]"}0..2),")\n"if@s;@s=([],[],[])}if($.==1){@h=map{uc s/\B(?=[A-Z])/_/gr}@F[1..3];next}p if@F[0]ne$l;$l=shift@F;push@{$s[$_]},$F[$_]for 0..2;END{p}' New_Text_Document_\(3\).txt
UC "TS_206" /DATA=(ACTION_ORDER=["1","2","3","4"],ACTION_DATE=["26/07/15","26/07/15","26/07/15","26/07/15"],ACTION_COMMENT=["Created","InProgress","Tested","Completed"])
UC "TS_450" /DATA=(ACTION_ORDER=["1","2","3"],ACTION_DATE=["02/08/15","03/08/15","03/08/15"],ACTION_COMMENT=["Created","InProgress","Rejected"]) 

Feherke.
feherke.ga

RE: Perl Help with Concatenation

(OP)
Hi Feherke.

Many thanks to reply back to my post and to help me with the code.

I get the following error on executing the command

Can't find string terminator "'" anywhere before EOF at -e line 1. (error screen shot attached)

I see END{p}' at the end not sure where the error is generated.

Please help.

RE: Perl Help with Concatenation

Hi

Your problem is that CMD.exe does not support single quote ( ' ) as string delimiter.

Better put all the part enclosed in single quotes in a file

CODE --> your_script_file.pl

sub p {
    print "UC \"$l\" /DATA=(", join(",", map{ "$h[$_]=[" . join(",", map { "\"$_\"" } @{$s[$_]}) . "]" } 0..2), ")\n" if @s;
    @s = ([], [], [])
}
if ($. == 1) {
    @h = map { uc s/\B(?=[A-Z])/_/gr } @F[1..3];
    next
}
p if @F[0] ne $l;
$l = shift @F;
push @{$s[$_]}, $F[$_] for 0..2;
END {
    p
} 
...then run it as :

CODE --> command line

perl -na your_script_file.pl New_Text_Document_\(3\).txt 

Feherke.
feherke.ga

RE: Perl Help with Concatenation

(OP)
Hello Feherke,

As you suggested, when I put the code to a file and executed it works fine.

I would appreciate if you could help me with the problem I have.

Sometimes my input file could contain more than one word for example Heading ActionComment could contain "Created by Zara".

The current code picks only Created and ignores (or puts into next column header if exists) the rest of the value (by Zara).

How do I tell the code to not break on spaces but on tab/tabs as my column values are separated by tabs.

I am attaching the input file with changes to column values so you could try and help me.

Many Thanks again.

RE: Perl Help with Concatenation

Hi

That should be simple to fix :

CODE --> command line

perl -nalF"\t" your_script_file.pl New_Text_Document_\(3\).txt 
And to compensate the -l added in the command line, remove the "\n" from your_script_file.pl.

As I am on Linux and your input file contains Windows line separators, the above alone not works for me, for which I had to add another line in the code :

CODE

# ...
$F[3] =~ s/\s+$//;
if ($. == 1) {
    # ...
}
# ... 
I suppose you will not need this, but adding it will not harm anyway.

Feherke.
feherke.ga

RE: Perl Help with Concatenation

(OP)
Thank you very much Feherke. Your code works fine.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close