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

adding specific line number to an array

Status
Not open for further replies.

bretttt

Programmer
Jul 23, 2002
74
US
hello! i have an array...@userff It contains text files from a directory, now i need to foreach $_ push @userii

line 32 from $_

Any help appreciated greatly, THANKS!

I cant seem how to push only line 32 from each file.
 
Using $file as the name of the file, open up a filehandle for each text file. Then you can loop through to the 32nd line and grab it. Then close the file and open the next one. Try this:

foreach $file (@userff)
{
open (IN, $file) or die "Can't open $file.";
for ($i = 1; $i <= 32; $i++)
{
$line = <IN>;
}
# $line now = line 32
push $line @userii;
close IN;
}

I can't think of a more efficient way, but there may be one. --Derek

&quot;Fear not the storm for this is where we grow strong.&quot;
 
i guess the following code will work
foreach $file(@userff)
{
open(FILEHANDLE,$file) || die &quot;cann't open the $file!&quot;;
@filearray=<FILEHANDLE>;
close(FILEHANDLE);
$i=0;
$line32;
LINE:
foreach $line(@filearray)
{

$line32=$line;
#here $line32 takes each time a new line
$i++;
last LINE if($i==32);
#when the 32 line comes it exit the inner foreach loop and the $line32 contains the value of the 32 line in the text file and you can push it into the @array
}
@userii=push(@userii,$line32);
}
 
You can push just one element (line) from an array like this.

push @arrayii, $arrayff[31];

That pushes the 32nd element (arrays are numbered from zero, remember) onto the top of @arrayii.

Is that what you meant? Mike
________________________________________________________________

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
each time the value of $line32 is replaced by the value of $line.As soon as the $i==31 (its $i==31 instead of $i==32)we are coming out of inner foreach loop and before that $line32 is assigned the value of $line and we are pushing the value in to the array @userii..

the outer foreach loop takes the next file and does the same.
with the foreach outer loop we can push 32nd line of all the files in to the @userii
 
Using the $. variable, which keeps track of line numbers you could write;

while(<FILE>) {

last if $. == 32;

}

push(@array, $_);

I have a funny feeling that you might have to explicitly set $. to 1 before you open the next file.
 
Using the $. variable, which keeps track of line numbers you could write;

while(<FILE>) {

last if $. == 32;

}

push(@array, $_);

I have a funny feeling that you might have to explicitly set $. to 1 before you open the next file.

See the perlvar manpage for details on $. .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top