We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
? and : is a ternary operator. This kind of syntax a ? b : c appears in many languages and it means "if a then b else c". Read more about that at https://en.wikipedia.org/wiki/%3F%3A
love it !
for people who did't get it :NR Number of record > it usually give us the number of the current record (each line) :
1
2
3
4
our code check of each number of the above, if it's %2 means print ; instead of a new line. else just print new line .
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
'Awk' - 4
You are viewing a single comment's thread. Return to all comments →
Mine
What a beutiful code!
here is problem solution - https://programs.programmingoneonone.com/2022/02/hackerrank-awk-4-problem-solution.html
impressive solution man !!
I didn't quite understand it can you please explain it.
ORS stands for Output Record Separator. And NR is Number of Record (or just current line number). ORS and NR are just variables and you can do with them what you want. There are also useful FS, OFS, RS, NF, FILENAME, FNR. See http://www.thegeekstuff.com/2010/01/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/
wow! good explanation bro!
not just good, AMAZING!
what does ? and : mean in this case if you can elaborate more please?
? and : is a ternary operator. This kind of syntax
a ? b : c
appears in many languages and it means "if a then b else c". Read more about that at https://en.wikipedia.org/wiki/%3F%3ATernary operator
They are ternery operator. CODE1 and CODE2 are equivalent CODE 1 if(condition){ statement1; } else{ statement2; } CODE2 condition?statement1:statement2;
It's the concatenation
Clever. Nice demonstration of the use of ORS.
Very nice! I used the paste tool to format the output. Can you provide a link as to where is this documented? I would like to read it in more detail.
excellent code bro!
Nice solution. Thanks.
awk 'NR%2 == 1?ORS=";":ORS="\n"'
is easier to understand.btw your code gives syntax error.
This code works as follows: awk 'ORS=NR%2?";":"\n"'
I am asking why the code given by a_programr gave error
above code gives syntax error. anyone explain please.
Impressive
I think this is the only solution which makes the problem interesting! Congratulations, it's great!
Same code using if else
awk '{if (NR%2==1) { ORS=";"; print} else {ORS="\n";print;}}'
Super smart! I learned a lot!
Does awk print the current line? I tried your code and it worked but I was under the impression that
print $0
was required.love it ! for people who did't get it :NR Number of record > it usually give us the number of the current record (each line) : 1 2 3 4 our code check of each number of the above, if it's %2 means print ; instead of a new line. else just print new line .