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

Hi! I'm looking for someone to give me some advice on a peice of programming I am doing, I'm willing to tip for tutoring (not sure if this is allowed). This is beginner work so nothing demanding, Message me if interested!

Replies are listed 'Best First'.
Re: Tutoring for beginner?
by AppleFritter (Vicar) on Mar 06, 2015 at 19:42 UTC

    Welcome to the Monastery!

    Just post whatever problem or piece of code you need advice on, and the monks will generally be happy to help, especially if your code shows that you invested a modicum of effort into solving your problem. We won't do your homework for you, as it were, but we'll try and help you get it done yourself.

    Tipping's not required, BTW, though you're welcome to put a few coins on the Offering Plate if you'd like to help support the Monastery.

      Thank you! I'm a bit apprehensive to post my code (what I have so far) publically - it's very simple compared to everything else I've seen on this site. Do complete newbies come here often? Perhaps I'm in the wrong place!

        Do complete newbies come here often? Perhaps I'm in the wrong place!

        They sure do! Complete newbie or seasoned veteran, as long as you're interested in learning Perl, you're in exactly the right place. Don't be shy, just post away -- and keep in mind that everybody here started from zero. Even the most exalted monks were not born with their wisdom.

        Don't worry! We get to see all sorts of code here and programmers at all levels. As long as you are willing to put in the effort to learn it does not matter at all what your code looks like!

        "do complete noobies come here often?"
        YES!! i am here.
Re: Tutoring for beginner?
by Halbird (Novice) on Mar 07, 2015 at 16:16 UTC
        Try
        @data{@speciesList} = split(/\t/, $_, scalar @speciesList); # ^ ^ remove "
        poj
          Brilliant, that worked - thanks a lot! I can't wrap my head around how that made a difference, though.

        Programs often start out small and evolve into more complex beasts. Not unlike species.

        First, to make it easier for others to run, here is my test file, Mammal.txt, with each field separated by a single TAB character:

        Thylacine 42 147 Quagga 34 18 Baiji 40 116

        Running my program below on this input file produces the following output:

        Name : Thylacine Latitude : 42 Longitude : 147 Name : Quagga Latitude : 34 Longitude : 18 Name : Baiji Latitude : 40 Longitude : 116
        which has essentially the same data as yours but with minor (cosmetic) differences in formatting.

        I show below how I would have written your program. I hope that you find it useful and that it may serve as motivation for you for further study to improve your Perl skills.

        # Always start your scripts with strict and warnings. use strict; use warnings; # With "use strict" you must declare all variables before use # (this catches errors when you misspell a variable name, for example) +. # So we put a "my" in front of @speciesList and others below to declar +e # them as lexical variables, which have a scope from the point of # declaration to end of scope (i.e. end of block or end of file). my @speciesList = qw(Name Latitude Longitude); # Put "Mammal.txt" into a variable to avoid having to repeat it. my $infile = "Mammal.txt"; # BTW, instead of hard-wiring $infile to "Mammal.txt" above # you could now accept it as a command line argument like so: # my $infile = shift or die "usage: $0 filename\n"; # Use three-argument open and a lexical file handle. # Also check if the open fails and die with a useful message if so. open my $MAMMALS, "<", $infile or die "error: open '$infile': $!"; # Use explicit lexical variables rather than $_ while (my $line = <$MAMMALS>) { my %data; @data{@speciesList} = split /\t/, $line, scalar @speciesList; foreach my $species (@speciesList) { printf "%-10.10s : %s\n", $species, $data{$species}; } } close $MAMMALS;

        That is a first cut only.

        You might like to think further about your input file format. For example, if you add more species properties (in addition to Latitude and Longitude) how easy would it be to adjust your program? Is the input file format convenient? I would at least think about a different input file format with named properties (so you can easily add new ones without affecting existing properties), and with fields separated by any white space rather than a TAB char (for convenience and to allow vertical whitespace alignment when editing), and with a comment character to allow you to comment out lines. For example:

        # This is a species file Thylacine Lat=42 Long=147 Quagga Lat=34 Long=18 Baiji Lat=40 Long=116
        Of course, if you did that, your code would need to change in step ... which might be a useful training exercise. It would be good training for you to write the code to read the species properties file as a subroutine, taking a file name as input and returning an appropriate data structure of species properties as output.