Help with Perl (my first assignment).

They have: 24 posts

Joined: Oct 2001

This should be really simple for you programmers out there, but it's destroying my virgin-programming mind. In the following directory are my files: http://www.andrew.cmu.edu/user/wdchan/carnegie/70643/

Lab2a.pl is the document my professor gave me. What it does is access the URL specified which is google.com and store the html code of the document in a string.

My professor wants me to add code to it so that if I type in "web" it'll automatically go through the string and count the number of occurences for "web".

Functions.txt is the file with the commands I pulled out of my notes from the previous class. My professor stated that those were all I needed, but after messing with them for a couple of hours, I gave up and started searching the net. I'm still in the process of going through some more tutorials but I think I'm heading in the right directions.

Lab2b.pl is my edited document so far. It at least returns a number so I guess it's counting something. But the number it returns is incorrect. I'm not sure where it's pulling the number from. For example, I've been using "web" and I think there are two occurences in the document. But when I run the script it returns 2142. I would guess the next step for me is to somehow use the other functions from my notes. But I'm confused as to why 'length($string) would be needed, and what the substr in my notes actually does.

Any hints or tips as to what I'm doing wrong would be greatly appreciated.

update: Nevermind. I was so freaking off in my coding. I definitely SUCK at this stuff. Had to use an array and the functions my professor gave didn't even make it into the final code. BAH! Wasted my time with his functions and what not.

DC Domain r5

"What I had, I gave today.
What I saved, I lost forever."

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

Don't get to discouraged, it can take a while for your brain to adapt to logical programming! You'll know when it happens because after that you tend to act like a Vulcan and say stuff like:

"That is not logical"

or

"After further analysis that does not compute"

PJ | Are we there yet?
pjboettcher.com

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

looking at your functions list...
here's something I came up with (not tested)

# $page_text
$search_string = "web";

while (index($page_text, $search_string) > -1) {
   
    # find the position of the last character of $search_string
    $start = index($page_text, $search_string) + length($search_string);
   
    # how many character from that point to the end of $page_text
    $end = length($page_text) - $start;
   
    # cut the $search_string and everything before it out of $page_text
    $page_text=substr($page_text,$start,$end);
   
}
'
I think that's a rather difficult first perl script.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 24 posts

Joined: Oct 2001

Thanks for the help. I ended up dumping all the functions my professor gave me (wasted a weekend). And with the help of a friend, came up with the following. I also neglected to add that I was looking to search the document with whatever the user input, not just 'web.' So I had to grab the user's input and drop it into a string like: $string = ; Anyway below was all I needed. Don't know why my professor made it so hard.

my $userinput = ;
chomp $userinput;

my @array = split(/$userinput/, $info);
print "\n$#array occurences of \"$userinput\"";

This programming stuff boggles my mind. Mad respect to you people.

DC Domain r5

"What I had, I gave today.
What I saved, I lost forever."

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.