help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: mapfile -C issue


From: alex xmb ratchev
Subject: Re: mapfile -C issue
Date: Sun, 18 Jun 2023 16:12:23 +0200

On Sun, Jun 18, 2023, 03:34 Emanuele Torre <torreemanuele6@gmail.com> wrote:

> Note that when you use mapfile with -C it will still store the values
> that are read in the array specified by the argument or MAPFILE, so if
> you want to use mapfile -C to read data, and run a function for every
> line, it is not ideal, it makes more sense to use a while read loop.
>
> Anyway,  -C C -c 3  will call C every 3 lines/records read with the
> current index of the array that will be set, and the current record.
> Note that the callback is called before that record is stored in the
> array.
>
>   C () {
>     declare -p MAPFILE
>     printf '$1 = %s\t$2 = %s\n' "${1@Q}" "${2@Q}"
>   }
>
>   mapfile -td '' -c 3 -C C < <(printf %s\\0 11 22 33 44 55 66 77)
>   # declare -a MAPFILE=([0]="11" [1]="22")
>   # $1 = '2'  $2 = '33'
>   # declare -a MAPFILE=([0]="11" [1]="22" [2]="33" [3]="44" [4]="55")
>   # $1 = '5'  $2 = '66'
>
>   declare -p MAPFILE
>   # declare -a MAPFILE=([0]="11" [1]="22" [2]="33" [3]="44" [4]="55"
> [5]="66" [6]="77")
>
> If you want C to be called for every element, you use -c1, not -c3.
>
>   mapfile -td '' -c 1 -C C < <(printf %s\\0 11 22 33 44 55 66 77)
>   # declare -a MAPFILE=()
>   # $1 = '0'  $2 = '11'
>   # declare -a MAPFILE=([0]="11")
>   # $1 = '1'  $2 = '22'
>   # declare -a MAPFILE=([0]="11" [1]="22")
>   # $1 = '2'  $2 = '33'
>   # declare -a MAPFILE=([0]="11" [1]="22" [2]="33")
>   # $1 = '3'  $2 = '44'
>   # declare -a MAPFILE=([0]="11" [1]="22" [2]="33" [3]="44")
>   # $1 = '4'  $2 = '55'
>   # declare -a MAPFILE=([0]="11" [1]="22" [2]="33" [3]="44" [4]="55")
>   # $1 = '5'  $2 = '66'
>   # declare -a MAPFILE=([0]="11" [1]="22" [2]="33" [3]="44" [4]="55"
> [5]="66")
>   # $1 = '6'  $2 = '77'
>
>   declare -p MAPFILE
>   # declare -a MAPFILE=([0]="11" [1]="22" [2]="33" [3]="44" [4]="55"
> [5]="66" [6]="77")
>
> And -c3 WILL NOT call C every 3 records, before assigning those three
> records passing   key1 value1 key2 value2 key3 value3   to C.
>

i see , didnt get that so

thanks

That is not how it is documented to work.
>
> o/
>  emanuele6
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]