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

Perl arrays

Status
Not open for further replies.

Oxymoron

Technical User
Joined
Dec 17, 2000
Messages
168
Location
GB
hi, I'm trying to get elements from an array (after a certain point) into another array.
However, the array simply takes seemingly random or incorrect elements of the array.

--------------------------code----------------------
#!/usr/bin/perl


print "enter letter to compare to file: ";
chomp($letter = <stdin>); print&quot;\n&quot;;
open (SORT, &quot;<C:/sort.dat&quot;);
@array = <SORT>;
close(SORT);
$pointer = 0;
open (SORT, &quot;>>C:/sort.dat&quot;);
until ($letter le $array[$pointer]) {
$pointer++;
if ($letter gt $array[$#array]) {
print SORT $letter;
}
}
print &quot;letter will be inserted in between \n----$array[$pointer -1] and \n----$array[$pointer]&quot;;
close(SORT);
print &quot;pointer = $pointer\n\n&quot;;
$x = 0;
print &quot;x = $x\n\n&quot;;
until ($pointer == $#array) {
$temp[$x] = $array[$pointer];
$pointer++;
$x++;
print &quot;$pointer element of array = $array[$pointer] \n&quot;;
print &quot;$x element of temp = $array[$x] \n&quot;;
}
print &quot;\n&quot;;
print&quot;array = $#array, temp = $#temp \n&quot;;
-----------------------------------------------------------
if ANYONE can help i'd b really gr8ful!
thanks!
JoE

we are all of us living in the gutter.
But some of us are looking at the stars.
 
Are you trying to compare the letter to the first letter of each word in sort.dat, or are you trying to compare the input letter to each line of sort.dat letter by letter? What does the data in sort.dat look like? (please just post a few sample lines, not the whole file, and a short example of how a letter would fit into the output)

jaa
 
the file 'sort.dat' is not a data file ( i just like putting .dat extensions to files i use in programs).
---------------sort.dat------------------------
a
b
d
e
f
-----------------etc--------------------------

I'm trying to compare a letter enetered against letters in the file.
The print statements in the 'Until' loop output summit like this if i enter an 'i':
---------------output------------------------
C:\perls>perl sort.pl
enter letter to compare to file: i

letter will be inserted in between
----h
and
----i
pointer = 7

x = 0

8 element of array = j

1 element of temp = b

9 element of array = k

2 element of temp = d


array = 9, temp = 1

C:\perls>
------------end of code-----------------
what i want is for the array; temp, to contain the elements in 'array' from $pointer onwards.

if anyone can help i'd b really grateful!!
JoE we are all of us living in the gutter.
But some of us are looking at the stars.
 
&quot;sort.dat&quot; is a file that contains data that your program opens, reads, and manipulates. But it's not a data file. Okaaaaay.

May I ask what your eventual goal here is? What you are going to do with this code once you get it working? ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
Why dont' you just splice the new letter into place?

splice(@array,$pointer,0,$letter);

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top