Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Using Split function

by catfish1116 (Beadle)
on Nov 27, 2019 at 15:41 UTC ( #11109324=perlquestion: print w/replies, xml ) Need Help??

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

Trying to create a program that is based off of an example in 'Learning Perl', (page 275). I would be taking multiple lines from a file and selecting certain fields, splitting on the ':'. I'm sort of stuck here, any input would be greatly appreciated. Below is the beginning of my code. TIA The Catfish

my $line = <>; while (defined($line = <>)) { chomp($line); my @items = split /:/ $line; # print "Customer Data: $line\n"; }

Replies are listed 'Best First'.
Re: Using Split function
by haukex (Chancellor) on Nov 27, 2019 at 15:47 UTC

    When you look at the documentation of split, you'll see that the regular expression is followed by a comma before the next argument.

Re: Using Split function
by AnomalousMonk (Bishop) on Nov 27, 2019 at 16:30 UTC
    my @items = split /:/ $line;

    Assuming the quoted statement is made syntactically correct as haukex suggests, if you print or dump (see the Data::Dumper core module) the  @items array with
        print @items;
        use Data::Dumper;
        print Dumper \@items;
    do you see what you expect?

    If the  @items array contains what you expect, how would you access a specific element of the array (which would correspond to selecting a field of the original record)? How would you select multiple fields? Might it be more convenient to use an array slice?

    Give a man a fish:  <%-{-{-{-<

Re: Using Split function
by perldigious (Priest) on Nov 27, 2019 at 16:43 UTC

    You have a simple syntax error in your call of the built in split function.

    use strict; use warnings; my $line = "This is :a line :with colons :in it"; my @items = split /:/, $line; print "$_\n" foreach (@items);

    If you are working through Learning Perl, Chapter 9 explains some basic usage of split.

    If you are like me and forget syntax easily, try to remember that things like split and print are built in functions of Perl and as such should be treated like any subroutine call where you wrote the subroutine (just no "&" ever necessary, see Chapter 4 of Learning Perl :-)). You can even wrap parenthesis around the list (list implies comma separators) of arguments you are passing them for clarity... e.g.

    my @items = split(/:/, $line); print('just ', 'like ', 'this ', ':-)')

    EDIT: Also, if you are only looking to extract certain items of your list for each line, and you know their index locations in the array you split them in to, you may want to take a look at array slices which are covered in Chapter 17 of Learning Perl.

    Just another Perl hooker - My clients appreciate that I keep my code clean but my comments dirty.
Re: Using Split function
by BillKSmith (Prior) on Nov 27, 2019 at 16:46 UTC
    You can eliminate most of your code by using the Command Switches -F which implies -a and -n.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11109324]
Approved by tobyink
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2019-12-05 23:12 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (154 votes). Check out past polls.