Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^3: Help to split

by semenych (Initiate)
on Mar 17, 2013 at 04:28 UTC ( #1023880=note: print w/replies, xml ) Need Help??

in reply to Re^2: Help to split
in thread Help to split

Thanks Athanasius

Replies are listed 'Best First'.
Re^4: Help to split
by Athanasius (Chancellor) on Mar 17, 2013 at 04:39 UTC

    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,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1023880]
[stevieb]: I find the M::S makefiles it generates are quite straight forward, and I usually have to add a few things (github info etc). They're about 15 lines or so give or take.
[Corion]: I don't think the EUMM-generated Makefile is that complicated ;)
[stevieb]: Corion++
[jedikaiti]: RonW++ #I have so much accumulated in my memory that finding the right item at a given moment often fails
[LanX]: corion only > 800 lines
[stevieb]: ahhh you're talking about the actual Makefile, not the PP Makefile.PL
[Corion]: LanX: But most of that is just setup of variables to be used later, like CC , LIB etc.
[LanX]: yes and some builders try to avoid it

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2017-08-18 21:04 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (310 votes). Check out past polls.