manupulating string having two rows

Discussion in 'Perl Misc' started by king, Jun 7, 2006.

  1. king

    king Guest

    $output = NVPCI32 Get Address: base=0xF0300060
    [0xE0300060] -> 0x00807805
    ....
    This whole thing is saved in a scalar variable say $output.
    Now i want to check the last two bits/characters that is 05 in this
    case, and if these last two characters
    are not 10, then
    put 78 of 0x00807805 in a another scalar variable.

    My problem-

    as $output is a scalar, and the string is arranged in a peculier
    manner, i am facing a lot of difficulties.
    can anybody suggest how to do this.
     
    king, Jun 7, 2006
    #1
    1. Advertising

  2. king wrote:

    > $output = NVPCI32 Get Address: base=0xF0300060
    > [0xE0300060] -> 0x00807805
    > ....
    > This whole thing is saved in a scalar variable say $output.
    > Now i want to check the last two bits/characters that is 05 in this
    > case,


    '05' are 2 bytes here (not bits).

    > and if these last two characters are not 10, then
    > put 78 of 0x00807805 in a another scalar variable.
    > My problem-
    > as $output is a scalar, and the string is arranged in a peculier
    > manner, i am facing a lot of difficulties.
    > can anybody suggest how to do this.


    The following should do the trick:

    #!perl
    use strict; use warnings;
    my $output= 'NVPCI32 Get Address: base=0xF0300060
    [0xE0300060] -> 0x00807805';
    my $var = 'nothing here!';
    my $check = '10';
    $var = $1 if $output =~ /(.{2})\Q$check\E$/;
    print $var;

    Hope this helps,

    --
    Bart
     
    Bart Van der Donck, Jun 7, 2006
    #2
    1. Advertising

  3. king

    tuser Guest

    king wrote:
    > $output = NVPCI32 Get Address: base=0xF0300060
    > [0xE0300060] -> 0x00807805
    > ....
    > This whole thing is saved in a scalar variable say $output.
    > Now i want to check the last two bits/characters that is 05 in this
    > case, and if these last two characters
    > are not 10, then
    > put 78 of 0x00807805 in a another scalar variable.
    >
    > My problem-
    >
    > as $output is a scalar, and the string is arranged in a peculier
    > manner, i am facing a lot of difficulties.


    You would need to describe more details about how the string is
    arranged, but from what I can tell from your message so far:

    use strict;
    use warnings;

    my $output = '[0xE0300060] -> 0x00807805';
    my $other_scalar = '';
    if ($output =~ /(..)$/ and $1 ne '10') { $other_scalar = '78' }
    print "\$other_scalar = '$other_scalar'\n";
     
    tuser, Jun 7, 2006
    #3
  4. king

    Mumia W. Guest

    king wrote:
    > $output = NVPCI32 Get Address: base=0xF0300060
    > [0xE0300060] -> 0x00807805
    > ....
    > This whole thing is saved in a scalar variable say $output.
    > Now i want to check the last two bits/characters that is 05 in this
    > case, and if these last two characters
    > are not 10, then
    > put 78 of 0x00807805 in a another scalar variable.
    >
    > My problem-
    >
    > as $output is a scalar, and the string is arranged in a peculier
    > manner, i am facing a lot of difficulties.
    > can anybody suggest how to do this.
    >


    Read "perldoc perlre."

    As you probably know, you can use the m// operator to match regular
    expressions. The /s option to m// allows you to match over several
    lines, so you would use m/<whatever-regex>/s.

    Then you have to figure out what goes into <whatever-regex>. A good idea
    is to make <whatever-regex> capture the data you want so that you can
    test it and possibly output it.

    if (m/<whatever-regex>/s) {
    if (<some-condition>) {
    print "<some-value>\n";
    }
    }
     
    Mumia W., Jun 7, 2006
    #4
  5. king

    Dr.Ruud Guest

    Glenn Jackman schreef:
    > king:


    >> $output = NVPCI32 Get Address: base=0xF0300060
    >> [0xE0300060] -> 0x00807805
    >> ....
    >> This whole thing is saved in a scalar variable say $output.
    >> Now i want to check the last two bits/characters that is 05 in this
    >> case, and if these last two characters
    >> are not 10, then
    >> put 78 of 0x00807805 in a another scalar variable.

    >
    > 2 ways:
    > $len = length $output;
    > my $other;
    > if (substr($output, $len-2) eq '05') {
    > $other = substr($output, $len-4, 2);
    > }


    You can use negative indexes:

    my $other ;
    $other = substr($output, -4, 2) if substr($output, -2) eq '05' ;


    > or
    > $output =~ /(\d\d)(\d\d)$/ and $2 eq '05' and my $other = $1;


    Don't put a declaration in a conditional zone.

    my $other ;
    $other = $1 if $output =~ /(\d\d)05$/ ;

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jun 7, 2006
    #5
  6. king

    Mumia W. Guest

    Mumia W. wrote:
    > king wrote:
    >> $output = NVPCI32 Get Address: base=0xF0300060
    >> [0xE0300060] -> 0x00807805
    >> ....
    >> [...]

    >
    > [...] The /s option to m// allows you to match over several
    > lines, so you would use m/<whatever-regex>/s.
    > [...]


    Silly me. The /s option is not needed here, but the general idea still
    works.
     
    Mumia W., Jun 7, 2006
    #6
  7. Dr.Ruud wrote:
    > Glenn Jackman schreef:
    >>
    >>or
    >> $output =~ /(\d\d)(\d\d)$/ and $2 eq '05' and my $other = $1;

    >
    > Don't put a declaration in a conditional zone.
    >
    > my $other ;
    > $other = $1 if $output =~ /(\d\d)05$/ ;


    It is only a problem if you use statement modifiers (like your example.)

    $output =~ /(\d\d)(\d\d)$/ and $2 eq '05' and my $other = $1;

    Is perfectly valid and will do the right thing with the lexical declaration.


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jun 7, 2006
    #7
  8. king

    Dr.Ruud Guest

    John W. Krahn schreef:

    > It is only a problem if you use statement modifiers [...]
    >
    > $output =~ /(\d\d)(\d\d)$/ and $2 eq '05' and my $other = $1;
    >
    > Is perfectly valid and will do the right thing with the lexical
    > declaration.



    I don't think 'statement modifiers' (trailing
    if/unless/while/until/for[each]) make the real difference. Aren't the
    and-s there, actually statement modifiers too?

    perl -MO=Deparse -e '
    $output =~ /(\d\d)05$/ and my $other = $1
    '
    my $other = $1 if $output =~ /(\d\d)05$/ ;

    "Don't put a declaration in a conditional zone." was not meant to say
    something was invalid, but as strong advice: Only declare early or late
    with a special reason.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jun 7, 2006
    #8
  9. king

    Mumia W. Guest

    Glenn Jackman wrote:
    > At 2006-06-07 05:13AM, king <> wrote:
    >> $output = NVPCI32 Get Address: base=0xF0300060
    >> [0xE0300060] -> 0x00807805
    >> ....

    > [...]
    > $output =~ /(\d\d)(\d\d)$/ and $2 eq '05' and my $other = $1;
    >


    But those are probably hex digits.
     
    Mumia W., Jun 7, 2006
    #9
  10. king

    Dr.Ruud Guest

    Mumia W. schreef:
    > Glenn Jackman:
    >> king:


    >>> $output = NVPCI32 Get Address: base=0xF0300060
    >>> [0xE0300060] -> 0x00807805
    >>> ....

    >>
    >> $output =~ /(\d\d)(\d\d)$/ and $2 eq '05' and my $other = $1;

    >
    > But those are probably hex digits.


    Sure they are.

    my $qrx2 = /( [[:xdigit:]]{2} )( [[:xdigit:]]{2} )$/x ;
    my $other ;
    $other = $1 if ($output =~ m/$qrx2/)
    and ($2 ne '10') ;

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jun 7, 2006
    #10
  11. king

    Ben Morrow Guest

    Quoth "John W. Krahn" <>:
    > Dr.Ruud wrote:
    > > Glenn Jackman schreef:
    > >>
    > >>or
    > >> $output =~ /(\d\d)(\d\d)$/ and $2 eq '05' and my $other = $1;

    > >
    > > Don't put a declaration in a conditional zone.
    > >
    > > my $other ;
    > > $other = $1 if $output =~ /(\d\d)05$/ ;

    >
    > It is only a problem if you use statement modifiers (like your example.)
    >
    > $output =~ /(\d\d)(\d\d)$/ and $2 eq '05' and my $other = $1;
    >
    > Is perfectly valid and will do the right thing with the lexical declaration.


    Could you explain a little further? I though perl converted statement
    modifiers into flow-control ops anyway; and B::Concise supports this:

    ~% perl -MO=Concise -e'$y and my $x=1'
    8 <@> leave[1 ref] vKP/REFC ->(end)
    1 <0> enter ->2
    2 <;> nextstate(main 1 -e:1) v ->3
    - <1> null vK/1 ->8
    4 <|> and(other->5) vK/1 ->8
    - <1> ex-rv2sv sK/1 ->4
    3 <$> gvsv(*y) s ->4
    7 <2> sassign vKS/2 ->8
    5 <$> const(IV 1) s ->6
    6 <0> padsv[$x:1,2] sRM*/LVINTRO ->7
    -e syntax OK
    ~% perl -MO=Concise -e'my $x=1 if $y'
    8 <@> leave[1 ref] vKP/REFC ->(end)
    1 <0> enter ->2
    2 <;> nextstate(main 1 -e:1) v ->3
    - <1> null vK/1 ->8
    4 <|> and(other->5) vK/1 ->8
    - <1> ex-rv2sv sK/1 ->4
    3 <$> gvsv(*y) s ->4
    7 <2> sassign vKS/2 ->8
    5 <$> const(IV 1) s ->6
    6 <0> padsv[$x:1,2] sRM*/LVINTRO ->7
    -e syntax OK

    In both cases the padsv/LVINTRO is on the RHS of an and...

    Ben

    --
    The cosmos, at best, is like a rubbish heap scattered at random.
    Heraclitus
     
    Ben Morrow, Jun 7, 2006
    #11
  12. Dr.Ruud wrote:

    > > king:

    >
    > >> $output = NVPCI32 Get Address: base=0xF0300060
    > >> [0xE0300060] -> 0x00807805
    > >> ....
    > >> This whole thing is saved in a scalar variable say $output.
    > >> Now i want to check the last two bits/characters that is 05 in this
    > >> case, and if these last two characters
    > >> are not 10, then put 78 of 0x00807805 in a another scalar variable.

    > >

    > my $other;
    > $other = $1 if $output =~ /(\d\d)05$/ ;


    I thought the OP wanted anthing but '10' not '05'

    Also at a guess the OP's data is not decimal numbers (i.e. \d) but
    upper case hex. We could restrict the regex to [0-9A-F] (a class which
    doubtless has a name but I can't be bothered to look it up) but why
    not:

    $other = $1 if $output =~ /(..)(?!10)..$/;

    Or, if you are happy for $other to be undef (rather that preserved) on
    failure to match.

    my ($other) = $output =~ /(..)(?!10)..$/;
     
    Brian McCauley, Jun 7, 2006
    #12
  13. Dr.Ruud wrote:
    > John W. Krahn schreef:
    >
    >>It is only a problem if you use statement modifiers [...]
    >>
    >>$output =~ /(\d\d)(\d\d)$/ and $2 eq '05' and my $other = $1;
    >>
    >>Is perfectly valid and will do the right thing with the lexical
    >>declaration.

    >
    >
    > I don't think 'statement modifiers' (trailing
    > if/unless/while/until/for[each]) make the real difference. Aren't the
    > and-s there, actually statement modifiers too?
    >
    > perl -MO=Deparse -e '
    > $output =~ /(\d\d)05$/ and my $other = $1
    > '
    > my $other = $1 if $output =~ /(\d\d)05$/ ;


    $ perl -MO=Deparse,-p -e'$output =~ /(\d\d)05$/ and my $other = $1'
    (($output =~ /(\d\d)05$/) and (my $other = $1));
    -e syntax OK



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jun 7, 2006
    #13
  14. Dr.Ruud wrote:
    > John W. Krahn schreef:
    >
    >> It is only a problem if you use statement modifiers [...]
    >>
    >> $output =~ /(\d\d)(\d\d)$/ and $2 eq '05' and my $other = $1;
    >>
    >> Is perfectly valid and will do the right thing with the lexical
    >> declaration.

    >
    >
    > I don't think 'statement modifiers' (trailing
    > if/unless/while/until/for[each]) make the real difference. Aren't the
    > and-s there, actually statement modifiers too?
    >
    > perl -MO=Deparse -e '
    > $output =~ /(\d\d)05$/ and my $other = $1
    > '
    > my $other = $1 if $output =~ /(\d\d)05$/ ;


    Not really. Deparse appears to just create an equivalent expression by
    transforming a trailing `and` to a statement modifier. Another `and`
    connective makes this clear:

    $ perl -MO=Deparse -e '$output =~ /(\d\d)05$/ and my $other = $1 and
    my $other2 = $1;'
    my $other2 = $1 if $output =~ /(\d\d)05$/ and my $other = $1;
    -e syntax OK

    With Deparse's -p option, the transformation doesn't happen at all:

    $ perl -MO=Deparse,-p -e '$output =~ /(\d\d)05$/ and my $other = $1'
    (($output =~ /(\d\d)05$/) and (my $other = $1));
    -e syntax OK


    >
    > "Don't put a declaration in a conditional zone." was not meant to say
    > something was invalid, but as strong advice: Only declare early or late
    > with a special reason.


    Agreed.

    --
    Charles DeRykus
     
    Charles DeRykus, Jun 8, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    4,236
    Oliver Wong
    Aug 4, 2006
  2. KK
    Replies:
    2
    Views:
    335
    Jim Langston
    Jul 12, 2006
  3. Jan Müller

    manupulating a xml-file

    Jan Müller, Mar 31, 2008, in forum: Java
    Replies:
    3
    Views:
    365
    Jan Müller
    Mar 31, 2008
  4. king
    Replies:
    3
    Views:
    385
    zentara
    Jun 2, 2006
  5. Jofio
    Replies:
    3
    Views:
    112
    BootNic
    Oct 9, 2005
Loading...

Share This Page