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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
joining elements


New User

Nov 9, 2015, 9:45 AM

Post #1 of 2 (1260 views)
joining elements Can't Post


I have a names.txt file that has the following contents:
02 Jhon
02 Nickolas
02 Marcel
03 Nick
03 Marcia
04 Humbert

How do I do to make it so:
02 Jhon|Nickolas|Marcel
03 Nick|Marcia
04 Humbert ?

I tried to use the join() function but I can not make the program. Can you help me?

(This post was edited by valter1 on Nov 9, 2015, 9:46 AM)


Nov 9, 2015, 10:58 AM

Post #2 of 2 (1253 views)
Re: [valter1] joining elements [In reply to] Can't Post

Read the data in a while loop.

Use the split function to separate the number and name.

Store the data in a hash. Use the number as the key. Use the concatenation operator (. or .=) to concatenate the name to the string of names which already correspond to that key.

After the hash is complete, print it in a foreach loop.
If it is important to print the lines in the order that you show, you must sort the keys of the hash.

foreach my number (sort  keys %hash) { 
print ... ;

I hope this helps. Remember, we are here to help not to do your homework for you. Feel free to ask more specific questions, but show your work.
Good Luck,


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

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