Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Help to split

by davido (Archbishop)
on Mar 16, 2013 at 05:38 UTC ( #1023811=note: print w/ replies, xml ) Need Help??


in reply to Help to split

I prefer 2teez's solution; it's the way to do it. But you specified that it should be done with one or more calls to split.

It's usually a better practice when asking for help here to explain what you've tried, and how it's failing, but not to dictate what tools a proper solution should employ. You're likely to get more elegant solutions if you don't limit people such. Nevertheless, if one or more calls to split is the solution you would prefer, you could do something like this:

use strict; use warnings; use feature qw( say ); # Warning: Big slurp! ;) say for map { ( split /\s/, $_, 2 )[0] } # 3: split on whitespace. map { ( split /:/, $_, 5 )[4] } # 2: split on colons. <DATA>; # 1: auto-split on newlines. __DATA__ bsulli03:*:32452:5002:barry sullivan,l230,555-6666,:/students/bsulli03 +:/usr/bin/ksh sbarto22:*:32453:7990:sally bartok,l134,444-3333,:/students/sbarto22:/ +usr/bin/ksh sbarto25:*:32453:7990:sparky bartok,l135,444-4444,:/students/sbarto25: +/usr/bin/ksh sbarto35:*:32453:7990:sammy bartok,l137,444-5555,:/students/sbarto35:/ +usr/bin/ksh sdemar03:*:32454:7990:sally demartra,S23,233-5234,:/students/sdemar03: +/usr/bin/ksh sdemar05:*:32454:7990:bill demartra,l123,555-1234,:/students/sdemar05: +/usr/bin/ksh singl04:*:32455:5002:bart singleton,l134,555-1235,:/students/singl:/us +r/bin/ksh gomez:*:32456:7990:bill gomez,l433,535-6666,:/students/gomez:/usr/bin/ +ksh bolde01:*:32457:7990:bart bolden,l444,444-1234,:/students/bolde:/usr/b +in/ksh reaga09:*:32458:7990:cal reagan,l455,333-1234,:/students/reaga:/usr/bi +n/ksh liu44:*:32460:5002:tom liu,s213,222-1234,:/students/liu:/usr/bin/ksh ulano:*:32462:7990:sally ulanouskay,s233,222-9999,:/students/ulano:/us +r/bin/ksh tran56:*:32463:5002:phuong tran,s111,444-8888,:/students/tran:/usr/bin +/ksh mehr:*:32464:5003:cindy mehr,c113,555-8888,:/students/mehr:/usr/bin/ks +h ramos:*:32449:7990:olivo ramos,l454,999-0000,:/students/ramos:/usr/bin +/ksh brown:*:32459:7990:bart bro,l477,888-7654,:/students/brown:/usr/bin/ks +h mojic:*:32461:7990:celia mojica,c242,555-7654,:/students/mojic:/usr/bi +n/ksh fisher:*:2915:208:cindy fisher,l312,478-0371,:/users/fisher:/usr/bin/k +sh bsulli02:*:32452:5002:bart sullivan,l130,555-5555,:/students/bsulli02: +/usr/bin/ksh

Dave


Comment on Re: Help to split
Download Code
Re^2: Help to split
by Athanasius (Monsignor) on Mar 16, 2013 at 06:19 UTC

    Or you can dispense with map by chaining the calls to split:

    #! perl use strict; use warnings; print +(split / /, (split /,/, (split /:/)[4])[0])[0], "\n" while <DAT +A>;

    Or as a one-liner:

    16:05 >perl -pwe "$_ = (split / /, (split /,/, (split /:/)[4])[0])[0] +. qq[\n];" passwd barry sally sparky sammy sally bill bart bill bart cal tom sally phuong cindy olivo bart celia cindy bart 16:05 >

    :-)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks Athanasius

        You’re welcome!

        Here is the same solution broken into separate steps, and with comments added:

        #! perl use strict; use warnings; my $file = 'passwd'; open(my $fh, '<', $file) or die "Cannot open file '$file' for reading: + $!"; # The while loop reads one line of the data file at a time by implicit +ly # splitting on newlines. So, on the first iteration, $line = # bsulli03:*:32452:5002:barry sullivan,l230,555-6666,:/students/bsulli +03:/usr/bin/ksh while (my $line = <$fh>) { chomp $line; # Remove the trailing newline # Split the line on the colon character. On the first loop iterati +on, # @fields = ('bsulli03', # $fields[0] # '*', # $fields[1] # '32452', # $fields[2] # '5002', # $fields[3] # 'barry sullivan,l230,555-6666,', # $fields[4] # '/students/bsulli03', # $fields[5] # '/usr/bin/ksh'); # $fields[6] # (Note that array indexes start at zero.) my @fields = split /:/, $line; # Now split the fifth field (index 4) on the comma character. On t +he first # loop iteration, @names = ('barry sullivan', 'l230', '555-6666'); my @names = split /,/, $fields[4]; # Now split the first field (index 0) on the space character. On t +he first # loop iteration, @first = ('barry', 'sullivan'); my @first = split / /, $names [0]; # Print the first name, which on the first loop iteration is 'barr +y' print "$first[0]\n"; } close $fh or die "Cannot close file '$file': $!";

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re^2: Help to split
by semenych (Initiate) on Mar 17, 2013 at 04:20 UTC

    Thanks, Dave. But unfortunately I have not learnt map yet. Could you please advice to do it the easiest way, probably, just using one or more split calls. Thanks again

Re^2: Help to split
by semenych (Initiate) on Mar 17, 2013 at 04:27 UTC

    Thanks Dave for your advice. But unfortunately I have not learnt map yet. As I understand I am supposed to do it with while or for loops and using one or more split calls. Could you please help to write it in the easiest way. Thanks again

Re^2: Help to split
by dsheroh (Parson) on Mar 17, 2013 at 09:21 UTC
    It's usually a better practice when asking for help here to explain what you've tried, and how it's failing, but not to dictate what tools a proper solution should employ.
    He may not have explicitly stated it, but this is clearly a homework assignment. The "solve by using split one or more times" requirement is part of the assignment. Even if he knew other ways to do it, the instructor has declared them to be out of bounds.

    Beyond that, he's also provided his source code in the question, which qualifies as "explain[ing] what you've tried" in my book. The only thing missing from the question is a statement of how it fails, although I expect that was obvious to most experienced monks pretty quickly (as CountZero said, he forgot to read from the file after opening it).

Re^2: Help to split
by grizzley (Chaplain) on Mar 18, 2013 at 15:15 UTC
    Pliiiiz, don't use regexps in your split. They are scary and OP doesn't want to be scared :)

      Pliiiiz, don't use regexps in your split. They are scary and OP doesn't want to be scared :)
      by grizzley

      Eeeep! A talking grizzley!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1023811]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-10-25 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (143 votes), past polls