[librecat-dev] string operations, defining functions in Catmandu

Patrick Hochstenbach Patrick.Hochstenbach at UGent.be
Thu Jun 29 13:28:32 CEST 2017


Hi,

There is no straightforward way currently to add a string to the last subfield of a marc_field. I created an issue for this:
https://github.com/LibreCat/Catmandu-MARC/issues/74

But while waiting for this new feature you can use a dirty trick to get the required result. The Fix script below will add the 100 subfields to the 400 field and adds a
period “.” when required:

$ cat marc.fix
marc_map(100a,f100a)
marc_map(100q,f100q)
marc_map(100b,f100b)
marc_map(100c,f100c)
marc_map(100d,f100d)

if marc_match(100aqbcd,"\.$")
    # Do nothing
    marc_add(400
        ,a ,$.f100a
        ,q ,$.f100q
        ,b ,$.f100b
        ,c ,$.f100c
        ,d ,$.f100d)
else
    marc_add(400
        ,a ,$.f100a
        ,q ,$.f100q
        ,b ,$.f100b
        ,c ,$.f100c
        ,d ,$.f100d)
    append(record.$last.$last,".")
end
$ catmandu convert MARC to MARC —fix marc.fix < camel.mrc

The trick is the append(record.$last.$last,".”) line which knows that MARC is modeled as an array of arrays in Catmandu and adds a period to the last element.

If you want to use parameter on the command line to control the mapping (e.g. to decide at runtime what the source and target MARC fields should be), then you can use the Fix preprocessor:

$ cat test2.fix
marc_map({{F}}a,f{{F}}a)
marc_map({{F}}q,f{{F}}q)
marc_map({{F}}b,f{{F}}b)
marc_map({{F}}c,f{{F}}c)
marc_map({{F}}d,f{{F}}d)

if marc_match({{F}}aqbcd,"\.$")
    # Do nothing
    marc_add({{V}}
        ,a ,$.f100a
        ,q ,$.f100q
        ,b ,$.f100b
        ,c ,$.f100c
        ,d ,$.f100d)
else
    marc_add({{V}}
        ,a ,$.f{{F}}a
        ,q ,$.f{{F}}q
        ,b ,$.f{{F}}b
        ,c ,$.f{{F}}c
        ,d ,$.f{{F}}d)
    append(record.$last.$last,".")
end

$ $ catmandu convert —pp —var F=100 —var V=400 MARC to MARC —fix marc.fix < camel.mrc

I hope this helps

Patrick

> On 28 Jun 2017, at 18:41, Uldis Bojars <captsolo at gmail.com> wrote:
> 
> Hi,
> 
> Could you suggest how to do this in Catmandu:
> 
> I need to form a new MARC field ("400") that consist of some subfields (in this order: $a $q $b $c $d) of an existing field ("100"). That much is not difficult, but there's another condition:
> 
> "." must be added to the last subfield that exists (e.g. if $d exists append "." to it unless it already ends with ".", otherwise do the same with $c, [...]) .
> 
> Questions:
> 
> 1) how to determine if a field (e.g. fld_100d created via marc_map) ends with "." ?
> 
> 2) is there a way to "package" some functionality and call it when needed?
> 
> In this case it would be good to create a function that appends "." to the field provided as a parameter unless it already ends with ".".
> 
> Then what is needed could be achieved like this:
> 
> if exists("fld_100d"):
>    my_function("fld_100d")
> elsif exists("fld_100c"):
>    my_function("fld_100c")
> elsif [...]
>    [...]
> end
> 
> P.S. Perhaps there is a more idiomatic way how to do this in Catmandu?
> 
> Thanks,
> Uldis
> 
> _______________________________________________
> librecat-dev mailing list
> - send list mails to librecat-dev at lists.uni-bielefeld.de
> - to unsubscribe or change options, visit https://lists.uni-bielefeld.de/mailman2/cgi/unibi/listinfo/librecat-dev
> - project website: http://librecat.org/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: Message signed with OpenPGP
URL: <http://lists.uni-bielefeld.de/mailman2/unibi/public/librecat-dev/attachments/20170629/1d5f93e6/attachment.asc>


More information about the librecat-dev mailing list