CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
concatenating strings and joining strings

 



alferic
Novice

Sep 10, 2013, 12:56 AM

Post #1 of 11 (908 views)
concatenating strings and joining strings Can't Post

Hello Perl Guru's,

I have this lines on my file"

T1 CONT 11 100.00 0 0.00 | T2 IEB 11 100.00 0 0.00 |
T3 IEB 11 100.00 0 0.00 | T4 ICBO 11 100.00 0 0.00 |
T5 ICBO 11 100.00 0 0.00 | T6 BVEB 11 100.00 0 0.00 |
T7 VFBE 11 100.00 0 0.00 | T8 VFBE 11 100.00 0 0.00 |

I wanted to split "|" and joined it into one that will look below

T1 CONT 11 100.00 0 0.00
T3 IEB 11 100.00 0 0.00
T5 ICBO 11 100.00 0 0.00
T7 VFBE 11 100.00 0 0.00
T2 IEB 11 100.00 0 0.00
T4 ICBO 11 100.00 0 0.00
T4 ICBO 11 100.00 0 0.00
T6 BVEB 11 100.00 0 0.00
T8 VFBE 11 100.00 0 0.00

please help!

Thanks!


(This post was edited by alferic on Sep 10, 2013, 12:59 AM)


BillKSmith
Veteran

Sep 10, 2013, 5:37 AM

Post #2 of 11 (899 views)
Re: [alferic] concatenating strings and joining strings [In reply to] Can't Post

Show us what you have tried. We are not here to do your homework.

Hints.
The regex for the split is a bit tricky. You must account for whitespace around the pipe character. Also remember that the pipe character is a metacharacter in a regex. You must escape it.

You need the first two fields from each line. To get the order right, you must accumulate the first and second fields separately and combine them at the end.
Good Luck,
Bill


alferic
Novice

Sep 10, 2013, 8:24 AM

Post #3 of 11 (894 views)
Re: [BillKSmith] concatenating strings and joining strings [In reply to] Can't Post

i tried this:

($dummy1, $dummy2) = split /\|/, $line;
($dummy3) = join ("\n", $dummy1, $dummy2);
print "$dummy3\n";

the above prints:

T1 CONT 21 100.00 0 0.00
T2 IEB 21 100.00 0 0.00
T3 IEB 21 100.00 0 0.00
T4 ICBO 21 100.00 0 0.00
T5 ICBO 21 100.00 0 0.00
T6 BVEB 21 100.00 1 4.76
T7 VFBE 21 100.00 0 0.00
T8 VFBE 21 100.00 0 0.00

I noticed that there is a space before T2,T4,T6,T8
then I split "$dummy3" by spaces and stored into and array

(@dummy) = split /[\s]+/, $dummy3;
print "@dummy\n";

but it prints like this:

T1 CONT 21 100.00 0 0.00 T2 IEB 21 100.00 0 0.00
T3 IEB 21 100.00 0 0.00 T4 ICBO 21 100.00 0 0.00
T5 ICBO 21 100.00 0 0.00 T6 BVEB 21 100.00 1 4.76
T7 VFBE 21 100.00 0 0.00 T8 VFBE 21 100.00 0 0.00


the script:

if ($line =~ /^T\d{1,}/)
{
($dummy1, $dummy2) = split /\|/, $line;
($dummy3) = join ("\n", $dummy1, $dummy2);
(@dummy) = split /[\s]+/, $dummy3;
print "@dummy\n";
}


Laurent_R
Veteran / Moderator

Sep 10, 2013, 8:34 AM

Post #4 of 11 (890 views)
Re: [alferic] concatenating strings and joining strings [In reply to] Can't Post

Change your first try to remove the spaces during the split when they occur:


Code
($dummy1, $dummy2) = split /\s?\|\s?/, $line; 
($dummy3) = join ("\n", $dummy1, $dummy2);
print "$dummy3\n";



BillKSmith
Veteran

Sep 10, 2013, 9:48 AM

Post #5 of 11 (887 views)
Re: [alferic] concatenating strings and joining strings [In reply to] Can't Post


Quote
I noticed that there is a space before T2,T4,T6,T8

There is also a space character at the end of every line of your output. They are both part of what I was warning you about in my first hint.

Use

Code
/\s*\|\s*/

as the pattern for your first (and only) split.

Your output is in the order T1, T2, T3... not the T1, T5, T2, T6... that you specified in your first post. That is the subject of my second hint which you ignored completely.

For each line, push the odd numbered field into one array and the even number field into another. After you complete the reading loop, concatenate the two arrays.

You probably do not want newlines in your strings. Use the special variable \" to supply them in your printed output.
Good Luck,
Bill


alferic
Novice

Sep 10, 2013, 11:36 PM

Post #6 of 11 (878 views)
Re: [Laurent_R] concatenating strings and joining strings [In reply to] Can't Post

