Explain a block of code:


 
Thread Tools Search this Thread
Top Forums Programming Explain a block of code:
# 1  
Old 09-24-2009
Explain a block of code:

Hi, I have this block in a code and I need to know it's meaning:

Code:
bool selectEvents = config.get("selectEvents",false);
  if (selectEvents) {
    ifstream in("events");
    while (in) {
      int run, evt;
      in >> run >> evt;
      if (in.eof()) break;
      selectedEvents.insert(make_pair(run,evt));
    }
    cout << "select " << selectedEvents.size() << " events" << endl;
  }

The statements I really need to know their meanings are the first one, which starts with bool and the one which starts with selectedEvents.insert().

I will appreciate any help,

eager
# 2  
Old 09-24-2009
Some of it we can only guess since it uses functions defined elsewhere, but here goes:

Code:
// Try to get the configuration value 'selectEvents'
bool selectEvents = config.get("selectEvents",false);
  // If the configuration value is true
  if (selectEvents) {
    // Open the file 'events'
    ifstream in("events");
    // While in still has more data to be read:
    while (in) {
      int run, evt;
      // Read two integers from the file
      in >> run >> evt;
      // If we couldn't read both, quit the loop right here
      if (in.eof()) break;
      // Otherwise, insert the new event into selectedEvents.
      selectedEvents.insert(make_pair(run,evt));
    }
    // Print the number of events stored in selectedEvents.
    cout << "select " << selectedEvents.size() << " events" << endl;
  }

We can't tell what selectedEvents actually is from this code snippet since it doesn't declare it anyway, it just uses it, but it's some sort of container, maybe a list. I can't guess what this 'config' function (or member, or whatever it is) comes from.
# 3  
Old 09-24-2009
Excellent!

So this line here:
bool selectEvents = config.get("selectEvents",false);

means selectEvents is of type bool, and it is equal to the outcome of the get() member. Now a small question:
What is the meaning of this format:
function("name", false)? what is the meaning of false here? does it mean if "name is not there"?

eager

PS.

All your guesses are right, it is a container. I am not able to post the whole code though Smilie But I am sure if I have your C++ knowledge, I could/can make miracles Smilie
# 4  
Old 09-25-2009
Quote:
Originally Posted by eager2no
So this line here:
bool selectEvents = config.get("selectEvents",false);

means selectEvents is of type bool, and it is equal to the outcome of the get() member. Now a small question:
What is the meaning of this format:
function("name", false)? what is the meaning of false here? does it mean if "name is not there"?
Perhaps, it could mean this

get (char* str, bool bAlwaysCreate)

i.e. if str is not present in config, create it and add it to the container.
# 5  
Old 09-25-2009
Quote:
Originally Posted by eager2no
Excellent!

So this line here:
bool selectEvents = config.get("selectEvents",false);

means selectEvents is of type bool, and it is equal to the outcome of the get() member. Now a small question:
What is the meaning of this format:
function("name", false)? what is the meaning of false here? does it mean if "name is not there"?
It might be what vino says. It might be a default value(what to return if the config value does not exist). It might be a useless stub for a feature not yet implemented. Without the function definition(or even its header) we'd only be guessing.
# 6  
Old 09-25-2009
Thank you all so much. I am really happy I am here in this nice forum.
Again, apologies for not being able to have the code here.

~eager
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

can you explain this sed code?

can anyone please explain this code? sed ':a;N;$!ba;s/]\n//g' file it replaces lines ending with "]" and concatenates with the next line so that line1] line2 becomes line1line2 i don't understand this part: :a;N;$!ba; I have noted that I can replace "a" with any letter: ... (1 Reply)
Discussion started by: locoroco
1 Replies

2. Shell Programming and Scripting

Please explain what this Awk code is doing

Hi Guys, Please help me, I am new to programming and I don’t understand what some parts of this code are doing. I have comments on the parts I know, please help if my understanding of the code is not correct and also help with parts with questions. awk ' { gsub( ">",... (1 Reply)
Discussion started by: James_Owen
1 Replies

3. Shell Programming and Scripting

Please explain what this code is doing

Hi, Pls explain me what the below code is doing. specially meaning if -a while calling test function- case $1 in 1) beg_dt=01; end_dt=07 ;; 2) beg_dt=08; end_dt=14 ;; 3) beg_dt=15; end_dt=21 ;; 4) beg_dt=22; end_dt=28 ;; 5) beg_dt=29; end_dt=31 ;; esac test \( `date +%w` -eq $2 -a... (3 Replies)
Discussion started by: sendtoshailesh
3 Replies

4. Shell Programming and Scripting

Can someone explain the 2 lines of code?

I have something like in the script. Can anyone explain what does the sort command does and followed by the sed sort -r -k3.7,3 -k3.1,3 -k3.4,3 -t: $dir"abc_"$x"_parsed" | sed -e 's/\(\)\(\{3\}\)/\1,\2/2' -e 's/\/2,00/\/200/' The file abc_"$x"_parsed contains something like below ... (7 Replies)
Discussion started by: Muthuraj K
7 Replies

5. Shell Programming and Scripting

Explain SED code

Hi, Can anyone pls explain me the below SED code in detail. sed -e :a -e '$!N;s/\n//;ta' -e P -e D When this code is executed with a file(has 1lac records), it is taking very long time to process. So I wanted to modify this SED code with equivalant AWK code. Thanks, Sri (1 Reply)
Discussion started by: srilaxmi
1 Replies

6. Shell Programming and Scripting

Please explain what code is doing between the two dotted lines

Can someone please explain the meaning of code between the two lines below. if ; then echo "./filter.sh <a flat file containing group and id>" fi ------------------------------------------------------------ >IDnotexist #account in request does not exist in NIS... (1 Reply)
Discussion started by: pinnacle
1 Replies

7. Shell Programming and Scripting

can any one explain the meaning of the below code ?

while getopts :g:l:h: KEY $* do case $KEY in g) NAME=$OPTARG ;; l) LAB=$OPTARG ;; h) HD=$OPTARG ;; *) echo "Illegal option: $OPTARG" echo "$usage" exitStatus=1 exitGrace esac done (2 Replies)
Discussion started by: santosh1234
2 Replies

8. Shell Programming and Scripting

Can any one explain what this code will do

ccc_con,CCC_CON,0 Above is the input for this code #!/usr/bin/bash my_path=`dirname $0` T_table=$1 S_table=$2 P_table=$3 #Star new code while read ${my_path}/arch_table_list.txt { awk -F "," '{print $1}' ${my_path}/arch_table_list.txt ${S_table} awk -F "," '{print... (1 Reply)
Discussion started by: scorp_rahul23
1 Replies

9. UNIX for Advanced & Expert Users

explain the code

Hi , Can anyone explains what does the below highlighted statements means: # Set environment variables . ${0%/*}/wrkenv.sh jobName_sh=${0##*/} jobName=${jobName_sh%.*} Thanks, Sri (1 Reply)
Discussion started by: srilaxmi
1 Replies

10. UNIX for Dummies Questions & Answers

could someone explain this code

hey peeps could someone explain what this part of the code means: 'if echo $* | grep -q' $i '|| thanks tHe_nEw_GuY (2 Replies)
Discussion started by: the_new_guy
2 Replies
Login or Register to Ask a Question