if/else formatting in fmt file

Started by crforcrq, June 12, 2024, 09:29:21 PM

Previous topic - Next topic

crforcrq

I am trying to create a format file which will output a waypoint with lat/log = gpsdestlatitude/gpsdestlongitude, if present and if not present use gpslatitude/gpslongitude.

I tried something like this
#[BODY] <wpt lat="${gpsdestlatitude#;($gpsdestlatitude)?$gpsdestlatitude:$gpslatitude}"  lon="$gpslongitude#">

#[BODY] <wpt lat="${gpsdestlatitude#;$_= (($gpsdestlatitude)?$gpsdestlatitude:$gpslatitude)}"  lon="$gpslongitude#">

#[BODY] <wpt lat="${gpslatitude#; my ($lat)=$gpsdestlatitude#;if (defined $lat) $_= $lat }"  lon="$gpslongitude#">
but this gives me an error
Global symbol "$gpslatitude" requires explicit package name (did you forget to declare "my $gpslatitude"?) for 'gpsdestlatitude' - and lat is set = ""

These do not error but gpsdestlatitude is not used
#[IF]  ${gpslatitude;$_ = $gpsdestlatitude if  not defined $gpsdestlatitude} } ${gpslongitude}
#[BODY] <wpt lat="$gpslatitude#"  lon="$gpslongitude#">

If I switch the logic and test for gpsdestlatitude then only files with the tag gpsdestlatitude is processed..

Wondering if there is any way- to use the fmt file, and use conditionals within that to get a custom gpx?

Or does this kind of processing require a full script? Thanks

Phil Harvey

You can't access other tags directly from inside an advanced formatting expression.  Instead use GetValue() like this:

#[BODY] <wpt lat="${gpslatitude#; my $lat=$self->GetValue('GPSDestLatitude','ValueConv');$_ = $lat if defined $lat}"  lon="${gpslongitude#; my $lon=$self->GetValue('GPSDestLongitude','ValueConv');$_ = $lon if defined $lon}">
I don't have time to test this, but it should work.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

crforcrq

This is awesome.  I can make this work. Thanks so much.