bmclellan
asked on
Printing elements of an array within an array
Here is some sample code that I am stumped with:
#!/usr/bin/perl
my($line1) = "line 1 from a text file";
my($line2) = "line 2 from a text file";
my($line3) = "a NON unique line that needs to be sorted later";
my($line4) = "line 4 from a text file";
my(@array1) = ($line1, $line2, $line4);
my(@array2) = (
[$line3, @array1],
);
Basically what I would like to do is:
sort array based on first dimension
then print the contents of array1 that is stored within array2
I have read about using a hash table, except that the first element in array2 can be a duplicate, and from what I have read hash table keys have to be unique.
Thanks for you help
Barry
#!/usr/bin/perl
my($line1) = "line 1 from a text file";
my($line2) = "line 2 from a text file";
my($line3) = "a NON unique line that needs to be sorted later";
my($line4) = "line 4 from a text file";
my(@array1) = ($line1, $line2, $line4);
my(@array2) = (
[$line3, @array1],
);
Basically what I would like to do is:
sort array based on first dimension
then print the contents of array1 that is stored within array2
I have read about using a hash table, except that the first element in array2 can be a duplicate, and from what I have read hash table keys have to be unique.
Thanks for you help
Barry
Could you please provide sample input and corresponding output, please...
It sounds to me as if you don't realise how easy it is to sort in Perl.
Simply use the sort command. Type the command
perldoc -f sort
for details (or see http://www.perl.com/pub/doc/manual/html/pod/perlfunc/sort.html)
In your example, if you want the 4 lines sorted, you simply say
my(@sortedArray) = sort ($line1, $line2, $line3, $line4);
Using a _hash_ to sort eliminates duplicates, so it is not suitable in this application.
Simply use the sort command. Type the command
perldoc -f sort
for details (or see http://www.perl.com/pub/doc/manual/html/pod/perlfunc/sort.html)
In your example, if you want the 4 lines sorted, you simply say
my(@sortedArray) = sort ($line1, $line2, $line3, $line4);
Using a _hash_ to sort eliminates duplicates, so it is not suitable in this application.
If you want to sort in some way other than alphanumeric ordering based on the entire contents of each element, then you must stipulate your rules in a subroutine, as described in perldoc.
For example:
my(@sortedArray) = sort mySpecialWay ($line1, $line2, $line3, $line4);
sub mySpecialWay {
..... # Your code here
}
For example:
my(@sortedArray) = sort mySpecialWay ($line1, $line2, $line3, $line4);
sub mySpecialWay {
..... # Your code here
}
If you want an answer to the question that is implied by the subject of your post, i.e. "How do I print elements of @array1 within @array2 in the example below?", the answer is
$i = 1;
foreach (@array1) {
print "$array2[0][$i++]\n";
}
$i = 1;
foreach (@array1) {
print "$array2[0][$i++]\n";
}
bmclellan ,
please elaborate on your question. instead of giving code snippets. i would suggest that you give this forum the raw input and the desired output.
That way one can clearly visualize your requirements.
Thanks
please elaborate on your question. instead of giving code snippets. i would suggest that you give this forum the raw input and the desired output.
That way one can clearly visualize your requirements.
Thanks
Your sample code seems to run without error. What are you stumped with?
Are you asking this?
perldoc -q "How do I compute the difference of two arrays"
Are you asking this?
perldoc -q "How do I compute the difference of two arrays"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, that's the answer I was looking for! Once I spend more time with this language and get out of "rookie" status, I will try to articulate my questions better instead of handing you all a riddle to solve on top of the actual question I am trying to ask.
Barry
Barry