Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > manupulating string having two rows

Reply
Thread Tools

manupulating string having two rows

 
 
king
Guest
Posts: n/a
 
      06-07-2006
$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.

 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      06-07-2006
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

 
Reply With Quote
 
 
 
 
tuser
Guest
Posts: n/a
 
      06-07-2006
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";

 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      06-07-2006
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";
}
}


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      06-07-2006
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."


 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      06-07-2006
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.
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      06-07-2006
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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      06-07-2006
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."


 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      06-07-2006
Glenn Jackman wrote:
> At 2006-06-07 05:13AM, king <(E-Mail Removed)> 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.

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      06-07-2006
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 = /( [[digit:]]{2} )( [[digit:]]{2} )$/x ;
my $other ;
$other = $1 if ($output =~ m/$qrx2/)
and ($2 ne '10') ;

--
Affijn, Ruud

"Gewoon is een tijger."


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
manupulating a xml-file Jan Müller Java 3 03-31-2008 02:38 PM
manupulating data from database KK C++ 2 07-12-2006 11:14 AM
perl TK related (manupulating the output text area) king Perl Misc 3 06-02-2006 02:29 PM
Having a Problem Trying to call two html iles to be loaded into two different frames Jofio Javascript 3 10-09-2005 09:50 AM
script for moving rows up and down and traverse thru rows of HTML table Subba Rao via DotNetMonster.com ASP .Net 0 03-19-2005 06:46 AM



Advertisments