• ArcaneSlime@lemmy.dbzer0.com
    link
    fedilink
    English
    arrow-up
    5
    ·
    1 year ago

    I’m still struggling with understanding for loops tbh. I kinda get them, but I can’t “make my own” so I don’t really understand them.

    I’ve never had any schooling coding, just made some scripts with internet help when I switched to linux, and I have ADHD like fuck so I haven’t really tried to understand them in months, but yeah if anyone knows of a good website to help learn for loops and how to create them (when to use what variables and brackets and shit, etc) I’m taking recomendations.

    For i in website do $(tell me please);

    (That can’t be right lol, see, I need help!)

    • coloredgrayscale@programming.dev
      link
      fedilink
      English
      arrow-up
      5
      ·
      1 year ago

      Don’t let yourself down because you don’t know the syntax off the top of your head.

      Even after 15 years of programming, and studying computer science, I would have to look up how to write loops, conditions, variable assignments in bash / sh / batch.

      Coming to python from a primarily java focus background wasn’t any different. I knew what steps the program should do, but had to look up how to translate it into whatever language. And for further improvements what features the language has to express the things “in the style of the language”

      • ArcaneSlime@lemmy.dbzer0.com
        link
        fedilink
        English
        arrow-up
        3
        ·
        1 year ago

        Thanks, that encouragement is definitely helpful, it felt like I was struggling with something most programmers would consider should be mastered day 1, right after lunch because hello world is before lunch haha. Glad to know people still have to look it up even after a while sometimes.

    • Regular Human@lemmy.world
      link
      fedilink
      English
      arrow-up
      5
      arrow-down
      1
      ·
      1 year ago

      For loops in scripting languages like bash should be a punishment. Try out something like python

    • joyjoy@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      1 year ago

      Is your issue about just syntax?

      for part in $text; do
        echo "xX${part}Xx"
      done
      

      In bash, this loops over each word in a variable. If you want each line, you’ll need to use a while read loop instead.

      while read -r line; do
        echo "xX${line}Xx"
      done <<< "$text"
      
      • ArcaneSlime@lemmy.dbzer0.com
        link
        fedilink
        English
        arrow-up
        2
        ·
        1 year ago

        I think my issue may be more than just syntax, I really am inexperienced lol, all just learning as I go (unix philosophy and all lol, I kid).

        The for loop I stole from the internet for use with ffmpeg is

        for i in *$input; do ffmpeg -i "$i" "${i%.*}$output"; done

        So I know what it does, it takes the input (read by the script earlier) filetype and changes it to the output filetype also read earlier for all of the files of $input type in the current directory, and I know how I got input and output as variables, and I know the ffmpeg -i foo -o bar command, but I get completely lost on "$i" "${i%.*}$output";. I don’t really understand when to use what brackets or where I need semicolons and why, though I do understand that $ calls a variable and * is an operator to designate “all,” I’m not entirely sure what this part of my script is doing (as this loop is the part I copied from stackexchange, and only half understood it “but it worked so fuck it” lol.)

        • joyjoy@lemmy.worldOP
          link
          fedilink
          English
          arrow-up
          3
          ·
          1 year ago

          The ${} syntax manipulates a variable. In this instance, I believe % removes a suffix. # is for a prefix. I can never remember which is which.

          Semicolons just separate statements. You can replace them with a new line to get the same effect.