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:
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;
The output produced like this:
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..
Re: Splitting multiple patterns by stevieb (Hermit) on Apr 09, 2012 at 04:11 UTC |
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
| [reply] [d/l] [select] |
|
open(INFILE, "<", "infos.txt") or die ("cannot open input: $!");
my @infos = <INFILE>;
chomp(@infos);
How can I split the datas on my @infos? thanks
| [reply] [d/l] |
|
#!/usr/bin/perl
use warnings;
use strict;
open my $fh, "<", "infos.txt" or die "cannot open infos.txt: $!";
my @array = <$fh>;
close $fh;
for my $line ( @array ){
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";
}
| [reply] [d/l] |
|
|
| Re: Splitting multiple patterns by Anonymous Monk on Apr 09, 2012 at 04:13 UTC |
#!/usr/bin/perl --
use strict; use warnings;
use Data::Dump qw/ dd /;
my( $left, $right, $righter ) = split ',', 'Mawts 25,female,melbourne'
+;
my( $name, $age ) = split ' ', $left;
dd [ $left, $right, $righter ];
dd [ $name, $age];
__END__
["Mawts 25", "female", "melbourne"]
["Mawts", 25]
| [reply] [d/l] |
Re: Splitting multiple patterns by exilepanda (Scribe) on Apr 09, 2012 at 08:03 UTC |
if you sure your pattern is correct and like this all the time, you can use this :
my ( $name, $age, $sex, $ad ) = split /[\s\,]+/, $_;
| [reply] [d/l] |
Re: Splitting multiple patterns by GrandFather (Cardinal) on Apr 09, 2012 at 10:46 UTC |
Looks like your input file is a CSV (comma separated variable) file so you should use an appropriate CVS module to dot he heavy lifting for you. Text::CSV is a good starting point. Consider:
use strict;
use warnings;
use Text::CSV;
my $filename = 'infos.txt';
open my $fOut, '>', $filename or die "Can't create $filename: $!";
print $fOut <<TEST;
Mawts 25,female,melbourne
Awts 24,male,sydney
TEST
close $fOut;
my $parser = Text::CSV->new();
open my $fIn, '<', $filename or die "Can't open $filename: $!";
while (my $row = $parser->getline($fIn)) {
my ($name, $age) = $row->[0] =~ /(.*)\s(\d+)/;
print "Name: $name\n";
print "Age: $age\n";
print "Gender: $row->[1]\n";
print "Address: $row->[2]\n\n";
}
close $fIn;
Prints:
Name: Mawts
Age: 25
Gender: female
Address: melbourne
Name: Awts
Age: 24
Gender: male
Address: sydney
True laziness is hard work
| [reply] [d/l] [select] |
Re: Splitting multiple patterns by sundialsvc4 (Monsignor) on Apr 09, 2012 at 15:16 UTC |
- Split multiple times, using multiple statements. This is no time for “golf.” Keep it simple and direct.
- If the problem appears to be one that has already been dealt with by a CPAN module, e.g. Text::CSV, use it in preference to a hand-made solution.
- Get to know the many modules in Regexp::Common. (Do a search ... there are many more than just the ones that the hotlink above will show you.)
| [reply] |
|
| [reply] |
|
Never said they didn’t, now did I? Addressing a particular reply “very specifically” is great of course, for that person who is at that particular moment being addressed. But any thread that is written on this forum is going to last for dozens of years, and every single bloke who encounters it in the future is not going to have “exactly this same problem.” (And, they just might not want to wade through even a paragraph or two of “Perl code.” Or, they might feel about my post exactly the same way you did. Who knows? Certainly not gonna self-censor the thread for worrying about such things.)
I just pointed out a few things that, well do I remember from the time, I didn’t know at the time, and as a consequence, spent more effort on than I really needed to. I would have found my comment useful. (Soapboxing a bit here ...) The real value of Perl is the vast CPAN library that goes with it. But “vast” means that there are a helluva lot of things in there that maybe you had no idea were there. We all have had the experience of writing something from scratch not knowing that something better was at-hand, and maybe benefiting from just a little nudge in an unexpected direction.
I’m never commenting on anything to hear myself talk, and I have a Rhett Butler attitude about “XP.” I have no idea who’s gonna find what useful, nor when nor why. Just gonna do my best.
| [reply] |
|
|
|