Hi all , I have this form of representation in array (N X 2) for my graph(undirected):- a b c d a d c a f g f h What i want is its representation in this form my %connections=(a=>[b,d],c=>[d,a],f=>[g,h]);

Now do you know how to modify the above code so that it would read the input data(in the same format as presented in comments section) from a text file (each in a new line). Actually I want to represent the Nx2 output which is ::: ==========connections====== from->to a b c d a d

As per all your suggestions I did this but its not printing anything at all. Laurent are you reading the "DATA" from a text file in the same directory if yes then let me know a little about how you are doing it. Because Im actually reading the @con in the while loop.

I gave you the complete way of printing the data the way you wish in order to show how to access the data elements in the structure, but if you just want to pretty print the hash data structure, you should probably use the Data::Dumper module.

Thanks a lot Laurent. But my problem is that I want to read the "DATA" generated from the program itself and thus my "DATA" isn't static/hard coded. So could you please help figure how to format the @con properly ,because @con has to be used for further data analysis and passing on to other functions etc. in an extension of the same program. So if there's a fault with @con then that's my main concern.

I for sure understand that you don't want to process static data appended at the end of your program. That wouldn't be very useful, this was just an example for you. If you were reading from a file lines looking like the sample data at the end of my program, you could use exactly the same syntax.

Your slightly different syntax is correct to visit all the @con array elements.

But for your program as you showed it to work correctly, each of the @con array elements needs to contain a pair of words separated by a white space. For example, you could possibly have:

to work correctly, the @con array element being visited must contain two words separated by a white space (or, at least two words, but if there are more than two words, only the first two words would be taken into account).

In other words, each array element must contain something looking more or less like anyone of the lines I used in my input example (except that the words may contain more than one letter).

The alternative is that you tell me what you currently have in the @con array, so that I can change my sample syntax to fit it to your data format, if possible.

Currently, it looks like @con might just be empty. Possibily you chould try to print it contents. Or maybe I could help you if you told me how you are currently trying to populate this array.

Thanks Laurent, 1.> Actually the content of @con(which has two columns only) is there in the post #6 of this thread. ==========connections====== from->to a b c d a d 2.> @con is getting its content from @array which has 3 columns. So if the user enters these as arguments to the code :- 'a,b|c' 'c,d|e' 'a,d|e'Then the code should save this input in 3x3 array(a row corresponding to each of the three input). Then the code should extract first two columns of this array and save them in @con (connection). Now this @con will be used to generate the hash the result of which should be :- (a=>[b,d],c=>[d])

Laurent is certainly right. Your requirements are hopelessly inconsistent from one post to the next. I have answered a nearly identical post in the perl monks forum. This suggests that either this is a school assignment or you have no interest in learning perl, but want someone to do your work for you. If this is not true, I suggest that you post a complete working package, including your perl code, all referenced data files, instructions for running it, and the exact output expected. We will be able to run it, examine the output, and help you correct the code.

Your post #15 is a step in the right direction, but I am not sure what code you are using or exactly what is wrong. I do not see any need for either @array or @con. I would parse the arguments with a regular expression, and store the data directly in the hash. Note: Your sample data is used if no arguments are provided.

Code

# Takes input in the form 'a,b|c' # How to run : perl code.pl 'a,b|c' 'c,d|e' 'a,d|e' # Outputs a NX3 for the above input data. # Outputs connections in Nx2 form use strict; use warnings;

That was spot on !!!! Thanks Bill, Laurent and wikedxter. And its not a homework but related to some research. I definitely will try to frame my questions properly the next time onwards. Now I have to parse through this Hash in order to find all the connected components in this graph (along with their vertices and connections). Could you suggest me a module or algorithm which would help me achieve the same.