Hi Laurent,

Thanks for the reply, the regex seems to work. Now I get this format correctly:
T1 CONT 21 100.00 0 0.00
T2 IEB 21 100.00 0 0.00
T3 IEB 21 100.00 0 0.00
T4 ICBO 21 100.00 0 0.00
T5 ICBO 21 100.00 0 0.00
T6 BVEB 21 100.00 1 4.76
T7 VFBE 21 100.00 0 0.00
T8 VFBE 21 100.00 0 0.00

Now the only problem that I have is when i split the new string and stored into array and wanted to display element [0]. The below script I used:

(@dummy) = split /\s+/, $dummy3;
print "$dummy[0]\n";

I wanted to display this:

T1
T2
T3
T4
T5
T6
T7
T8

but it gave me this:
T1
T3
T5
T7

Any ideas?


alferic
Novice

Sep 10, 2013, 11:40 PM

Post #7 of 11 (876 views)
Re: [BillKSmith] concatenating strings and joining strings [In reply to] Can't Post

Hi Bill,

Your regex suggestions also works. Thanks! I also get this format correctly:
T1 CONT 21 100.00 0 0.00
T2 IEB 21 100.00 0 0.00
T3 IEB 21 100.00 0 0.00
T4 ICBO 21 100.00 0 0.00
T5 ICBO 21 100.00 0 0.00
T6 BVEB 21 100.00 1 4.76
T7 VFBE 21 100.00 0 0.00
T8 VFBE 21 100.00 0 0.00

Now the only problem that I have is when i split the new string and stored into array and wanted to display element [0]. The below script I used:

(@dummy) = split /\s+/, $dummy3;
print "$dummy[0]\n";

I wanted to display this:

T1
T2
T3
T4
T5
T6
T7
T8

but it gave me this:
T1
T3
T5
T7

Any ideas?


Laurent_R
Veteran / Moderator

Sep 11, 2013, 2:59 AM

Post #8 of 11 (869 views)
Re: [alferic] concatenating strings and joining strings [In reply to] Can't Post

Yes, the problem is that $dummy3 is a two-line string. Your split gives you the first field of the first line only.

Try to also print $dummy[6]:


Code
print "$dummy[0]\n$dummy[6]\n";



alferic
Novice

Sep 11, 2013, 4:35 AM

Post #9 of 11 (865 views)
Re: [Laurent_R] concatenating strings and joining strings [In reply to] Can't Post

Thanks Laurent,

I thought that storing the joined strings to 'dummy3' will make the new string something like flat string :( I guess I am wrong.

Is there way to store all values of "dummy[6]" t0 "dummy[0]"... "dummy[7]" to "dummy[1]" and so on and so forth. Not as separate arrays?


Laurent_R
Veteran / Moderator

Sep 11, 2013, 4:53 AM

Post #10 of 11 (863 views)
Re: [alferic] concatenating strings and joining strings [In reply to] Can't Post


In Reply To
Is there way to store all values of "dummy[6]" t0 "dummy[0]"... "dummy[7]" to "dummy[1]" and so on and so forth. Not as separate arrays?


I don't understand what you want. Can you explain further or give an example?


BillKSmith
Veteran

Sep 11, 2013, 5:14 AM

Post #11 of 11 (862 views)
Re: [alferic] concatenating strings and joining strings [In reply to] Can't Post

You seem to have changed your mind about the order. That makes the original problem much easier. I have printed the array @data with Data::Dumper so you can see exactly what it contains.

Next I split off and print the first field of each element.


Code
use strict; 
use warnings;
use Data::Dumper;
my @dummy;
push @dummy, split( /\s*\|\s*/) while (<DATA>);
print Dumper \@dummy;
print ((split)[0],"\n") foreach @dummy;
__DATA__
T1 CONT 11 100.00 0 0.00 | T2 IEB 11 100.00 0 0.00 |
T3 IEB 11 100.00 0 0.00 | T4 ICBO 11 100.00 0 0.00 |
T5 ICBO 11 100.00 0 0.00 | T6 BVEB 11 100.00 0 0.00 |
T7 VFBE 11 100.00 0 0.00 | T8 VFBE 11 100.00 0 0.00 |



Output:

Code
$VAR1 = [ 
'T1 CONT 11 100.00 0 0.00',
'T2 IEB 11 100.00 0 0.00',
'T3 IEB 11 100.00 0 0.00',
'T4 ICBO 11 100.00 0 0.00',
'T5 ICBO 11 100.00 0 0.00',
'T6 BVEB 11 100.00 0 0.00',
'T7 VFBE 11 100.00 0 0.00',
'T8 VFBE 11 100.00 0 0.00'
];
T1
T2
T3
T4
T5
T6
T7
T8


I do not understand the newest requirement which you tried to explain to Laurent. Please post the complete program that you are refering to.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives