Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
reading a text file and extracting info

Posted by esen (esen), 2 November 2005
hi peeps, im hoping someone can help...
im having a problem reading the text file and outputting a matrix type  display which has the links along the x and y co-ordiantes (or an  abbreviation) and 0 or 1 in the matrix if the page did link to another  page (1) or didnt (0).

I have a text file made up of links in the form of

Visited: http://www...
Link: http://www.whatever url
Link: http://www.whatever url
Link: http://www.whatever url
Link: http://www.whatever url
Visited: http://www.whatever url
Link: http://www.whatever url
Link: http://www.whatever url
Link: http://www.whatever url
Link: http://www.whatever url

etc..im having a problem reading the text file and outputting a matrix type (table)  display which has the urls along the top and left side (or an  abbreviation) and 0 or 1 in the matrix if the page did link to another  page (ie puts in a 1 in the matrix display ) or didnt (puts in a 0).

for instance
Visited: A
Link: B
Link: C


In the matric A couldnt link to itself so a 0 would be put in the matric. A does link to B so a 1 would be output in the matrix..and so on.

would really appreciate some help.. as im fast going grey
thanks all!

Posted by admin (Graham Ellis), 2 November 2005
I think you're looking for something like this:

Code:
while (<DATA>) {
       ($from,$to) = (split)[0,2];
       $ltable{$from}{$to} = 1;
       $used{$from} = 1;
       $used{$to} = 1;
       }
@names = sort(keys(%used)) ;

foreach $row (@names) {
foreach $col (@names) {
       $val = $ltable{$row}{$col} ? 1 : 0;
       print "$val ";
       }
       print "\n";
       }
__END__
a to b
a to c
b to c
d to a
c to b


Which runs to give

Code:
0 1 1 0
0 0 1 0
0 1 0 0
1 0 0 0


I'm not sure I understood the question fully though .. so I won't go into a long explanation in case I got it wrong.   It was fun writing that anyways!

Posted by esen (esen), 3 November 2005
thanks!...i will try to see whats going on


Posted by esen (esen), 3 November 2005
hmmm my head hurts..sorry, which bit does the file reading...probably a very basic question...

Posted by admin (Graham Ellis), 3 November 2005
In my example, I've used <DATA> to read from the same file as the program is in, after the __END__ marker.   It's a great way of writing a demo as it means that the whole thing is selfcontained.

You'll want to open a file in the usual way, and read from it in your while loop ...



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho