Gossamer Forum
Home : General : Perl Programming :

Need help with Array

Quote Reply
Need help with Array
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.)

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?
Quote Reply
Re: [Watts] Need help with Array In reply to
Probably easier, would be what format you want it in? i.e should it be:

I'm not really sure what output you are trying to achieve (and the format of your data looks a bit weird, as you have 2 "Names", and then also address,city,state and zip fields ... but only one of them)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Need help with Array In reply to
It's text file that is space delimited. Each line has an identifier at the beginning.
I can extract the lines (and the text I need to assign to variables) that are "unique" (ie, only one occurance - such address, city, state, zip).

Where I'm getting lost is on how to handle the lines with multiple occurances (such as "name") which is
why I was thinking it'd be best to do a "foreach loop" and make an array
and do $name1 = @foo[0], $name2 = @foo[1], etc.
Quote Reply
Re: [Watts] Need help with Array In reply to
Not sure if this is what you're aiming for - but give this a go:

Code:
#!/usr/local/bin/perl

use CGI::Carp qw(fatalsToBrowser);
print qq|Content-Type: text/html \n\n|;
#use Data::Dumper;

my @data = split /\n/, q|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|;

my $data; # a hashref, so we can store it all
my $name;
my @names;
foreach (@data) {

if (/^Name /) {
my @split = split / /, $_;
$name = "$split[1] $split[2]";
push @names, "$split[1] $split[2]";
} else {
my @split = split / /, $_;
my @return;
for (my $i = 1; $i <= split; $i++) {
push @return, $split[$i];
}
$data->{$name}->{$split[0]} = join(" " , @return);
}

}

#use Data::Dumper;
#print Dumper($data);

foreach (@names) {

print qq|Name: $_ <br />
|;
my $vals = $data->{$_};
map {
print qq|$_ ===> $vals->{$_} <Br />|;
} keys %$vals;

print qq|<hr>|;

}

This is how it comes out for me:

http://gossamerlinks.com/cgi-bin/test.cgi

Seems to work ok Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Need help with Array In reply to
SmileSmileSmile

Great! I think I can take it from here.. ( I just never know at what point I need to split/push, etc.)
As long as I have a working example I can usually "reverse engineer" it.. lol..

I'm going to study this section and see what's happening and why.

Many many thanks!
Quote Reply
Re: [Watts] Need help with Array In reply to
haha np - glad its what your looking for Angelic

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!