manupulating string having two rows

K

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 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.
 
B

Bart Van der Donck

king said:
$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,
 
T

tuser

king said:
$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";
 
M

Mumia W.

king said:
$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";
}
}
 
D

Dr.Ruud

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$/ ;
 
M

Mumia W.

Mumia said:
king said:
$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.
 
J

John W. Krahn

Dr.Ruud said:
Glenn Jackman schreef:

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
 
D

Dr.Ruud

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.
 
M

Mumia W.

Glenn said:
At 2006-06-07 05:13AM said:
$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.
 
D

Dr.Ruud

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') ;
 
B

Ben Morrow

Quoth "John W. Krahn said:
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
 
B

Brian McCauley

Dr.Ruud said:
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)..$/;
 
J

John W. Krahn

Dr.Ruud said:
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
 
C

Charles DeRykus

Dr.Ruud said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top