![]() |
|
Welcome to the UndergroundScene Forums forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Senior Member
Join Date: Feb 2003
Location: Dundee
Posts: 2,537
![]() |
perl programming(quite similar to java)
I'm currently doing perl programming as part of my chemistry course and so far its been pretty similar to java so maybe someone can help me with this.
How can you return/print the number of an array, not the actual value thats in the array but the array numbers itself(eg 0, 1,2, 3, 4 etc) Its just a little task we've been given but its bugging me that i can't do it |
|
|
|
|
|
#4 (permalink) |
|
Hardcore is serious guys
Join Date: Mar 2003
Location: Dundee
Posts: 5,996 Band: Blasphemous Necrorapist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
I don't know perl, but what exactly is it that you want to do? Your question isn't really clear.
"The number of an array" doesn't really make sense. Your explanation after that doesn't really clarify things either. It sounds like you might be talking about an associative array, or "hash table", rather than an array, and you want to print a list of keys rather than values? I'm assuming that, because it doesn't really make sense to talk about a regular array that way. If you know the length of a normal array, then you know what all the indices are. Bear in mind I don't know perl, there could be errors in this because I just looked at some examples on the net and tried to change them without really knowing the syntax all that well. Code:
%capitals = ('china' => 'beijing', 'england' => 'london', 'france' => 'paris', 'norway' => 'oslo', 'italy' => 'rome');
# process hash elements
foreach $k (keys (%capitals))
{
print $k, " ";
}
Last edited by humndislocation : 26th January 2006 at 12:10 AM. |
|
|
|
|
|
#6 (permalink) |
|
Senior Member
Join Date: Feb 2003
Location: Dundee
Posts: 2,537
![]() |
@titrants=(175,149,122,126,150,169);
$min = $arr[0]; $array = 0; foreach $t (@titrants) { if ($t < $min) { $min = $t; } print "Minium is $min\n"; I got that so far, but to the print statement i need to add the arraynumber becuase im using the following set of data Titrant Ohms 0 175 1 148 2 122 3 126 4 150 5 169 The question is write a program to find the minium point on the graph and which point it is |
|
|
|
|
|
#7 (permalink) | |
|
Hardcore is serious guys
Join Date: Mar 2003
Location: Dundee
Posts: 5,996 Band: Blasphemous Necrorapist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Quote:
|
|
|
|
|
|
|
#8 (permalink) |
|
Hardcore is serious guys
Join Date: Mar 2003
Location: Dundee
Posts: 5,996 Band: Blasphemous Necrorapist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Ahh right, I see what you want to do now. This isn't really so much of a perl question, as it is a generic programming question (unless there's a specific way to do this in perl).
You need two more variables. One of them holds the current index you are looking at, one of them holds the index of the current min value. You update the current index each time you go through the loop, and you update the current min index each time you change the min. Sorry, that sounds a bit more complex than it really is. Here's what I'm talking about Code:
@titrants=(175,149,122,126,150,169);
$min = $titrants[0];
$minIndex = 0;
$currentIndex = 0;
foreach $t (@titrants)
{
if ($t < $min)
{
$min = $t;
$minIndex = $currentIndex;
}
$currentIndex = $currentIndex + 1;
}
|
|
|
|
|
|
#10 (permalink) |
|
Senior Member
Join Date: Apr 2002
Location: Dundee/Alyth hybrid
Posts: 1,502 Band: 15 Minutes
![]() ![]() ![]() ![]() ![]() ![]() |
You could actually be really fly and only keep a record of the index of the lowest element the just print the that index and the value at that index
Code:
@titrants=(175,149,122,126,150,169);
$min = $titrants[0];
$minIndex = 0;
$currentIndex = 0;
foreach $t (@titrants)
{
if ($t < $min)
{
$minIndex = $currentIndex;
}
$currentIndex = $currentIndex + 1;
}
print "Minium is $titrants[$minIndex ] at position $minIndex";
|
|
|
|
|
|
#11 (permalink) |
|
Hardcore is serious guys
Join Date: Mar 2003
Location: Dundee
Posts: 5,996 Band: Blasphemous Necrorapist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Yeah, that's actually a hell of a lot smarter than my solution.
I must say though, Perl has to be up there as one of the ugliest non-joke languages I have ever seen. Give me Python or Lua any day. |
|
|
|
|
|
#12 (permalink) | |
|
Senior Member
Join Date: Apr 2002
Location: Dundee/Alyth hybrid
Posts: 1,502 Band: 15 Minutes
![]() ![]() ![]() ![]() ![]() ![]() |
Quote:
My favourite at the moment (mostly beause I'm working with it most of the time) is PL/SQL, based on ADA if I remember correctly, it makes life very easy with type anchoring and extensions to SQL. |
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|