in reply to Re: Tutoring for beginner?
in thread Tutoring for beginner?

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!

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

    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.

      and keep in mind that everybody here started from zero

      Speak for yourself, peasant! I was created, fully-JAPHed, sprung forth directly from Larry’s forehead. Hubris doesn’t grow on trees you know!

      Too much? Hmmm. Well, this is strong coffee. Hate the roaster not the drinker.

      That's very encouraging! Thanks a lot, I'll try to feel less embarrased about posting my code, haha. Something I'm stuggling with is loading data from a file into a list. I tried doing it like this:
      open(FILEA, "<", "Population.txt") or die("Cannot open: $!\n"); my @Data; @Data = <FILEA>;
      I think that's ok? So the list should be working with the text in the file? I'm trying to split each line in the list into two components... Would that be something like:
      $Data[0] = "$PopA"; $Data[1] = "$PopB";
      As I said, I'm a complete beginner so any hints would be greatly appreciated!

        Yes, the code for reading the file is fine. People often use lexical filehandles these days, e.g.

        open my $filea, "<", "Population.txt" or die "Cannot open: $!\n"; my @Data = <$filea>;

        The reason for this is that "regular" filehandles are global -- in a short script it doesn't make a difference, but I believe it's best to get used to good style right away. (Also, short, throwaway scripts have a tendency to become longer and longer-lasting when you least expect it.)

        So the list should be working with the text in the file? I'm trying to split each line in the list into two components.

        For that you'll want to iterate over the lines, i.e. your @Data array, and split each individual line according to some rule. For iterating, you can use e.g. for or foreach (they're synonyms), like so:

        # ... foreach my $line (@Data) { # do something with $line }

        Depending on how large your input file is and how much of it needs to be kept in memory at the same time for processing, you may be better of not reading the entire file at once, and instead using a while loop to read it line by line:

        open my $filea, "<", "Population.txt" or die "Cannot open: $!\n"; while(<$filea>) { # do something with $_ }

        $_, BTW, is a special variable that acts as the default argument for many built-in functions; it's called the topic, and it's indeed pretty much that, the thing that is currently being talked about (i.e. processed). The angle bracket operator, when used in a while loop, assigns to $_ as well, so the above is a common idiom for processing a file line by line.

        How you'd go about splitting a line into two components depends on where and how you want to split it. split is a useful function if you want to split on e.g. whitespace. If you can share some sample data and post a description of how you want it processed, we can help you further.

        open(FILEA, "<", "Population.txt") or die("Cannot open: $!\n"); my @Data; @Data = <FILEA>;
        I highly recommend not to indent like this; instead, use an editor that automatically indents code for you. I heard that Notepad++ works well on Windows.
Re^3: Tutoring for beginner?
by Anonymous Monk on Mar 06, 2015 at 20:13 UTC

    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!

Re^3: Tutoring for beginner?
by james28909 (Deacon) on Mar 07, 2015 at 01:25 UTC
    "do complete noobies come here often?"
    YES!! i am here.
      That makes me feel much better! I've been skimming over some posts in the forum and I've found it very encouraging... Hopefully we won't be noobies for much longer :)
        It is a very good community, they have helped me alot with the things i were learning and people around here are always willing to help and with a good attitude. Best thing is, to jump right in and start putting some code together :)