Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I cannot trace why all my captured vars come back undef because the debugger steps not finegrained enough. Where do I need to fix my code?

Also, how can I express it in regex that the captured var for e.g. $vr stays undef if there is no video bitrate, yet X/Y resolution and FPS are populated (because they are always on the line)?

Is that a job for regex anyway or is it more elegant with other tools? If yes, what do you recommend?

#!perl use strict; use diagnostics; use Data::Dump::Streamer; while (<DATA>) { my ($ar, $x, $y, $vr, $fps); ($ar) = (m|(\d+) kb/s|) if /Stream.*Audio/; ($x, $y, $vr, $fps) = (m|(\d+)x(\d+).*(\d+) kb/s.*(\d+\.\d+) fps|) + if /Stream.*Video/; Dump [$ar, $x, $y, $vr, $fps] if /^$/; }; __DATA__ E:\jyb.avi Stream #0.1: Audio: mp3, 44100 Hz, mono, 80 kb/s Stream #0.0: Video: mpeg4, yuv420p, 464x240, 25.00 fps(r) E:\weit.m4v Stream #0.0(eng): Audio: aac, 44100 Hz, stereo Stream #0.1(eng): Video: h264, yuv420p, 320x240, 15.00 fps(r) E:\otm.mov Stream #0.1(eng): Audio: qdm2, 44100 Hz, stereo Stream #0.0(eng): Video: svq3, yuv420p, 320x240, 24.00 fps(r) E:\bo.mp4 Stream #0.1(eng): Audio: aac, 44100 Hz, stereo Stream #0.0(eng): Video: mpeg4, yuv420p, 320x240, 25.00 fps(r) E:\tom.mpeg Stream #0.1[0x1c0]: Audio: mp2, 44100 Hz, stereo, 256 kb/s Stream #0.0[0x1e0]: Video: mpeg1video, yuv420p, 352x240, 1180 kb/s, +29.97 fps(r) E:\gb.rmvb Stream #0.1: Audio: cook, 44100 Hz, stereo, 44 kb/s Stream #0.0: Video: RV40 / 0x30345652, 352x288, 180 kb/s, 12.00 fps( +r) E:\xt.wmv Stream #0.0: Audio: wmav2, 32000 Hz, stereo, 32 kb/s Stream #0.1: Video: wmv1, yuv420p, 320x240, 15.00 fps(r)

Replies are listed 'Best First'.
Re: conditional regex
by ikegami (Patriarch) on Feb 05, 2007 at 17:27 UTC

    my ($ar, $x, $y, $vr, $fps);
    creates new variables through each pass of the loop.
    Dump [$ar, $x, $y, $vr, $fps] if /^$/;
    only prints on the passes where the input is a blank line. When $_ is a blank line, the other if conditions are false, so the variables don't get popupulated.

    One solution is to read a paragraph at a time.

    local $/ = ''; # Paragraph mode. while (<DATA>) { my ($ar) = m|(\d+) kb/s|; my ($x, $y, $vr, $fps) = m|(\d+)x(\d+).*(?:(\d+) kb/s.*)?(\d+\.\d+) + fps|; Dump [$ar, $x, $y, $vr, $fps]; }

    Update: Since the data is rather variable, the following might be more reliable. I also added a fix to ignore 0x12345678.

    local $/ = ''; # Paragraph mode. while (<DATA>) { my ($ar ) = m|Audio.*?(\d+) kb/s|; my ($x, $y) = m|(?!0)(\d+)x(\d+)|; my ($vr ) = m|Video.*?(\d+) kb/s|; my ($fps ) = m|(\d+(?:\.\d+)?) fps|; Dump [$ar, $x, $y, $vr, $fps]; }
Re: conditional regex
by Util (Priest) on Feb 05, 2007 at 18:51 UTC
    #!perl use strict; use warnings; #use diagnostics; use Data::Dump::Streamer; my $file_types = join '|', qw( avi m4v mov mp4 mpeg rmvb wmv ); my $empty_re = qr{ \A \s* \z }msx; my $filename_re = qr{ \A ( [A-Z] : \\ .+? \. (?:$file_types) ) \s* \z +}msx; my $audio_re = qr{ \A \s+ Stream [ ] \#\d+\.\d+ (?: \(eng\) | \[0x[0-9a-f]+\] )? : [ ] Audio: [ ] ( mp[23] | aac | qdm2 | cook | wmav2 ), # 1 [ ] (\d+)[ ]Hz, # 2 [ ] (mono|stereo) # 3 (?: , [ ] (\d+)[ ]kb/s )? # 4 \s* \z }msx; my $video_re = qr{ \A \s+ Stream [ ] \#\d+\.\d+ (?: \(eng\) | \[0x[0-9a-f]+\] )? : [ ] Video: [ ] ( mpeg4 | h264 | svq3 | mpeg4 | mpeg1video | wmv1 | RV40 ) # + 1 (?: [ ]/ | ,) [ ] ( yuv420p | 0x[0-9a-f]+ ), # + 2 [ ] (\d+)x(\d+) # + 3,4 (?: , [ ] (\d+)[ ]kb/s )? # + 5 , [ ] ( \d+ (?: \. \d+ )? ) [ ] fps\(r\) # + 6 \s* \z }msx; my ( $filename, $ar, $x, $y, $vr, $fps ); while (<DATA>) { if ( /$filename_re/ ) { $filename = $1; next; } if ( /$audio_re/ ) { $ar = $4; next; } if ( /$video_re/ ) { ( $x, $y, $vr, $fps ) = ( $3, $4, $5, $6 ); next; } if ( /$empty_re/ ) { Dump [ $filename, $ar, $x, $y, $vr, $fps ]; undef $_ for ( $filename, $ar, $x, $y, $vr, $fps ); next; } warn "Unrecognized line '$_' "; }; __DATA__ E:\jyb.avi Stream #0.1: Audio: mp3, 44100 Hz, mono, 80 kb/s Stream #0.0: Video: mpeg4, yuv420p, 464x240, 25.00 fps(r) E:\weit.m4v Stream #0.0(eng): Audio: aac, 44100 Hz, stereo Stream #0.1(eng): Video: h264, yuv420p, 320x240, 15.00 fps(r) E:\otm.mov Stream #0.1(eng): Audio: qdm2, 44100 Hz, stereo Stream #0.0(eng): Video: svq3, yuv420p, 320x240, 24.00 fps(r) E:\bo.mp4 Stream #0.1(eng): Audio: aac, 44100 Hz, stereo Stream #0.0(eng): Video: mpeg4, yuv420p, 320x240, 25.00 fps(r) E:\tom.mpeg Stream #0.1[0x1c0]: Audio: mp2, 44100 Hz, stereo, 256 kb/s Stream #0.0[0x1e0]: Video: mpeg1video, yuv420p, 352x240, 1180 kb/s, +29.97 fps(r) E:\gb.rmvb Stream #0.1: Audio: cook, 44100 Hz, stereo, 44 kb/s Stream #0.0: Video: RV40 / 0x30345652, 352x288, 180 kb/s, 12.00 fps( +r) E:\xt.wmv Stream #0.0: Audio: wmav2, 32000 Hz, stereo, 32 kb/s Stream #0.1: Video: wmv1, yuv420p, 320x240, 15.00 fps(r)