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

Hello Experts, kindly help me in extracting the value "34" from the below given text. O1/APZ "GWHMSC2_R132_EP" 3233 150407 1448 BACKUP INFORMATION FAULT FAULT CODE 34 need to set a flag too,which results to false in case the value is other than "34". my apologies for not mentioning my requirements in a proper methodology. regards Vijay

Replies are listed 'Best First'.
Re: reading a value from a given text
by ww (Archbishop) on Apr 07, 2015 at 12:22 UTC
Re: reading a value from a given text
by toolic (Bishop) on Apr 07, 2015 at 12:18 UTC
    • Edit your post to add "code" tags around your input text: Writeup Formatting Tips
    • Read perlintro and write some code.
    • Post back if you have more specific questions regarding your code.
Re: reading a value from a given text
by boftx (Deacon) on Apr 07, 2015 at 16:13 UTC

    Assuming you are reading a log file and are interested in the value of FAULT CODE and that the value is always at the end of the line if it is present at all:

    #!/usr/bin/perl use strict; use warnings; my $input = q{O1/APZ "GWHMSC2_R132_EP" 3233 150407 1448 BACKUP INFORMA +TION FAULT FAULT CODE 34}; # initialize this to a numeric value so you don't get warnings my $fault_code = 0; if ( $input =~ m/FAULT CODE (\d+)$/ ) { $fault_code = $1; } my $flag = ( $fault_code == 34 ); print "FAULT CODE is 34\n" if $flag; exit; __END__
    The use of a flag is somewhat redundant since you can always do a test for the value where needed.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
      Thank you so much for helping me out in getting the scipt. My apologies for not mentioning it properly,as i wanted to read the value "34" from the print output. This numeric value of FAULT CODE keeps changing most of the time,i wanted to read the value "34" only else the script should return as false or fc=34 not found. i hope i am clear this time. Regards Vijay
Re: reading a value from a given text
by choroba (Cardinal) on Apr 07, 2015 at 12:32 UTC
    my $string = 'O1/APZ "GWHMSC2_R132_EP" 3233 150407 1448 BACKUP INFORMA +TION FAULT FAULT CODE 34'; my $thirty_four = substr($string, 18, 1) . substr($string, 33, 1); my $flag = (34 == $thirty_four);

    Or did you have a different way of extracting "34" from the string?

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      "...a different way of extracting "34"...

      my $flag = grep { /(34)$/ } split / /, $string;

      Update: I regret this crap. Had a bad day, or and it is to too late, sorry.

      Update 2: my $flag = ( ( split / /, $string )[-1] == 34 );

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»