Solved

Printing elements of an array within an array

Posted on 2000-03-09
8
180 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:bmclellan
8 Comments
 
LVL 3

Expert Comment

by:monas
ID: 2602478
Could you please provide sample input and corresponding output, please...
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2602696
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.
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2602713
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
}
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Expert Comment

by:PC_User321
ID: 2602780
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";
}
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2602883
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 2603376
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"
0
 
LVL 1

Accepted Solution

by:
mrachow earned 100 total points
ID: 2603785
Maybe you (bmclellan) want the contained array "moved" when sorting by the non unique lines?!
#!/usr/local/bin/perl -w

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,@array2,@arrSorted);
my($nIdx);

{
my @arrTmp = ($line1, $line2, $line4);
push @array2, (["Z: ".$line3, \@arrTmp]);
}{
my @arrTmp = ($line2, $line4, $line1);
push @array2, (["A: ".$line3, \@arrTmp]);
}{
my @arrTmp = ($line4, $line2, $line1);
push @array2, (["Y: ".$line3, \@arrTmp]);
}{
my @arrTmp = ($line2, $line2, $line1);
push @array2, (["X: ".$line3, \@arrTmp]);
}{
my @arrTmp = ($line4, $line4, $line4);
push @array2, (["Y: ".$line3, \@arrTmp]);
}

@arrSorted = sort ownSort (@array2);

for($nIdx=0;$nIdx<5;$nIdx++)
{
  print  "$arrSorted[$nIdx][0]","\n";
  print  "@{$arrSorted[$nIdx][1]}","\n";
}

sub ownSort
{
  return ${$a}[0] cmp ${$b}[0];
}
 __END__
 
0
 

Author Comment

by:bmclellan
ID: 2604528
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
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question