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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array and print

Status
Not open for further replies.

pani765

Programmer
Aug 14, 2003
12
US
Hello,

I am trying to pick up Array's in perl. I have a simple function, open a text file that has multiple line and 4 field. I am pushing the file into an array then printing the value of $state from each line into another file. When I tried this my Print line keeps giving me error.



Thanks For all ur help.
DATAFILE:
MI,Detroit,CASS,Jack
CO,Denver,DPS,Linda


CODE:
open (USERUPDATELIST, $usrfile) || &open_error($usrfile);
while (<USERUPDATELIST>) {
($state,$district,$school,$username) = split (/,/, $, 4);
push( @UpdateUSRList, join(&quot;,&quot; ($state,$district,$school,$username)));
}
close (USERUPDATELIST);


for my $i (0..$#UpdateUSRList) {
open (MYPAGEDATA, &quot;>$mypage&quot;) || &open_error($mypage);
print (MYPAGEDATA &quot;$UpdateUSRList[$i][$state]&quot;);
close (MYPAGEDATA);

}
 
The only thing you want to do is get the name of the states in a file right? Do you need @UpdateUSRList for any other reason further in you program, because if you don't then you don't need it just to get the name of the states.
Yep, your print statement is definitely in error. ok here is the code to do your task:

open (MYPAGEDATA, $mypage) || &open_error($mypage);
open (USERUPDATELIST, $usrfile) || &open_error($usrfile);
while (<USERUPDATELIST>) {
chomp;
($state,$district,$school,$username) = split (/,/, $_);
print MYPAGEDATA $state, &quot;\n&quot;;
}
close (USERUPDATELIST);
close (MYPAGEDATA);
 
And, just for your edification let's pick apart your original code to see what is wrong with it.

open (USERUPDATELIST, $usrfile) || &open_error($usrfile); #good
while (<USERUPDATELIST>) { #good
($state,$district,$school,$username) = split (/,/, $, 4); #good
push( @UpdateUSRList, join(&quot;,&quot; ($state,$district,$school,$username))); #see note 1
}
close (USERUPDATELIST);


for my $i (0..$#UpdateUSRList) { #good
open (MYPAGEDATA, &quot;>$mypage&quot;) || &open_error($mypage); #good
print (MYPAGEDATA &quot;$UpdateUSRList[$i][$state]&quot;); #see note 2
close (MYPAGEDATA);

}


Note 1) You are rejoinging the string that you just split and placing it into an array. It is a simple array with one line from the file per element. It would look like this:

$UpdateUSRList[0] = MI,Detroit,CASS,Jack
$UpdateUSRList[1] = CO,Denver,DPS,Linda

If your code had looked like this:

open (USERUPDATELIST, $usrfile) || &open_error($usrfile);
@UpdateUSRList = <USERUPDATELIST>;

and had skipped the while loop and the splitting, you would have accomplished the exact same thing. In essence, you are slurping the entire file into memory and storing it in @UpdateUSRList. That is a terribly inefficient process and should be avoided if possible.

Note 2)You are trying to call a multidimensional array that does not exist - hence the error. To get the state out of the @UpdateUSRList array, you would have to loop through each element of the array, split the string into parts, and then print the portion you want.

Mactonio provided a perfect solution and I hope you learned where you went wrong from my explanation.

Cheers.
 
Another error in the original code:

for my $i (0..$#UpdateUSRList) {
open (MYPAGEDATA, &quot;>$mypage&quot;) || &open_error($mypage);
print (MYPAGEDATA &quot;$UpdateUSRList[$i][$state]&quot;);
close (MYPAGEDATA);
}

You're opening and closing MYPAGEDATA on each pass through the for loop. Not what you want to do. Open
the file once before the loop and close it once
after the loop. Opening an existing file with &quot;>&quot;
clobbers the file, so you'd never have anything but the
last line of output in it, assuming everything else was
correct.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top