Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Ordering of print statements

by Eshan_k (Acolyte)
on Jun 01, 2018 at 22:41 UTC ( #1215681=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, I am seeking perl wisdom. I have a text file in format:

APP> LumaIntraMode :D45_PRED, use_intrabc:0 AED> Dif: 80369151, Rng: 34624, Cnt: 10, Ret: 6 AED> Dif: 642953215, Rng: 52408, Cnt: 7, Ret: 13 APP> ChromaIntraMode :UV_CFL_PRED AED> Dif: 22413311, Rng: 36032, Cnt: 2, Ret: 5 AED> Dif: 115632447, Rng: 63232, Cnt: 11, Ret: 3 APP> ALPHA_U: 48, ALPHA_V: 0, CFL_ALPHA_SIGN: 5, CFL_ALPHA_IDX: 4 +8 APP> IntraAngleDelta : 3 0

I am trying to order print statements after grepping information from above text file.

if ($line =~ /\s*APP>\s*LumaIntraMode\s:(\w+),\suse_intrabc:(\d)/i) { print "Luma_mode = $1\n"; } if ($line =~ /\s*APP>\s*ChromaIntraMode:\s(\w+/i) { print "Chroma mode: $1\n"; } if ($line =~ /\s*ALPHA_U: (\d+), ALPHA_V: (\d+), CFL_ALPHA_SIGN: 5/i) +{ print "CFL : $3\n"; } if ($line =~ /\s*APP>\sIntraAngleDelta\s:\s(\d+)\s(\d+)/i) { print "Luma Angle : $1\n" } if ($line =~ /\s*APP>\sIntraAngleDelta\s:\s(\d+)\s(\d+)/i) { print "Chroma Angle : $2\n" }

I am getting output as: <\p>

Luma Mode: D45_PRED Chroma Mode: UV_CFL_PRED CFL: 5 Luma Angle: 3 Chroma Angle: 0

Can anyone suggest logic to print the above output in following order (changing ordering of print statement):

Luma Mode: D45_PRED Luma Angle: 3 Chroma Mode: UV_CFL_PRED CFL: 5 Chroma Angle: 0

I would really appreciate any help.

Replies are listed 'Best First'.
Re: Ordering of print statements
by Paladin (Vicar) on Jun 01, 2018 at 22:51 UTC
    I'm assuming that's all inside of a while loop you are using to read the file. Instead of printing when you match the line, store the value in a variable. Then after the while loop, print it all out.
    if ($line =~ /\s*APP>\s*LumaIntraMode\s:(\w+),\suse_intrabc:(\d)/i) { $values{Luma_mode} = $1; } # etc...
    Then after the loop
    print "Luma Mode: ", $values{Luma_Mode}, "\n"; print "Luma Angle: ", $values{Luma_Angle}, "\n"; # etc...
      Thanks Paladin for your reply.
Re: Ordering of print statements
by Marshall (Canon) on Jun 02, 2018 at 03:03 UTC
    I would do as Paladin suggested, put values into a hash. To print, I would declare an array with the correct order that you want and then use that array in the main loop of a generalized print routine. Of course you might want to think about what happens if for some reason some desired key didn't wind up existing.
    #!/usr/bin/perl use strict; use warnings; $|=1; # generated from the if regex statements instead of printing # right away my %values = ( 'Luma Mode' => 'D45_PRED', 'Chroma Mode' => 'UV_CFL_PRED', 'CFL' => 5, 'Luma Angle' => 3, 'Chroma Angle' => 0, ); my @order = ('Luma Mode', 'Luma Angle', 'Chroma Mode', 'CFL', 'Chroma Angle'); foreach my $key (@order) { print "$key:\t $values{$key}\n"; } __END__ Luma Mode: D45_PRED Luma Angle: 3 Chroma Mode: UV_CFL_PRED CFL: 5 Chroma Angle: 0
    Oh, I suppose that this code:
    if ($line =~ /\s*APP>\sIntraAngleDelta\s:\s(\d+)\s(\d+)/i) { print "Luma Angle : $1\n" } if ($line =~ /\s*APP>\sIntraAngleDelta\s:\s(\d+)\s(\d+)/i) { print "Chroma Angle : $2\n" }
    Should be this??:
    if ($line =~ /\s*APP>\sIntraAngleDelta\s:\s(\d+)\s(\d+)/i) { $values{'Luma Angle'} = $1; $values{'Chroma Angle'} = $2; }
      Thank you Marshall for your suggestion.
Re: Ordering of print statements
by BillKSmith (Monsignor) on Jun 02, 2018 at 14:44 UTC
    If all the lines are in an array, you could use grep to find each line. Arrange the tests in the order that you want the output.
    C:\Users\Bill\forums\monks>type eshan_k.pl use strict; use warnings; my @text = do{undef $/; <DATA>}; my $line; ($line) = grep {/LumaIntraMode/} @text; if ($line =~ /\s*APP>\s*LumaIntraMode\s:(\w+),\suse_intrabc:(\d)/i) { print "Luma_mode = $1\n"; } ($line) = grep {/\sAPP\>\sIntraAngleDelta\s\:\s/} @text; if ($line =~ /\s*APP>\sIntraAngleDelta\s:\s(\d+)\s(\d+)/i) { print "Luma Angle : $1\n" } ($line) = grep {/ChromaIntraMode/} @text; if ($line =~ /\s*APP>\s*ChromaIntraMode\s\:(\w+)/i) { print "Chroma mode: $1\n"; } ($line) = grep {/ALPHA_U\:/} @text; if ($line =~ /\s*ALPHA_U: (\d+), ALPHA_V: (\d+), CFL_ALPHA_SIGN: (\d+) +/i) { print "CFL : $3\n"; } ($line) = grep {/\sAPP\>\sIntraAngleDelta\s\:\s/} @text; if ($line =~ /\s*APP>\sIntraAngleDelta\s:\s(\d+)\s(\d+)/i) { print "Chroma Angle : $2\n" } __DATA__ APP> LumaIntraMode :D45_PRED, use_intrabc:0 AED> Dif: 80369151, Rng: 34624, Cnt: 10, Ret: 6 AED> Dif: 642953215, Rng: 52408, Cnt: 7, Ret: 13 APP> ChromaIntraMode :UV_CFL_PRED AED> Dif: 22413311, Rng: 36032, Cnt: 2, Ret: 5 AED> Dif: 115632447, Rng: 63232, Cnt: 11, Ret: 3 APP> ALPHA_U: 48, ALPHA_V: 0, CFL_ALPHA_SIGN: 5, CFL_ALPHA_IDX: 48 APP> IntraAngleDelta : 3 0 C:\Users\Bill\forums\monks>perl eshan_k.pl Luma_mode = D45_PRED Luma Angle : 3 Chroma mode: UV_CFL_PRED CFL : 5 Chroma Angle : 0

    Note: Minor corrections to several of the regexes and to the order of the tests.

    Bill
      Thank you Bill. It was very helpful.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1215681]
Front-paged by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2022-06-28 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (92 votes). Check out past polls.

    Notices?