From: John W. Krahn on 2 Dec 2009 05:13 Rakesh Sharma wrote: > > Thanks for pointing it out! > > I was working under the assumption given by the OT as: > >>>> 1- If line_b appears just immediately after line_a, then do nothing. > >>>> 2- In other cases, move line_b to make sure it just immediately after line_a. > > He doesn't want to swap the positions of line_a <=> line_b, rather he > just wants to place line_b under line_a. > So that's the basic premise I used to write it. > > But I had a few questions of my own. > > 1) What's the difference between perl -00pae & perl -0777pae? > I used perl -00 to intend "slurping". The perl manual lists that > we use -0777 as you have used also. The -00 option uses the "\0" (the ASCII NUL) character as the Input Record Separator which should not appear in a text file, but might, and -0777 sets the Input Record Separator to an invalid value so it will always properly slurp the entire file. > 2) In the line that I wrote: () = map { /line_[ab]/ && do{$h{$&}=[$i, > $_];};++$i; } @F; > I remember being forced to put that do{} construct in otherwise > it gave an error. Whereas, if I take a look > at what you wrote: $F[ $_ ] =~ /line_([ab])/ and $h{ $1 } = $_ > for 0 .. $#F; > Why didn't perl blurt out in this case? The '&&' operator has higher precedence than the '=' operator which will produce a error message: $ perl -le' $ARGV[0] && $x = 123 ' 789 Can't modify logical and (&&) in scalar assignment at -e line 1, at EOF Execution of -e aborted due to compilation errors. Whereas the 'and' operator has lower precedence than the '=' operator so it will work properly: $ perl -le' $ARGV[0] and $x = 123 ' 789 If you want to use the '&&' operator you will have to enclose the assignment in parentheses: $ perl -le' $ARGV[0] && ( $x = 123 ) ' 789 John -- The programmer is fighting against the two most destructive forces in the universe: entropy and human stupidity. -- Damian Conway
From: John W. Krahn on 2 Dec 2009 05:23 Rakesh Sharma wrote: > > Thanks for pointing it out! > > I was working under the assumption given by the OT as: > >>>> 1- If line_b appears just immediately after line_a, then do nothing. > >>>> 2- In other cases, move line_b to make sure it just immediately after line_a. > > He doesn't want to swap the positions of line_a <=> line_b, rather he > just wants to place line_b under line_a. > So that's the basic premise I used to write it. > > But I had a few questions of my own. > > 1) What's the difference between perl -00pae & perl -0777pae? > I used perl -00 to intend "slurping". The perl manual lists that > we use -0777 as you have used also. The -00 option uses the "\0" (the ASCII NUL) character as the Input Record Separator which should not appear in a text file, but might, and -0777 sets the Input Record Separator to an invalid value so it will always properly slurp the entire file. > 2) In the line that I wrote: () = map { /line_[ab]/ && do{$h{$&}=[$i, > $_];};++$i; } @F; > I remember being forced to put that do{} construct in otherwise > it gave an error. Whereas, if I take a look > at what you wrote: $F[ $_ ] =~ /line_([ab])/ and $h{ $1 } = $_ > for 0 .. $#F; > Why didn't perl blurt out in this case? The '&&' operator has higher precedence than the '=' operator which will produce a error message: $ perl -le' $ARGV[0] && $x = 123 ' 789 Can't modify logical and (&&) in scalar assignment at -e line 1, at EOF Execution of -e aborted due to compilation errors. Whereas the 'and' operator has lower precedence than the '=' operator so it will work properly: $ perl -le' $ARGV[0] and $x = 123 ' 789 If you want to use the '&&' operator you will have to enclose the assignment in parentheses: $ perl -le' $ARGV[0] && ( $x = 123 ) ' 789 John -- The programmer is fighting against the two most destructive forces in the universe: entropy and human stupidity. -- Damian Conway
From: John W. Krahn on 2 Dec 2009 05:26 Rakesh Sharma wrote: > > Thanks for pointing it out! > > I was working under the assumption given by the OT as: > >>>> 1- If line_b appears just immediately after line_a, then do nothing. > >>>> 2- In other cases, move line_b to make sure it just immediately after line_a. > > He doesn't want to swap the positions of line_a <=> line_b, rather he > just wants to place line_b under line_a. > So that's the basic premise I used to write it. > > But I had a few questions of my own. > > 1) What's the difference between perl -00pae & perl -0777pae? > I used perl -00 to intend "slurping". The perl manual lists that > we use -0777 as you have used also. The -00 option uses the "\0" (the ASCII NUL) character as the Input Record Separator which should not appear in a text file, but might, and -0777 sets the Input Record Separator to an invalid value so it will always properly slurp the entire file. > 2) In the line that I wrote: () = map { /line_[ab]/ && do{$h{$&}=[$i, > $_];};++$i; } @F; > I remember being forced to put that do{} construct in otherwise > it gave an error. Whereas, if I take a look > at what you wrote: $F[ $_ ] =~ /line_([ab])/ and $h{ $1 } = $_ > for 0 .. $#F; > Why didn't perl blurt out in this case? The '&&' operator has higher precedence than the '=' operator which will produce a error message: $ perl -le' $ARGV[0] && $x = 123 ' 789 Can't modify logical and (&&) in scalar assignment at -e line 1, at EOF Execution of -e aborted due to compilation errors. Whereas the 'and' operator has lower precedence than the '=' operator so it will work properly: $ perl -le' $ARGV[0] and $x = 123 ' 789 If you want to use the '&&' operator you will have to enclose the assignment in parentheses: $ perl -le' $ARGV[0] && ( $x = 123 ) ' 789 John -- The programmer is fighting against the two most destructive forces in the universe: entropy and human stupidity. -- Damian Conway
From: Rakesh Sharma on 3 Dec 2009 03:29 On Dec 2, 3:12 pm, "John W. Krahn" <some...(a)example.com> wrote: > Rakesh Sharma wrote: > > > Thanks for pointing it out! > > > I was working under the assumption given by the OT as: > > >>>> 1- If line_b appears just immediately after line_a, then do nothing. > > >>>> 2- In other cases, move line_b to make sure it just immediately after line_a. > > > He doesn't want to swap the positions of line_a <=> line_b, rather he > > just wants to place line_b under line_a. > > So that's the basic premise I used to write it. > > > But I had a few questions of my own. > > > 1) What's the difference between perl -00pae & perl -0777pae? > > I used perl -00 to intend "slurping". The perl manual lists that > > we use -0777 as you have used also. > > The -00 option uses the "\0" (the ASCII NUL) character as the Input > Record Separator which should not appear in a text file, but might, and > -0777 sets the Input Record Separator to an invalid value so it will > always properly slurp the entire file. > > > 2) In the line that I wrote: () = map { /line_[ab]/ && do{$h{$&}=[$i, > > $_];};++$i; } @F; > > I remember being forced to put that do{} construct in otherwise > > it gave an error. Whereas, if I take a look > > at what you wrote: $F[ $_ ] =~ /line_([ab])/ and $h{ $1 } = $_ > > for 0 .. $#F; > > Why didn't perl blurt out in this case? > > The '&&' operator has higher precedence than the '=' operator which will > produce a error message: > > $ perl -le' $ARGV[0] && $x = 123 ' 789 > Can't modify logical and (&&) in scalar assignment at -e line 1, at EOF > Execution of -e aborted due to compilation errors. > > Whereas the 'and' operator has lower precedence than the '=' operator so > it will work properly: > > $ perl -le' $ARGV[0] and $x = 123 ' 789 > > If you want to use the '&&' operator you will have to enclose the > assignment in parentheses: > > $ perl -le' $ARGV[0] && ( $x = 123 ) ' 789 > > John > -- > The programmer is fighting against the two most > destructive forces in the universe: entropy and > human stupidity. -- Damian Conway- Hide quoted text - > > - Show quoted text - Thanks once again John for your masterly explanations.! Has it ever crossed your mind (or O'Reilly's) to gather your perl-one- liner solutions from all over comp.unix.shell, comp.perl, etc. & put them in a book form? Thanks, --Rakesh
First
|
Prev
|
Pages: 1 2 3 4 5 6 7 Prev: disable backslash escape for command line parameters Next: Quote problem |