astronogun has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks
I would like to know how to split multiple patterns or expressions?
For example I have an input file from infos.txt file and I want to split the data there per line
The infos.txt file have the following input:
"Mawts 25,female,melbourne
Awts 24,male,sydney"
I want to split the <Tab> or space between the name and the age, and also split the "commas" (,)
Here's my code but I'm getting different result:The output produced like this:open(INFILE, "<", "infos.txt") or die ("cannot open input: $!"); while (<INFILE>){ chomp; $name; $age; $gender; $address; ($name) = split(" "); print "Name: $name\n"; ($age, $gender, $address) = split(","); print "Age: $age\n"; print "Gender: $gender\n"; print "Address: $address\n"; } exit 0;
Name: Mawts 25,female,melbourne
Age: Mawts 25
Gender: female
Address: melbourne
Name: Awts 24,male,sydney
Age: Awts 24
Gender: male
Address: sydney
On the result only the "Name" and "Age" has the wrong output but the "Gender" and "Address" is correct. I know there's something wrong with the code but can't figure it out, I tried combining the splitting patterns but gave me blank results only the commas was seen in the result. Hope you could help guys thanks very much..
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Splitting multiple patterns
by stevieb (Canon) on Apr 09, 2012 at 04:11 UTC | |
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 | |
| |
|
Re: Splitting multiple patterns
by GrandFather (Saint) on Apr 09, 2012 at 10:46 UTC | |
|
Re: Splitting multiple patterns
by exilepanda (Friar) on Apr 09, 2012 at 08:03 UTC | |
|
Re: Splitting multiple patterns
by Anonymous Monk on Apr 09, 2012 at 04:13 UTC | |
|
Re: Splitting multiple patterns
by locked_user sundialsvc4 (Abbot) on Apr 09, 2012 at 15:16 UTC | |
by Anonymous Monk on Apr 09, 2012 at 15:30 UTC | |
by locked_user sundialsvc4 (Abbot) on Apr 09, 2012 at 18:26 UTC | |
by Anonymous Monk on May 02, 2012 at 08:50 UTC |