in reply to Help with split() function

This question has been asked before: Pattern matching.

Out of curiosity, what book are you using? It seems to be a poor one (based on the number of times these questions have been asked).

-driver8

PS: I also posted about this in your old thread.

Replies are listed 'Best First'.
Re^2: Help with split() function
by negzero7 (Sexton) on Mar 19, 2008 at 01:17 UTC

    I'm using Beginning Perl. It's a good book, just regular expression pattern matching is a whole new concept to me. I read through most of that stuff, but I don't feel like I gained any insight into this problem. What am I doing wrong that it's printing out the wrong stuff?

      I don't want to sound too harsh here, but you are missing many of the basics of Perl. You are using split(), arrays, regular expression matching, and "if" statements wrong. The only parts you have right are the ones you took from answers to your previous questions. You need to actually read your book and get familiar with the perl documentation if you want to Learn Perl. So far I can't see that you've learned anything but how to copy and paste.

      I'll try to point you in the right direction anyways, though. Get one thing working at a time, and don't move on to the next thing until everything before it is doing what you want. I can tell you that "my @line = split ();" is not doing what you want it to. Read the perldoc for the split function.


      -driver8
        I disagree. My other code was fine and worked for the most part. Others gave advice and said what I have was a more correct way, so I have improved to use that. I don't consider that "copy and pasting".

        Could you please elaborate on why certain things are wrong? Telling me they are wrong and throwing me some documentation is only confusing me more. I can obviously tell it's wrong since it's not working.

        Maybe you could explain why it's out putting what it is?

Re^2: Help with split() function
by negzero7 (Sexton) on Mar 19, 2008 at 01:36 UTC

    I've edited it down to this

    #!usr/bin/env perl use warnings; use strict; open my $test_fh, '<', @ARGV or die "Can not open file $!\n"; while (<$test_fh>) { my @line = split (); # if ($line[1] =~ /P/i); print "$line[1]"; } close ($test_fh);

    I'm still getting the same thing. I think my split() needs something but I have no idea what. Any help?

      You have several problems here. The main one is that you decided to use split for no obvious reason. Yes it can be done that way, but why would you want to?

      The simplest would be just to use a regular expression to find a 'p' in the line, all you need is: print if /p/i; No split is required.

      Where split could be used is split '' which breaks up each character into a seperate list element. You could then test each one in a loop. This method is occasionally useful, but only for specialised character-by-character operations, not for a simple task like this. You also seem unclear as to what accessing an array is doing. I suggest you read the section on arrays before you try to use them.

      Keep plugging with the book, and keep writing Perl.
      Well, here's what i would do, and presuming i can still read the output is tested and correct:

      #!/usr/bin/perl -w use strict; while (<DATA>) { my @line = split (); foreach my $word (@line) { if ($word =~ /P/i) { print "$word\n"; } } } __DATA__ CPAN stands for comprehensive Perl Archive Network. ^ and $ are used as anchors in a regular expression. /pattern/ is a pattern match operator. Perl is very easy to learn. Enter 'H' or 'h' for help.

      Why?

      Your use of split is fine. But you need to loop thru your array:
    • line 6: if it doesn't make sense, figure it out
    • line 7: ditto
    • line 8: ditto