Gossamer Forum
Home : General : Perl Programming :

split on array

Quote Reply
split on array
Hi

I have brought a flat file database into an array using delimeter |, this has been reduced down from numerous records, but there is now only one element in the array

I want to split it further into the 23 fields so that I can add to certain field, split will not work, it is giving me a numerical output.

if I convert to a scalar I can then use split this does give me my 23 fields, however the output has striped the | and replaced it with a space.

Is there something that I am missing

thanks

DavyC
http://www.amijet.com
Life's more fun in a virtual world
Quote Reply
Re: split on array In reply to
Um I didn't really understand what you were saying but....

open(DB,"<db.db") || die "Can't open db:$!";
@db=<DB>;
close(DB);

foreach $line (@db) {
split(/\|/,$line);
}

That splits each line using the pipe as the delimiter

You could do....

foreach $line (@db) {
($f1,$f2,$f3...$f23) = split(/\|/,$line);
}

That would put the values into $f1 up to $f23....where I used ($f1,$f2,$f3...$f23), you will actually have to enter all of them.

Im sure theres an easier way. Hmmmmmmm.......

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: split on array In reply to
my @data = split /\|/, $line;
$data[0] .. $data[22] is where the stuff would be stored..

Jerry Su
widgetz sucks
Quote Reply
Re: split on array In reply to
Thanks Paul and Jerrysu, will give it a go


many thanks

DavyC
http://www.amijet.com
Life's more fun in a virtual world
Quote Reply
Re: split on array In reply to
Sorry still cannot get this to work here is a section of my code

open (PILOTS, "pilots.db");
@pilots = <PILOTS>;
close (PILOTS);

@pilots = grep /$pilotid/, @pilots; #$pilotsid should give one line of db

print "<p>we found the following record <b>@pilots</b>"; #used for testing

foreach $line (@pilots) {
split(/\|/,$line);
}

$no =@pilots; #just used for testing

print "<p>There $no elements in the arrray";


here is the result that I get after a use grep

72|AJ001|David|Campbell|24-Mar-2001|email@address.com|England|FS2000|00|Pilot|00|Pilot|00|Pilot|00|Pilot|00|Pilot|48|Pilot|BAV``Midland|48|Virtual life is a lot less complicated

This is the record that I wish to modify, but I must be able to refer to

$pilots[8] and so on to add in hours etc.

I have tried the different bits of code but I still get only 1 element in the array. Converting to a hash would make life even easier but I would not even attempt that yet.


DavyC
http://www.amijet.com
Life's more fun in a virtual world
Quote Reply
Re: split on array In reply to
In Reply To:
Converting to a hash would make life even easier but I would not even attempt that yet.
It's not as hard as you think...
Code:
my %data;
open (PILOTS, "pilots.db") or die "Reason: $!";
while () {
my ($line, $id, %rec);
chomp ($line = <PILOTS>);
last if !$line;
($id, $rec{this}, $rec{that}) = split /\|/, $line;
$data{$id} = { %rec };
}
close (PILOTS);
if (exists $data{$pilotid}) {
my $ref = $data{$pilotid};
print "$ref->{this}\t$ref->{that}\n";
} else {
print "Record $pilotid not found.\n";
}
Happy Coding,

--Drew
http://www.FindingHim.com
Quote Reply
Re: split on array In reply to
I'm still having a very hard time following what you are trying to do.

Please give an example of multiple entries in your flat file, show which line you are trying to extract, and then show what you are trying to extract from the line that matched your criteria.

From what I'm gathering, you're matching and pulling out the line

72|AJ001|David|Campbell|24-Mar-2001|email@address.com|England|FS2000|00|Pilot|00|Pilot|00|Pilot|00|Pilot|00|Pilot|48|Pilot|BAV``Midland|48|Virtual life is a lot less complicated

And then want to split that on the pipe and have each piece in an array. Is that correct?

If you could post the details as outlined above, I'm sure we could get you a quick solution.

--mark

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: split on array In reply to
Hi Mark

Yes you seem to have got what I am looking to do exactly.

Full picture
============


I am using dbman to store pilot records for a virtual airline,

This database will hopefully grow in size as each new pilot joins. Each pilot record will hold 23 fields. Name email address and various other data. I am currently using test records, but I have posted an example at

http://www.amijet.com/test.txt

used a text file in order that you could read it, normally pilots.db.

Field 8 contains the amount of hours that the pilot has flown in a category A aircraft, field 10 hours in a category B aircraft, 5 categories in total. These hours are then totaled into another field.

When a pilot makes a flight they complete a form on the site which stores the information into a temporary database, with 9 fields, I refer to the relevant hours field in this database by @temp[3], I have this already worked out and in my script.

This database also contains their pilot id number in my examples it is AJ001 .. AJ002 and so on.

In the main data base, I firstly must identify their record using their pilotid. Then add the hours to the relevant category hours total, field 8, 10, 12 or whatever, this is not a problem either.

I am opening the main database placing it all into an array

I use grep to find the line with the pilot id

my array now only contains one element the line or record for the pilot

as in your previous post example

I then need to be able to refer to various fields in this (you are again correct the | (pipe) is the delimeter.

This is the stage were I am having problems, I cannot seem to get this to split as it is an array with one element, which I need to split into 23 elements using |.

Sorry it is very obvious I am not explaining this well, but Marks seems to have understood me in the previous post.

Thanks everybody for all your help it is appreciated, I am trying to learn perl from books, and can usually get there in the end but this one is really confusing me (probably simple too)

thanks



DavyC
http://www.amijet.com
Life's more fun in a virtual world
Quote Reply
Re: split on array In reply to
http://www.amijet.com/test.txt returns an custom 404-message. (Oeps.....Amijet is currently under construction, it would appear that you have clicked on a link)

Just wanted to let you know ;-)

Quote Reply
Re: split on array In reply to
Sorry - renamed it pilots.txt forgot to make it test.txt, fixed now, very sorry my blunder

DavyC
http://www.amijet.com
Life's more fun in a virtual world