Forum Discussion

Walter_WorkAcct's avatar
Walter_WorkAcct
Icon for Altostratus rankAltostratus
Mar 15, 2022
Solved

Assistance with Concatenation

I am trying to use the following code to convert html links to those with PDFs as the target. The issue I have is with concatenation and the correct syntax. The code below gives a trailing close bracket on the URL:

HTTP::respond 301 Location [concat "https://[HTTP::host]/archive"[string map -nocase {".html" ".pdf"} [HTTP::uri]]]

 

  • You don't really need to use the concat command here, you can just order your strings inline like this:

    HTTP::respond 301 Location "https://[HTTP::host]/archive/[string map -nocase {.html .pdf} [HTTP::uri]]"

    You can also test this Tcl syntax with placeholders in the tcl shell:

    % set host www.example.com
    www.example.com
    % set uri1 matching-uri.html
    matching-uri.html
    % set uri2 unmatching-uri.htm
    unmatching-uri.htm
    % set uri3 unmatching-uri.pdf
    unmatching-uri.pdf
    % puts “https://$host/archive/[string map -nocase {.html .pdf} $uri1]”
    “https://www.example.com/archive/matching-uri.pdf”
    % puts “https://$host/archive/[string map -nocase {.html .pdf} $uri2]”
    “https://www.example.com/archive/unmatching-uri.htm”
    % puts “https://$host/archive/[string map -nocase {.html .pdf} $uri3]”
    “https://www.example.com/archive/unmatching-uri.pdf”

     

1 Reply

  • You don't really need to use the concat command here, you can just order your strings inline like this:

    HTTP::respond 301 Location "https://[HTTP::host]/archive/[string map -nocase {.html .pdf} [HTTP::uri]]"

    You can also test this Tcl syntax with placeholders in the tcl shell:

    % set host www.example.com
    www.example.com
    % set uri1 matching-uri.html
    matching-uri.html
    % set uri2 unmatching-uri.htm
    unmatching-uri.htm
    % set uri3 unmatching-uri.pdf
    unmatching-uri.pdf
    % puts “https://$host/archive/[string map -nocase {.html .pdf} $uri1]”
    “https://www.example.com/archive/matching-uri.pdf”
    % puts “https://$host/archive/[string map -nocase {.html .pdf} $uri2]”
    “https://www.example.com/archive/unmatching-uri.htm”
    % puts “https://$host/archive/[string map -nocase {.html .pdf} $uri3]”
    “https://www.example.com/archive/unmatching-uri.pdf”