CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Sorting a hash (cont...)

 



Wiliam
Novice

Aug 15, 2000, 12:33 AM

Post #1 of 4 (440 views)
Sorting a hash (cont...) Can't Post

Hi Folks,

Thankyou for all your help over the last couple of days. I do really appreciate it!

But, I'm afraid I am still stuck in the mud.

I was just wondering... how would you go about doing what I'm trying to do and getting it to work??

What I have is a number of files in a directory. My search script works through each of these files and if a word within a file matches that of the search criteria then I need to mark that file as a result.

I then need to enter another routine where the script opens up each file in the results array or hash or whatever and prints out the details of that file. However I need the files to be printed in alphabetical order. The order will be determined from the information within the individual files.

I have had no luck sorting these so far. Does anyone have any last thoughts before this monitor gets thrown out of my window?

Rgds,

Wiliam.


mrossi
Deleted

Aug 15, 2000, 3:58 AM

Post #2 of 4 (440 views)
Re: Sorting a hash (cont...) [In reply to] Can't Post

Heh, I've been struggling with a similar problem myself this afternoon! Found some help in Perlfaq4 under Data:Arrays/How do I sort an array by (anything). Here's a working example:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@data = qw(2;c 1;b 3;a);
@idx = ();
for (@data) {
($item) = /.;(.)/;
push @idx, ($item);
}
@sorted = @data[ sort { $idx[$a] cmp $idx[$b] } 0 .. $#idx ];
print "This is a normal sort:\n", sort @data;
print "\nThe same, sorted by second column:\n", @sorted;
</pre><HR></BLOCKQUOTE>

This will work for you, too. Just populate an array with filename-keyword pairs, delimited with (a) suitable character(s), and modify the regex on line 4 accordingly.

If someone could explain how this works, I'd be happy. I can't understand how $a and $b can be used as references for a seemingly unrelated array. Another example in Perlfunc is even more confusing, where $a and $b seem to be used both as references and string variables at the same time.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@new = sort {($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0]} @old;
</pre><HR></BLOCKQUOTE>

Any explanations gratefully received.


[This message has been edited by mrossi (edited 08-15-2000).]


Wiliam
Novice

Aug 16, 2000, 12:10 AM

Post #3 of 4 (440 views)
Re: Sorting a hash (cont...) [In reply to] Can't Post

Thankyou for your reply.

Any idea how I can push my two variables into an array then? I have the first which is $dat_id and the second $e_dat_name. Any ideas how to push these onto an array and then modify the code accordingly?

Rgds,

Wil.


mrossi
Deleted

Aug 16, 2000, 1:01 AM

Post #4 of 4 (440 views)
Re: Sorting a hash (cont...) [In reply to] Can't Post

I'm sure the experienced folks have a dozen shortcuts but hey, it's a free world and TMTOWTDI.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#2 arrays with same number of elements
#push contents into third array
@a = qw(one two three four five six seven);
@b = qw(1 2 3 4 5 6 7);
do {
push @c, "$a[$n];$b[$n]";
$n++;
}
while $n < $#a;
foreach (@c)
{
print "$_\n";
}
</pre><HR></BLOCKQUOTE>

Or:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#2 arrays with same number of elements
#concatenate second array stuff into first
@a = qw(one two three four five six seven);
@b = qw(1 2 3 4 5 6 7);
do {
$a[$n] .= ";$b[$n]";
$n++;
}
while $n < $#a;
foreach (@a)
{
print "$_\n";
}
</pre><HR></BLOCKQUOTE>

These work with the previous example as long as the the arrays don't contain semicolons.

Have a nice day.

P.S.
Forgot about the regex. You need to figure out a regular expression that mathces the part in each array element that you want it sorted by: /firstpart;(whatyouwant)/ . I can't help with it without knowing the dataset. Strongly recommend reading up on regular expressions if you haven't done so yet. See O'Reilly's Camel and Llama books. The online manual that comes with ActivePerl is great too.

[This message has been edited by mrossi (edited 08-16-2000).]

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives