Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

working with databases

Status
Not open for further replies.

mike101

Programmer
Jul 20, 2001
169
US
Hey all.
I have my script open a database. It grabs the information from a database with the script below.

open (ORGDB,&quot;<$mainfile2&quot;);
@ODB=<ORGDB>;
close (ORGDB);

foreach $rec (@ODB){
chomp($rec);
($tid,$tinformation)=split(/\|/,$rec);
if ($id eq $tid) {
$information = $tinformation;
}
}

the database looks like this:

1|Player,POS,G,AB,R,H,2B,3B,HR,RBI,TB,BB,SO,SB,CS,OBP,SLG,AVG
2|Hello,test,what,is,up

if $id is 1 then $information becomes Player,POS,G,AB,R,H,2B,3B,HR,RBI,TB,BB,SO,SB,CS,OBP,SLG,AVG

now I need to know how to split each of them and make it so $1 = Player
$2 = POS
$3 = G
etc.

but will do it for each one. The thing is though is if say $id is 2 then there are only 5 so there would just be $1 - $5 and for id 1 it would be $1 - $18. I need the script to keep making it like that. Next, I need another script that will count how many there are and make a variable $total be what that number is for example id 1 would make 18. Thanks.
 
Maybe I'm missing something but it seems to me that you could just call split() again to get the other variable list.

I don't recommend using $1 $2 etc because they have a special meaning in Perl, but you could do something like:
[tt]
open (ORGDB,&quot;<$mainfile2&quot;);
@ODB=<ORGDB>;
close (ORGDB);

foreach $rec (@ODB){
chomp($rec);
($tid,$tinformation)=split(/\|/,$rec);
if ($id eq $tid) {
@info_array = split(/,/,$tinformation);
}
}
[/tt]

this would fill the array @info_array with the values you want and you can get at the individual values in that array like this:
[tt]
$first_element = $info_array[0];
$second_element = $info_array[1];
# etc...
[/tt]

Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
ok i see what you are doing. The problem is I need the part you have that says

$first_element = $info_array[0];
$second_element = $info_array[1];
# etc...

to be a for statement cause one line might have only 3 things in the array and another might have 1000. So i mean i need it so it will count how many commas there are and then make

$a1 = $info_array[0];
$a2 = $info_array[1];
etc.

i added the a to fix that $1, $2 thing you said. but I need it to do it like i said with the for command and to ALSO make variable $total equal the amount of things in the array. What I mean by that is above we have $a1 and $a2 so that is 2 things so $total would be 2. Thanks for your help. I will copy this and email it to you also.
 
@info_array = (POS,G,AB,R,H,2B);
@a;

for (0..$#info_array) {
$a[$_] = $info_array[$_];
}

In this way, you don't have to know beforehand how many elements are in @info_array, if i understood you correctly.

pearl2
 
Ok thanks. I see that will make $a1, $a2 etc. automatically. The next thing I need is to have it so after it makes these it makes a variable telling me how many elements are in @info_array cause I need that for something else later on in the script. Thanks.
 
Pls ignore my earlier solution - it's actually quite silly because it does nothing but duplicates the array @info_array.

To do what you want to do, here is a revised solution:

@array = qw(a b c d);
# $array_len gives you the length of @array
$array_len = @array;

for (0..$#array) {
$a = &quot;\$a&quot;;
$a .= $_;
# this prints $a0, $a1, $a2, $a3
print &quot;$a\n&quot;;
}

print &quot;$array_len\n&quot;;

Hope that helps :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top