Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Optional Arguments..?

by davido (Cardinal)
on Jun 03, 2012 at 07:18 UTC ( [id://974090]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Optional Arguments..?
in thread Optional Arguments..?

It's the one thing that my lecturer told us to google for, to implement in our codes but I just couldn't find a simple explanation to use this.

Google is great, but I'm surprised your professor didn't mention the Perl documentation. It's included free of charge with every Perl distribution, and also at http://perldoc.perl.org. You can read about defined on your own terminal by typing "perldoc -f defined".

Is my following code correct?

No.

Well... your use of defined is reasonable, but besides that, NO. It doesn't compile.

Why would you post code that doesn't compile except to possibly ask why it doesn't? You can check yourself, at which point you can begin working through the error messages. For example, "Not enough arguments for keys at mytest.pl line 27, near "keys =~"" Your line numbers will probably be different, but that error message will still be there, unless the code you posted isn't what's in your editor.

There's no need for me to enumerate the error messages you're getting; Perl will do it for you when you type perl -c mytest.pl, where "mytest.pl" is the name of your script. Once you resolve the issues that perl -c tells you about, you'll run into other problems as well, such as your strange for loop around line 31. It seems to set a topical variable, but the list of items it iterates over is empty, and the print statement on the next line tries to print $_, which isn't going to contain anything meaningful.

Really, there are enough problems you probably should sit down with your instructor for a half hour and go over them together.

If you don't have it already, get yourself a copy of Learning Perl, and start reading. It will begin to flow before you know it. And remember this slightly gentler version of a famous quote within the Perl community: "You can't just make stuff up and expect the computer to know what you mean." Programming rewards attention to detail.


Dave

Replies are listed 'Best First'.
Re^4: Optional Arguments..?
by Watergun (Novice) on Jun 03, 2012 at 08:28 UTC
    Thanks for the reply again. My lecturer doesn't really helps us with assignments, we are expected to complete it without his help. How do I print the hashes of arrays that I encoded, with or without the specified search string (site name)? I don't know how to tell the computer what I want to convey, I need a little help here :(

      Here's how you go about printing hashes of arrays. This should be general enough that you can adapt it to your situation once you work through the syntax and other errors in your code that are dumped to your screen when you type perl -c scriptname.

      Let's say you have a Hash of Arrays like this:

      my %HOA = ( this => [ 'A', 'B', 'C' ], that => [ 'D', 'E', 'F' ], other => [ 'G', 'H', 'I' ], );

      You could print a given element like this:

      print $hoa{this}[2], "\n"; # prints 'C'.

      ...or you could print each row like this:

      foreach my $key ( keys %HOA ) { print "$key:\t"; print "$_\t" for @{$HOA{$key}}; print "\n"; } # Prints the following (don't rely on the order of hash keys). # this: A B C # that: D E F # other: G H I

      Helpful documentation: perlreftut, perlref, perllol, perldsc.


      Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://974090]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-23 09:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found