Re: Regular expression: option match after a greedy/non-greedy match
Hi Viet-Duc Le, On 17 Sep 2014, at 10:23, Viet-Duc Le wrote: > Greeting from S. Korea ! > > I am parsing the output of ffmpeg with perl. Particular, I want to print only > these lines among the output and capturing the resolution, i.e. 1280x720. > > Stream #0:0: Video: h264 (High), yuv420p, 1280x720, SAR 1:1 DAR 16:9, 23.98 > fps, 23.98 tbr, 1k tbn, 47.95 tbc (default) > Stream #0:1(jpn): Audio: ac3, 48000 Hz, stereo, fltp, 192 kb/s (default) > Stream #0:2(eng): Subtitle: ass (default) > . > > My code is following: > # INFO is pipe to ffmpeg > # Here, the is for debugging . > while ( ) { > if ( ) { > print "$1 $2 $3 $4\n"; > } > } > > Desirable outputs: > -> Video 1280 720 > Audio > Subtitle > > Regarding the : > 1. /Stream #\d:\d.*(Video|Audio|Subtitle).*(\d+)x(\d+)/ (greedy) > -> Video 0 720 > Q: why does $2 give 0? I remember .* match backward starting from the end of > the string. Then it should be "Video 1280 720" as output. that '0' is from 128'0', since the '.*' consumes 128. What it does under the hood is .* first will reach to the end of the target string, and then backtract according to the following regex. Once the whole regex is satisfied, it will stop backtracting, although further retracting will possibly also satisfy the regex. > > 2. /Stream #\d:\d.*(Video|Audio|Subtitle).*?(\d+)x(\d+)/ (non greedy) > -> Video 1280 720 > Q: I can understand this, but again I think (1) should work too. > > 3. /Stream #\d:\d.*(Video|Audio|Subtitle).*?(?:(\d+)x(\d+))?/ ( non-capturing > optional group ) > -> Video > Audio > Subtitle > Q: It seems that the resolution part is ignored because it is optional. > Otherwise, the output will contains "Video" only as (1) and (2). How can I > circumvent this ? that ?: prevents $ variables to capture the matching regex group. I guess you can get rid of it. The trailing ? already tells the regex group to match optionally. It is equivalent to {0,1}. The big problem coming with it is the middle .*?. Since the last part is optional, .*? will just match the least number of char possible, which is nothing. > > 4. /Stream #\d:\d.*(Video|Audio|Subtitle).+?(?:(\d+)x(\d+))?.*?$/ > -> Video > Audio > Subtitle > Q: I tried to match things after the resolution, hoping that it will be > captured. Again the ?: prevents it being captured. .+? in the middle is better, now it matches ':'. > > 5. /Stream #\d:\d.*(Video|Audio|Subtitle).+?(?:(\d+)x(\d+))?(.*?)$/ ( let's > capture the last part) > -> Videoh264 (High), yuv420p, 1280x720, SAR 1:1 DAR 16:9, 23.98 fps, > 23.98 tbr, 1k tbn, 47.95 tbc (default) > Audioac3, 48000 Hz, stereo, fltp, 192 kb/s (default) > Subtitleass (default) > Q: Now $2 and $3 is undef, and the rest of the string went to $4. Again, I am > quite puzzled by the output. If it is optional, it is non greedy. So everything goes to the (.*?)$. > > Please pardon my long email. I hope someone can point out the flaws in my > logic. Here, I can match and print Video/Audio/Subtitle separately. > But I wish for one expression to match them all, one expression to print > them. > In general, it is a better practise to add 'x' to your regex to make it more readable. My regex might not be the best, but it works as expected. use strict; use warnings; use 5.16.0; while(){ / (Video|Audio|Subtitle) (?: (?:.) +? (\d+x\d+) || (?:.)+ ) /x and say $1, $2, $3, $4; } __DATA__ Stream #0:0: Video: h264 (High), yuv420p, 1280x720, SAR 1:1 DAR 16:9, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default) Stream #0:1(jpn): Audio: ac3, 48000 Hz, stereo, fltp, 192 kb/s (default) Stream #0:2(eng): Subtitle: ass (default) The '||' operator will first check the group before it. It will only look at the other group if the first group fails. This puts your resolution group matching as priority, but not necessity. Hope this helps. Jing
Regular expression: option match after a greedy/non-greedy match
p{margin:0;padding:0;} Greeting from S. Korea ! I am parsing the output of ffmpeg with perl. Particular, I want to print only these lines among the output and capturing the resolution, i.e. 1280x720. Stream #0:0: Video: h264 (High), yuv420p, 1280x720, SAR 1:1 DAR 16:9, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default) Stream #0:1(jpn): Audio: ac3, 48000 Hz, stereo, fltp, 192 kb/s (default) Stream #0:2(eng): Subtitle: ass (default) . My code is following: # INFO is pipe to ffmpeg # Here, the is for debugging . while ( ) { if ( ) { print "$1 $2 $3 $4\n"; } } Desirable outputs: -> Video 1280 720 Audio Subtitle Regarding the : 1. /Stream #\d:\d.*(Video|Audio|Subtitle).*(\d+)x(\d+)/ (greedy) -> Video 0 720 Q: why does $2 give 0? I remember .* match backward starting from the end of the string. Then it should be "Video 1280 720" as output. 2. /Stream #\d:\d.*(Video|Audio|Subtitle).*?(\d+)x(\d+)/ (non greedy) -> Video 1280 720 Q: I can understand this, but again I think (1) should work too. 3. /Stream #\d:\d.*(Video|Audio|Subtitle).*?(?:(\d+)x(\d+))?/ ( non-capturing optional group ) -> Video Audio Subtitle Q: It seems that the resolution part is ignored because it is optional. Otherwise, the output will contains "Video" only as (1) and (2). How can I circumvent this ? 4. /Stream #\d:\d.*(Video|Audio|Subtitle).+?(?:(\d+)x(\d+))?.*?$/ -> Video Audio Subtitle Q: I tried to match things after the resolution, hoping that it will be captured. 5. /Stream #\d:\d.*(Video|Audio|Subtitle).+?(?:(\d+)x(\d+))?(.*?)$/ ( let's capture the last part) -> Videoh264 (High), yuv420p, 1280x720, SAR 1:1 DAR 16:9, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default) Audioac3, 48000 Hz, stereo, fltp, 192 kb/s (default) Subtitleass (default) Q: Now $2 and $3 is undef, and the rest of the string went to $4. Again, I am quite puzzled by the output. Please pardon my long email. I hope someone can point out the flaws in my logic. Here, I can match and print Video/Audio/Subtitle separately. But I wish for one expression to match them all, one expression to print them. Best regards, Viet-Duc
Re: Argument isn't numeric warning in if statement
-Original Message- From: SSC_perl Sent: Wednesday, September 17, 2014 10:37 AM To: Perl Beginners Subject: Argument isn't numeric warning in if statement I just ran across something puzzling. Why are these two statements not equivalent when it comes to warnings? if ($item->{'optionprice'}) { $item->{'unitprice'} += $item->{'optionprice'}; } and $item->{'unitprice'} += $item->{'optionprice'} if ($item->{'optionprice'}); Given the following values: $item->{'unitprice'} = '12.16'; $item->{'optionprice'} = ''; the 2nd statement returns an "Argument '' isn't numeric in addition (+)" warning, while the 1st one doesn't. I thought I read where Peal reads a statement like the 2nd one from right to left. It looks like it doesn't, since $item->{'optionprice'} is evaluated in spite of the 'if'. Am I mistaken? Perl 5.10.1 Can't reproduce the anomaly here on 5.10.0 and 5.12.0 - I don't have 5.10.1. ## C:\_32\pscrpt>type try.pl use warnings; $item->{'unitprice'} = '12.16'; $item->{'optionprice'} = ''; $item->{'unitprice'} += $item->{'optionprice'} if ($item->{'optionprice'}); C:\_32\pscrpt>perl try.pl C:\_32\pscrpt> ## Are you sure you've quoted the code (that's producing the warning) correctly ? Cheers, Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Argument isn't numeric warning in if statement
I just ran across something puzzling. Why are these two statements not equivalent when it comes to warnings? if ($item->{'optionprice'}) { $item->{'unitprice'} += $item->{'optionprice'}; } and $item->{'unitprice'} += $item->{'optionprice'} if ($item->{'optionprice'}); Given the following values: $item->{'unitprice'} = '12.16'; $item->{'optionprice'} = ''; the 2nd statement returns an "Argument '' isn't numeric in addition (+)" warning, while the 1st one doesn't. I thought I read where Peal reads a statement like the 2nd one from right to left. It looks like it doesn't, since $item->{'optionprice'} is evaluated in spite of the 'if'. Am I mistaken? Perl 5.10.1 Thanks, Frank SurfShop shopping cart is now open source... http://www.surfshopcart.com/ Setting up shop has never been easier! Follow us on GitHub! https://github.com/surfshopcart/surfshop -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Difference between list and arrays.
On Tue, Sep 16, 2014 at 02:43:28PM -0500, Andy Bach wrote: > On Tue, Sep 16, 2014 at 1:45 PM, Paul Johnson wrote: > > > The comma operator evaluates its LHS, throws it away, evaluates its RHS > > and returns that. The comma operator is left associative (see perlop). > > > > So the result of evaluating the RHS (1, 2, 3) is: > > > > (1, 2, 3) -> ((1, 2), 3) -> (2, 3) -> 3 > > > > And this is why $one_var gets the value 3 - not because there are three > > elements in a list on the RHS. > > > > D'oh! Somewhere I knew something like that, that a list in scalar context > ends up assigning the last element to the scalar - though I'm pretty sure I > didn't know it was due to the comma operator. I should have though, as: > # perl -we 'my $one = (3,2,1); print "$one\n"' > Useless use of a constant in void context at -e line 1. > Useless use of a constant in void context at -e line 1. > 1 > > Those two warnings are the "dropping" of the result of the first to comma > operations. Hmm, so this works [1]: > > my $second_elem = ('first', 'second', 'third')[1]; > $second_elem eq 'second'; > > because ...??? Aha, turning it up a notch ;-) In this case, ('first', 'second', 'third') is actually a list. Why? Because the [1] indexes into it. The [1] forces it into list context. You can't index into something that is in scalar context. > $ perldoc -q 'difference between a list and an array' > Well, I would've found that if perldoc would've found this for queries on > "scalar context", "list context", "array context" or, for that matter, > plain old "context" but .. Yes, I think the search is only on the title of the section, not its contents. Which can mean that you need to find what you are looking for in order to know how to find it. -- Paul Johnson - p...@pjcj.net http://www.pjcj.net -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Difference between list and arrays.
On Tue, Sep 16, 2014 at 1:45 PM, Paul Johnson wrote: > The comma operator evaluates its LHS, throws it away, evaluates its RHS > and returns that. The comma operator is left associative (see perlop). > > So the result of evaluating the RHS (1, 2, 3) is: > > (1, 2, 3) -> ((1, 2), 3) -> (2, 3) -> 3 > > And this is why $one_var gets the value 3 - not because there are three > elements in a list on the RHS. > D'oh! Somewhere I knew something like that, that a list in scalar context ends up assigning the last element to the scalar - though I'm pretty sure I didn't know it was due to the comma operator. I should have though, as: # perl -we 'my $one = (3,2,1); print "$one\n"' Useless use of a constant in void context at -e line 1. Useless use of a constant in void context at -e line 1. 1 Those two warnings are the "dropping" of the result of the first to comma operations. Hmm, so this works [1]: my $second_elem = ('first', 'second', 'third')[1]; $second_elem eq 'second'; because ...??? > This all becomes easier to understand if you don't use the values 1, 2 and 3 :) I "knew" that was a bad idea ... just not enough to not use it. $ perldoc -q 'difference between a list and an array' ... As a side note, there’s no such thing as a list in *scalar context*. When you say $scalar = (2, 5, 7, 9); you’re using the comma operator in *scalar context*, so it uses the scalar comma operator. There never was a list there at all! This causes the last value to be returned: 9. Well, I would've found that if perldoc would've found this for queries on "scalar context", "list context", "array context" or, for that matter, plain old "context" but .. -- a [1] perl -we 'my $s_e = ("x", "y", "z")[1]; print "$s_e\n"' same as: perl -we 'my $s_e = qw(x y z)[1]; print "$s_e\n"' and: perl -we 'my ($s_e) = qw(x y z)[1]; print "$s_e\n"' Andy Bach, afb...@gmail.com 608 658-1890 cell 608 261-5738 wk
Re: Difference between list and arrays.
On Tue, Sep 16, 2014 at 12:12:05PM -0500, Andy Bach wrote: >The other thing to think > about is "context" - the LHS of the assigning "=" determines how the RHS > list is treated. So far, so good. Well, almost ... > In scalar context > my $one_var = (1, 2, 3); > > the list returns its element count But, I'm afraid, this is a common misconception. The fact is that there is no such thing as a list in scalar context. So what, then, is the construct above? You were correct in saying that it (whatever it is) is evaluated in scalar context. In scalar context, the parentheses () are used simply for precedence. The commas are operators. The comma operator evaluates its LHS, throws it away, evaluates its RHS and returns that. The comma operator is left associative (see perlop). So the result of evaluating the RHS (1, 2, 3) is: (1, 2, 3) -> ((1, 2), 3) -> (2, 3) -> 3 And this is why $one_var gets the value 3 - not because there are three elements in a list on the RHS. This all becomes easier to understand if you don't use the values 1, 2 and 3 :) See also perldoc -q 'difference between a list and an array' -- Paul Johnson - p...@pjcj.net http://www.pjcj.net -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Difference between list and arrays.
On Tue, Sep 16, 2014 at 4:22 AM, Uday Vernekar wrote: > Confusion on array and list. can anybody explain me the difference between > list and arrays. > > my @xyz = ( 4, 5, 6 ); > > The right-hand side of the equals sign is a list.here I assign that list > to the variable @xyz. > its an array,list can be assigned to an array. > > on similiar lines > we can assign Lists to hashes > my %zzz = ( a => 42, b => 43, b => 44 ); > Not sure exactly what your confusion is, you are correct. Couple notes, one is, the hash assigned list uses the "super comma" ("=>") which is actually just a fancy comma; it quotes the left hand side for you, so: ( a => 42, b => 43, b => 44 ); is the same as (note, 2nd and 3rd keys were "b" which'd overwrite the "43" w/ "44" so I made it a "c": ( "a", 42, "b", 43, "c", 44 ); That is, the hash assignment just requires (well, Perl will assign "undef" if you're short a value and complain if you've warnings set[1]) an even number of elements; key, value, pairs. You can do: my @uvw = ( a => 42, b => 43, b => 44 ); and end up w/ 6 elements (in order) in the array. The other thing to think about is "context" - the LHS of the assigning "=" determines how the RHS list is treated. In scalar context my $one_var = (1, 2, 3); the list returns its element count (as does an array - for hashes, it's similar but you get a fraction: # of elements/buckets [2]) so $one_var == 3; In list context, you get assignment, as you see above or: my ($one_var) = (1,2,3); Here, the first element on the RHS gets assigned to the first member of the LHS, i.e. $one_var == 1; The rest get thrown away. If the LHS has an array, it gets everything else, if there's too many LHS scalars, they get assigned undef. -- a [1] # perl -we 'my %h = (b => 3, a); print "$h{a}\n";' Unquoted string "a" may clash with future reserved word at -e line 1. Odd number of elements in hash assignment at -e line 1. Use of uninitialized value in concatenation (.) or string at -e line 1. [2] http://stackoverflow.com/questions/7427381/what-do-you-get-if-you-evaluate-a-hash-in-scalar-context Andy Bach, afb...@gmail.com 608 658-1890 cell 608 261-5738 wk
Suggestions - printing to ZEBRA credit card printer attached to Win7 PC
I've got a ZEBRA ZXP Series 1 credit card printer attached to a Win7 PC. By using Word and creating a paper size of 84mm by 54mm I can printer on a card successfully. The cards we will be using are pre-printed with logo's etc. but I need to print a person's name and reference number across the botom of it. The data is being generated by my LAPP based system. I'm looking at options as to how I can implement a system for printing the cards. As I see it, I have a number of options. 1) mail-merge using Word. Generate a CSV file via PHP for download. Manual intervention required to download the file and run the mail-merge, but simple and easy to set up. 2) Have a process run on my LAPP server to create the document and print using a normal windows share. 3) Have a daemon running on the Win7 PC accepting instructions via the network and printing the cards. This then goes back to the earlier thread of which PERL to use. 4) Any other method you can suggest. Obviously, options 2 and 3 are the ones I'd prefer. I've Googled "perl Windows print" and all I get are various postings, many of them over 10 years old on how to print text to PRN or a windows share. There are posts about generating HTML/CSS and using rundll32 to print it. Google on "windows html css print" doesn't give much help. Does anyone know how I would be able to get it to print to 84CMx54CM page? If I don't go down the HTML/CSS route, what other options are available for producing the document suitable for sending to the printer. Going back 20 years to when I used to write in Borland Delphi there was a component for creating printer documents and adding contents. Are there similar PERL modules? What are people's preferences? -- Gary Stainburn -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Difference between list and arrays.
Confusion on array and list. can anybody explain me the difference between list and arrays. my @xyz = ( 4, 5, 6 ); The right-hand side of the equals sign is a list.here I assign that list to the variable @xyz. its an array,list can be assigned to an array. on similiar lines we can assign Lists to hashes my %zzz = ( a => 42, b => 43, b => 44 ); -- * Don't ask them WHY they hurt you, because all they'll tell you is lies and excuses. Just know they were wrong, and try to move on. **