in reply to Splitting multiple patterns
You could split the line the first time at the space, assigning the name as the first side of the whitespace and put the rest into another variable, then split the $rest variable on the commas:
#!/usr/bin/perl use warnings; use strict; open my $fh, "<", "infos.txt" or die "cannot open infos.txt: $!"; while ( my $line = <$fh> ){ chomp $line; my ( $name, $rest ) = split /\s+/, $line; my ( $age, $gender, $address ) = split /,/, $rest; print "Name: $name\n"; print "Age: $age\n"; print "Gender: $gender\n"; print "Address: $address\n"; print "\n"; }
Output:
Name: Mawts Age: 25 Gender: female Address: melbourne Name: Awts Age: 24 Gender: male Address: sydney
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Splitting multiple patterns
by astronogun (Sexton) on Apr 09, 2012 at 04:35 UTC | |
by stevieb (Canon) on Apr 09, 2012 at 04:48 UTC | |
by astronogun (Sexton) on Apr 09, 2012 at 10:30 UTC | |
by Marshall (Canon) on Apr 09, 2012 at 19:07 UTC | |
by astronogun (Sexton) on Apr 10, 2012 at 09:35 UTC |