D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

[LUG] perl question - how to sort arrays of hashes?

 


Posted by "dave morgan" <morgadave@xxxxxxxxxxxxxx>


I am trying to load EXIF data from jpegs into some sort of array, and I want it to 
end up sorted by timestamp, but I can't get the sort to work right.

Any suggestions on how to sort, or if I need a different data structure?

more background in the Man page at http://www.morgad.co.uk/csv2kml-man.html


my current code (excerpts) -

# find all jpeg files in current directory
my $file;
my $filetype;
my @jpeg;
$numjpegs = 0;
my ($mtime, $csv_timestamp, $exifTool);

my $mm = new File::MMagic;
opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
foreach $file ( readdir(DIR) ) {
        $filetype = $mm->checktype_filename("$file");   
        if ($filetype eq "image/jpeg") {
                $numjpegs++;
                $jpeg[$numjpegs]{file} = $file;
                $exifTool = new Image::ExifTool;
                $exifTool->ExtractInfo($file);

                $csv_timestamp = $exifTool->GetValue('CreateDate');
                unless($csv_timestamp) {
                        $csv_timestamp = $exifTool->GetValue('FileModifyDate');
                }
                
                $mtime = &to_seconds($csv_timestamp);
                
                $jpeg[$numjpegs]{mtime}= $mtime;
                $jpeg[$numjpegs]{lat}  = 
parse_location($exifTool->GetValue('GPSLatitude'));
                $jpeg[$numjpegs]{long} = 
parse_location($exifTool->GetValue('GPSLongitude'));

        }
}
closedir(DIR);

# unsorted
foreach $count (1 .. $numjpegs) {
        print "$count - $jpeg[$count]{file} - $jpeg[$count]{mtime}\n";
        }

# sorted
my @sorted;
## //FIXME// what to put here??
@sorted = sort { $jpeg[$a]{mtime} <=> $jpeg[$b]{mtime} };

foreach $count (1 .. $numjpegs) {
        print "$count - $sorted[$count]{file} - $sorted[$count]{mtime}\n";
        }


best regards
Dave
--
++++++++++++++++++++++++++++++++

Generated by the contributors page 
in the members area at D+C GLUG
http://www.dcglug.org.uk/members/list.php
webmaster@xxxxxxxxxxxxx


-- 
The Mailing List for the Devon & Cornwall LUG
http://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/linux_adm/list-faq.html