![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| perl question | convenientstore | Shell Programming and Scripting | 7 | 05-21-2007 08:59 AM |
| Calling a perl script from a perl script | new2ss | Shell Programming and Scripting | 3 | 02-06-2007 07:17 PM |
| Perl: Run perl script in the current process | vino | Shell Programming and Scripting | 10 | 12-09-2005 06:45 AM |
| Question about Perl | SolidSnake | Shell Programming and Scripting | 5 | 06-10-2003 06:20 AM |
| PERL question | frank | Shell Programming and Scripting | 1 | 06-18-2002 12:13 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
question on perl script
LOGFILE
======== My name is: ?Anthony Perkins I am an American citizen. My name is: ?Donte Suarez I am a Spanish citizen. My name is: ? lenny Davis I am an Australian citizen. My name is: ?allen rigodeau I am a French citizen. My name is: ? manuel williams I am a Mexican citizen. OUTPUT ======= First Name - ANTHONY Last Name - PERKINS First Name - DONTE Last Name - SUAREZ First Name - LENNY Last Name - DAVIS First Name - ALLEN Last Name - RIGODEAU First Name - MANUEL Last Name - WILLIAMS >>>>>>>>>> SCRIPT ======= if ( $currline =~ m/My name is\: \?/ ) { @record = split(/\?/, $currline); $message = "$record[1]"; @record2 = split(/ /, $message); $firstname= "$record2[0]"; $lastname = "$record2[1]"; print OUTPUT "First Name -".$firstname; print OUTPUT "Last Name -".$lastname; } >>>>>>>>>> For the above logfile, my script would be working properly only on Anthony Perkins and allen rigodeau. But this no longer works if there's a space after the question mark, or when there's more than one space between the first and last name. Appreciate your advice. Thanks. |
| Forum Sponsor | ||
|
|
|
|||
|
Check out split - perldoc.perl.org. You're splitting the array by spaces, which means if there's more than one space, your array's all off. Try this:
Code:
use Data::Dumper;
if ( $currline =~ m/My name is\: \?/ )
{
@record = split(/\?/, $currline);
$message = "$record[1]";
@record2 = split(/ /, $message);
print Dumper(@record2);
$firstname= "$record2[0]";
$lastname = "$record2[1]";
print OUTPUT "First Name -".$firstname;
print OUTPUT "Last Name -".$lastname;
}
Reading a bit more, and we can see that what we want is: Code:
if ( $currline =~ m/My name is\: \?/ )
{
@record = split(/\?/, $currline);
$message = "$record[1]";
@record2 = split(' ', $message);
$firstname= "$record2[0]";
$lastname = "$record2[1]";
print OUTPUT "First Name -".$firstname;
print OUTPUT "Last Name -".$lastname;
}
Code:
if ( $currline =~ m/My name is\: \?\s*(\S+)\s+(\S+)/ )
{
print OUTPUT "First Name -".$1;
print OUTPUT "Last Name -".$2;
}
|
| Thread Tools | |
| Display Modes | |
|
|