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

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.

Replies are listed 'Best First'.
Re^4: Tutoring for beginner?
by Your Mother (Archbishop) on Mar 06, 2015 at 21:38 UTC
    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.

Re^4: Tutoring for beginner?
by Halbird (Novice) on Mar 06, 2015 at 22:52 UTC
    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.

        Thank you so much! That's more help than I would have expected, much appreicated. I think I'm on the verge of having an epiphany with this, but I've gotten stuck somewhere else. I'll psot an update shortly :)
      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.