I've got to open & read a space-delimited text file and assign various chunks of it to variables..
I'm having trouble with my logic, thinking I should use an array at one point for data with the same "names".
If I have the following (space-delimited) text file:
--------------------------------------
Name Bob Johnson and some more extraneous text and some more extraneous text
Name Susie Johnson and some more extraneous text and some more extraneous text
Address 1313 Mockingbird Lane and some more extraneous text
City Munsterville and some more extraneous text and some more extraneous text
State Alabama and some more extraneous text and some more extraneous text
Zip 12345 and some more extraneous text
---------------------------------------
Using the following script I can extract things that there are *only one of* like address, state, zip. It's the "Names" that I'm thinking should go into an array
so I can do something like:
$Name1 = Array[0];
$Name 2 = Array[1];
(I don't really need to get too fancy with a counter, etc. since I know I'll never have more than 4 names or so per text file, etc.)
open (FNMA, $FNMAFileName) or warn ("unable to open: text file.\nReason: $!");
while (<FNMA>) {
@FNMAbig = <FNMA>;
#match data to corresponding line and find FNMA value
foreach $FNMAline (@FNMAbig) {
##for multiple matches put into an array
if ($FNMAline =~ /^Name/) {
@VariableToExtract = substr($FNMAline,4,20);
print $VariableToExtract[0];
} #end if match
} #end foreach loop
} #end while loop
close (FNMA);
The "print $VariableToExtract[0];"
Gives me both variables: Bob Johnson Sue Johnson (or)
Bob Johnson
Sue Johnson
(depending on whether I split/strip out returns)
Logically I'm thinking it should only give me "Bob Johnson"... any guidance?
I'm having trouble with my logic, thinking I should use an array at one point for data with the same "names".
If I have the following (space-delimited) text file:
--------------------------------------
Name Bob Johnson and some more extraneous text and some more extraneous text
Name Susie Johnson and some more extraneous text and some more extraneous text
Address 1313 Mockingbird Lane and some more extraneous text
City Munsterville and some more extraneous text and some more extraneous text
State Alabama and some more extraneous text and some more extraneous text
Zip 12345 and some more extraneous text
---------------------------------------
Using the following script I can extract things that there are *only one of* like address, state, zip. It's the "Names" that I'm thinking should go into an array
so I can do something like:
$Name1 = Array[0];
$Name 2 = Array[1];
(I don't really need to get too fancy with a counter, etc. since I know I'll never have more than 4 names or so per text file, etc.)
Code:
$FNMAFileName = "12635.fnm"; open (FNMA, $FNMAFileName) or warn ("unable to open: text file.\nReason: $!");
while (<FNMA>) {
@FNMAbig = <FNMA>;
#match data to corresponding line and find FNMA value
foreach $FNMAline (@FNMAbig) {
##for multiple matches put into an array
if ($FNMAline =~ /^Name/) {
@VariableToExtract = substr($FNMAline,4,20);
print $VariableToExtract[0];
} #end if match
} #end foreach loop
} #end while loop
close (FNMA);
The "print $VariableToExtract[0];"
Gives me both variables: Bob Johnson Sue Johnson (or)
Bob Johnson
Sue Johnson
(depending on whether I split/strip out returns)
Logically I'm thinking it should only give me "Bob Johnson"... any guidance?