Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Parsing and Obtaining Values from output

by Perllace (Acolyte)
on Apr 12, 2011 at 06:45 UTC ( [id://898856]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Parsing and Obtaining Values from output
by moritz (Cardinal) on Apr 12, 2011 at 06:49 UTC

    Your data example will be much easier to read if you put <code>...</code> tags around it.

    You can get the values within square brackets with

    if ($string =~ /\[([^\]]+)\]/) { print $1; }

    As for no. 2, please take a look at split.

Re: Parsing and Obtaining Values from output
by Ratazong (Monsignor) on Apr 12, 2011 at 06:59 UTC

    Hi!

    It would have been helpful if you have shown us the code you have already written. Otherwise it just could be general like "do it stepwise":

    • extract the data betwheen the [] using a regex, e.g. $s =~ /\[(.*?)\]/;
    • split the result using the |
    • do some comparision for the "highest value" - here I coulnd't find out what you want... where is 204 in your example?
    Of course there are many other ways to do it, and that's why it is so important for you to show us your tries - so we could give you specific help to the way you attack the problem ...

    HTH, Rata

    Update: replaced /[(.*?)]/ by /\[(.*?)\]/ to escape the metacharacters ... missed that point :-( Thanks AnomalousMonk for the correction!

      /[(.*?)]/ won't match anything having to do with square brackets. It is a character set (the  [] metacharacters) consisting of the  ( ) . * ? characters.

      Update: Something like  m{ \[ ([^]]*) \] }xms might be useful:

      >perl -wMstrict -le "my $s = 'foo [bar baz] quux [%^&*] boff [] biff'; my @bracketed = $s =~ m{ \[ ([^]]*) \] }xmsg; print qq{'$_'} for @bracketed; @bracketed = $s =~ m{ \[ [^]]* \] }xmsg; print qq{'$_'} for @bracketed; " 'bar baz' '%^&*' '' '[bar baz]' '[%^&*]' '[]'

      /[(.*?)]/ won't match anything having to do with square brackets. It is a character set (the  [] metacharacters) consisting of the  ( ) . * ? characters.

        Hi
        my @array1; my $data = "[0|0|{A=145,B=2,C=12,D=18}|!][0|0|{A=167,B=2,C=67,D=1 +7}|.1iit][196|0|{A=244,B=6,C=67,D=12}|10:48AM][204|0|{A=9,B=201,C=61, +D=11}|Calculator][66|0|{A=145,B=450,C=49,D=14}|phone]0|0|{A=145,B=2,C +=12,D=18}|!0|0|{A=167,B=2,C=67,D=17}|.1iit196|0|{A=244,B=6,C=67,D=12} +|10:48AM204|0|{A=9,B=201,C=61,D=11}|Calculator66|0|{A=145,B=450,C=49, +D=14}|phone"; my $high = 0; my @values = split(/\[([^\]]+)\]/,$data) ; print "Values is @values \n"; foreach(@values) { #I want the value that preceeds the first occurence of | in ea +ch array element, i.e 0,0,196,204,etc.. my ($conf,$rest)= split(/\|/,$_) ; print "Conf is $conf \n"; print "Rest is $rest \n"; push(@array1, $conf ); push (@array2, $rest); print "Array 1 is @array1 \n"; print "Array 2 is @array2 \n"; } $conf = highest(@array1); my $i=0; #I want the index value of the element that contains the highest conf +value..in this case 204 for (@myarray1) { last if $conf eq $_; $i++; }; print "$conf=$i\n"; #I want to print the rest of the string that was split in the sam +e index position, $rest = @array2[$i]; print "Rest is $rest \n"; # To get the higest conf value sub highest { my @data = @_; my $high = 0; for(@data) { $high = $_ if $_ > $high; } $high; }
        I tried this..but I'm not getting anywhere close to the output that I need... THanks, Perllace

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-29 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found