while-loop

What's the difference between iterating over a file with foreach or while in Perl?

ぃ、小莉子 提交于 2019-12-17 09:23:10
问题 I have a filehandle FILE in Perl, and I want to iterate over all the lines in the file. Is there a difference between the following? while (<FILE>) { # do something } and foreach (<FILE>) { # do something } 回答1: For most purposes, you probably won't notice a difference. However, foreach reads each line into a list (not an array) before going through it line by line, whereas while reads one line at a time. As foreach will use more memory and require processing time upfront, it is generally

What's the difference between iterating over a file with foreach or while in Perl?

荒凉一梦 提交于 2019-12-17 09:23:01
问题 I have a filehandle FILE in Perl, and I want to iterate over all the lines in the file. Is there a difference between the following? while (<FILE>) { # do something } and foreach (<FILE>) { # do something } 回答1: For most purposes, you probably won't notice a difference. However, foreach reads each line into a list (not an array) before going through it line by line, whereas while reads one line at a time. As foreach will use more memory and require processing time upfront, it is generally

Reading key/value parameters from a file into a shell script

落爺英雄遲暮 提交于 2019-12-17 07:55:48
问题 I've got my script working almost. The goal of this is to take defined values from a file and then populate these valeus in a shell script at runtime. Please take a look at what i have here... The first file: ab.sh #!/bin/bash USER_TYPE=$1 #IDENTIFY USER TYPE TYPE1,TYPE2,TYPE3,TYPE4 USERNAME=$2 PERMISSION_TYPE=$3 #IDENTIFY PERMISSION TYPE SELECT,UPDATE,DELETE,INSERT TARGET_USER=$4 TARGET_TABLE=$5 if [ $USER_TYPE == 'TYPE1' ] then cat Parameters.conf |while read USERNAME do sqlplus / as sysdba

Reading key/value parameters from a file into a shell script

徘徊边缘 提交于 2019-12-17 07:55:17
问题 I've got my script working almost. The goal of this is to take defined values from a file and then populate these valeus in a shell script at runtime. Please take a look at what i have here... The first file: ab.sh #!/bin/bash USER_TYPE=$1 #IDENTIFY USER TYPE TYPE1,TYPE2,TYPE3,TYPE4 USERNAME=$2 PERMISSION_TYPE=$3 #IDENTIFY PERMISSION TYPE SELECT,UPDATE,DELETE,INSERT TARGET_USER=$4 TARGET_TABLE=$5 if [ $USER_TYPE == 'TYPE1' ] then cat Parameters.conf |while read USERNAME do sqlplus / as sysdba

Bash: How to end infinite loop with any key pressed?

*爱你&永不变心* 提交于 2019-12-17 07:19:56
问题 I need to write an infinite loop that stops when any key is pressed. Unfortunately this one loops only when a key is pressed. Ideas please? #!/bin/bash count=0 while : ; do # dummy action echo -n "$a " let "a+=1" # detect any key press read -n 1 keypress echo $keypress done echo "Thanks for using this script." exit 0 回答1: You need to put the standard input in non-blocking mode. Here is an example that works: #!/bin/bash if [ -t 0 ]; then SAVED_STTY="`stty --save`" stty -echo -icanon -icrnl

Bash: How to end infinite loop with any key pressed?

穿精又带淫゛_ 提交于 2019-12-17 07:19:30
问题 I need to write an infinite loop that stops when any key is pressed. Unfortunately this one loops only when a key is pressed. Ideas please? #!/bin/bash count=0 while : ; do # dummy action echo -n "$a " let "a+=1" # detect any key press read -n 1 keypress echo $keypress done echo "Thanks for using this script." exit 0 回答1: You need to put the standard input in non-blocking mode. Here is an example that works: #!/bin/bash if [ -t 0 ]; then SAVED_STTY="`stty --save`" stty -echo -icanon -icrnl

Why piping input to “read” only works when fed into “while read …” construct? [duplicate]

风格不统一 提交于 2019-12-17 04:45:42
问题 This question already has answers here : Read values into a shell variable from a pipe (15 answers) Closed 5 months ago . I've been trying to read input into environment variables from program output like this: echo first second | read A B ; echo $A-$B And the result is: - Both A and B are always empty. I read about bash executing piped commands in sub-shell and that basically preventing one from piping input to read. However, the following: echo first second | while read A B ; do echo $A-$B

Why piping input to “read” only works when fed into “while read …” construct? [duplicate]

流过昼夜 提交于 2019-12-17 04:45:27
问题 This question already has answers here : Read values into a shell variable from a pipe (15 answers) Closed 5 months ago . I've been trying to read input into environment variables from program output like this: echo first second | read A B ; echo $A-$B And the result is: - Both A and B are always empty. I read about bash executing piped commands in sub-shell and that basically preventing one from piping input to read. However, the following: echo first second | while read A B ; do echo $A-$B

'do…while' vs. 'while'

拥有回忆 提交于 2019-12-17 03:04:07
问题 Possible Duplicates: While vs. Do While When should I use do-while instead of while loops? I've been programming for a while now (2 years work + 4.5 years degree + 1 year pre-college), and I've never used a do-while loop short of being forced to in the Introduction to Programming course. I have a growing feeling that I'm doing programming wrong if I never run into something so fundamental. Could it be that I just haven't run into the correct circumstances? What are some examples where it

'do…while' vs. 'while'

只愿长相守 提交于 2019-12-17 03:04:04
问题 Possible Duplicates: While vs. Do While When should I use do-while instead of while loops? I've been programming for a while now (2 years work + 4.5 years degree + 1 year pre-college), and I've never used a do-while loop short of being forced to in the Introduction to Programming course. I have a growing feeling that I'm doing programming wrong if I never run into something so fundamental. Could it be that I just haven't run into the correct circumstances? What are some examples where it