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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho