spyder78 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have been given 6 questions to research as part of my studies and I have answered 4 out of the 6 but I am struggling with the two questions below. I am only a junior at Perl and really am struggling. Please help.

[1]   If you have ever used an Internet browser you may have come across links that don't go anywhere (broken links) which are extremely annoying. Part of the challenge of making programs that put in hypertext links is ensuring the link the program puts in always has an associated destination or anchor. An associated problem is ensuring that jump destinations or anchors are unique. i.e. jump destinations are never repeated.

What variable type in Perl could you use, that would identify repeated jump destinations.

Write a subroutine that uses the above data type (you can assume the data type is a global variable), takes as input a jump destination and prints to STDOUT a warning message if it has already received the input jump destination.
Below is a skeleton subroutine you should base your answer on:

sub CheckJumpDestination { my($jump_destination) = @_; if ( # Your code here ) { # Your code here. } else { # Your code here. } }
[2]    What will the following subroutine return (DoSomething) given the input value below:
%unreported_case_list = ( '30/11/90, cp654/88' => '1' ); &DoSomething('In <FD:"Case Title">Bradburn v Harris</FD:"Case Title"> +<FD:"Case Citation">30/11/90, Fisher J, HC Auckland CP654/88</FD:"cas +e Citation"> Fisher J was dealing with a counterclaim for interest un +der two vendor mortgages.'); sub DoSomething { my($line) = @_; my($new_line, $citation, $start_of_line, $jump_destination) = ('','','','',''); DEBUG('c',"IN DoSomething\n"); while ( $line =~ s{^ ( .*? ) < FD:"Case \s Citation" > ( .+? ) </ +FD:"Case \s Citation" > }{}xi ) { $start_of_line = $1; $citation = &StripWhiteSpace($2); if ( $citation !~ m{ \b tclr \b }xi ) { $jump_destination = lc($citation); if ( exists($unreported_case_list{$jump_destination}) ) { $new_line .= "$start_of_line<FD:\"Case Citation\"><JL: +\"Internal Jump\",\"$jump_destination\">$citation</JL></FD:\"Case Cit +ation\">"; $unreported_jl_count++; } elsif ( $citation =~ m{ ( \d{1,2} / \d{1,2} / \d{4} | \d{2 +} ) , .+? ( [a-z]+\d+ (?: / \d+ )? ) }xi ) { $jump_destination = &NormaliseDate("$1").', '.lc($2); if ( exists($unreported_case_list{$jump_destination} +) ) { $new_line .= "$start_of_line<FD:\"Case Citation\"> +<JL:\"Internal Jump\",\"$jump_destination\">$citation</JL></FD:\"Case + Citation\">"; $unreported_jl_count++; } else { $new_line .= "$start_of_line<FD:\"Case Citation\"> +$citation</FD:\"Case Citation\">"; } } else { $new_line .= "$start_of_line<FD:\"Case Citation\">$cit +ation</FD:\"Case Citation\">"; } } else { $new_line .= "$start_of_line<FD:\"Case Citation\">$citatio +n</FD:\"Case Citation\">"; } } $new_line .= $line; DEBUG('c',"LEAVING DoSomething\n"); return($new_line); } #-------------------------------NormaliseDate------------------------- +---- # INPUT # $date # # RETURNS # normalised date # # Normalises the input date to dd/dd/dddd format. #--------------------------------------------------------------------- +---- sub NormaliseDate { my($date) = @_; my($day, $year, $month) = (0,0,0); DEBUG('c',"IN NormaliseDate\n"); if ( $date =~ m{^ ( \d{1,2} ) / ( \d{1,2} ) / ( \d{4} | \d{2} ) $} +x ) { $day = $1; $month = $2; $year = $3; if ( length($day) == 1 ) { $day = '0'.$day; } if ( length($month) == 1 ) { $month = '0'.$month; } if ( length($year) == 2 ) { $year = '19'.$year; } } DEBUG('c',"LEAVING NormaliseDate\n"); return("$day/$month/$year"); } #-----------------------------StripWhiteSapce------------------------- +---- # INPUT # $line # # RETURNS # $line # # Removes leading and trailing whitespace from the input string. #--------------------------------------------------------------------- +---- sub StripWhiteSapce { my($line) = @_; DEBUG('c',"IN StripWhiteSapce \n"); $line =~ s{^ \s+ }{}x; $line =~ s{ \s+ $}{}x; DEBUG('c',"LEAVING StripWhiteSapce \n"); return($line); }

update (broquaint): added formatting
update 2 (broquaint): title change (was Perl Newbe Urgently Needs Help)

Replies are listed 'Best First'.
Re: Perl junior entreats monastery for course help
by ViceRaid (Chaplain) on Jun 16, 2003 at 10:36 UTC

    Hello

    They're your research questions, not my research questions, so I'm not going to give you a straight answer. Sorry, but I hope you'll find the following useful.

    Question 1: take a look at the Perl FAQ section discussing finding unique elements in a list, and the subsequent section on "How can I tell whether a list or array contains a certain element?". The good authors of the document say:

    Hearing the word "in" is an indication that you probably should have used a hash

    Question 2: Try following the function calls through. The first function that gets called is DoSomething, and the argument it gets passed is the big long string about "Bradburn vs Harris". The functions do some stuff manipulating the strings with regular expressions, and those hashes turn up again with functions like exists. Try reading up on these.

    You could try running the program too, assuming you trust the person it came from. I'll give you a clue too, it won't do anything harmful to your PC. You could install Perl, save the program in a file and run it to see what happens. You'll need to correct a typo in the line sub StripWhiteSappace, and you might add a DEBUG function to follow what's going on.

    HTH
    ViceRaid

Re: Perl junior entreats monastery for course help
by Skeeve (Parson) on Jun 16, 2003 at 10:21 UTC
    Tell us your thoughts about answers to the questions. You don't want us to do your homework, do you?

    My Answer to Question 1 is:
    You can use any data type. Some are more appropriate than others ;-)

      This is what I think Question 1 could be.. I have no idea to be honest. sub CheckJumpDestination { my($jump_destination) = @_; if (exists($jump_destination)) { print STDOUT "Jump destination $jump_destination has already been received.\n"; } else { # Your code here. } }
        So you know about "exists". Tell us more about it. It's a good idea (really!).
Re: Perl junior entreats monastery for course help
by erasei (Pilgrim) on Jun 16, 2003 at 13:58 UTC
    The above posts have done a good job at trying to help you without giving you direct answers, so I won't try to add to what they say.

    However, I just wanted to compliment you on being honest and straightforward with us about this being coursework. A lot of posters here try to pass it off as not being school-related, and get busted pretty quickly. By being honest and open with the monks, you will find, as in this post, that they provide you with links and insights that will help you to help yourself.