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

remove 16 Zeros

by eghdam (Initiate)
on Oct 13, 2015 at 08:47 UTC ( [id://1144651]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Gurus,

i am beginner and want to remove 16 zeros from thish String:

pvid 00c1be9a467335ce0000000000000000 >0000000000000000 if ($line =~ /pvid\+(.*$)"and remove 16 zeors) { $pvid = $1 }

only getting this 00c1be9a467335ce from String. appriciate Your Help

best regards

???
if ($line =~ /pvid\+(.*$)s{0000000000000000}{ $pvid = $1 }
???

Replies are listed 'Best First'.
Re: remove 16 Zeros
by flexvault (Monsignor) on Oct 13, 2015 at 09:23 UTC

    eghdam,

    if you know that the 'pvid' is always 32 characters then:

    my $pvid = "00c1be9a467335ce0000000000000000"; my $newpvid = substr( $pvid, 0, 16 ); print "\$newpvid: $newpvid\n";
    but also reading some more documentation wouldn't hurt!

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

      Hi! Yes thank u for Response. PVID is allways 32

      Charachters.

      My Script is:

      @output =`command` # Output looks like 7 lines and on line is the pvid:

      pvid 00c1be9a467335ce0000000000000000

      foreach $line (@output) {

      if ($line =~ /pvid ..... here i want to grep only the 32 charachters

      $pvid = $1 } i just want to get the $pvid.

        eghdam,

        Why make it so complicated ( however it is your script :-)

        ############# UNTESTED ################ use strict; my $pvid = ''; # initialize my @output = `command`; # Output looks like 7 lines and on [one] li +ne is the pvid: # pvid 00c1be9a467335ce0000000000000000 # I use qx/command/; # easier to see in cod +e for me! foreach my $line ( @output ) { # print "$line\n"; ## If not working, use print to figure out w +hat's happening if ( substr( $line, 0, 5 ) eq 'pvid ' ) { $pvid = substr( $line, 6, 16 ) # or '32' as you require last; } } if ( $pvid eq '' ) { ... } ## Generate warning or do some error h +andling!

        I'm coding to my style, but you are free to chose what works for you. But remember if you have to go back to the code and update, make it as easy to understand as possible. Don't spend 2 hours figuring out what you did and then make a 2 line update.

        Good Luck...Ed

        "Well done is better than well said." - Benjamin Franklin

Re: remove 16 Zeros
by mr_ron (Chaplain) on Oct 13, 2015 at 14:34 UTC

    Perl regular expressions can count as explained in perlretut - Perl regular expressions tutorial in the section on on matching repetitions. So /0{16}/ matches exactly 16 zeroes.

    use strict; use warnings; my $line = "pvid 00c1be9a467335ce0000000000000000"; my $pvid; if ($line =~ /pvid\s+(.*)0{16}$/) { $pvid = $1 } print "$pvid\n"; __END__ 00c1be9a467335ce

    Since the string you are trying to extract consists of hex digits, a slight improvement on the regex might be:  /pvid\s+([[:xdigit:]]*)0{16}$/ . Go to perldoc.perl.org and search for "character class" to learn more ...

    Ron

      Oh yeah. that works fine. I have to find out about 1000 PVID's from DISKS. So i list DISKS and read the Configuration of DISK's, where i must grep the string pvid and filter only he pvid.

      So this is the better solution.

      thank u Ron :-)

Re: remove 16 Zeros
by ww (Archbishop) on Oct 13, 2015 at 12:20 UTC

    Frankly, I find your question hard to understand... and not just because you failed to use proper markup -- i.e., code tags (<c> ... </c>) around data and code. Please see the markup instructions at the text entry box or read Writeup Formatting Tips).

    But addressing what I surmise you may think is a partial approach to your problem (including the possibility of a variable char count before your 16 zeros), the following should do the job:

    #! /usr/bin/perl -w use strict; # 1144651 my $pvid; my $line = "pvid 00c1be9a467335ce0000000000000000"; if ($line =~ /(pvid .*?)0000000000000000$/ ) { $pvid = $1; } print $pvid;

    Your "partial approach"

    " if ($line =~ /pvid\+(.*$)s{0000000000000000}{ $pvid = $1 }"

    1. provides no mechanism to capture the initial four chars, "pvid", nor any limitation on how many times to accept what the dot represents (a single instance of anything, char, num, symbol....).
    2. In a regex, the "$" marks the end of an input line, so an input with additional data -- if you had any limit on the .* (anything, any number of times) -- would never match.
    3. Then -- as written -- your curly braces enclosing the zeros are a quantifier for how many times to match "s"
    4. Finally, you don't terminate the "partial approach's" regex with a closing slash, "/".

    As a prophet once said, you can't just make stuff up and expect the computer to understand.

    Note also the "my $pvid which makes the variable global, and allows perl to pass its content back to the code outside the if clause.

    As others observed, best you read docs such as perldoc perlre and perldoc perlretut... available at your own console... and perhaps tutorials (Tutorials) here such as Getting Started with Perl.

    Updated: textual typos corrected and formatting changed for readability.

      thank u very much. I am just in Process to get to manual. again thank u.

        Your installation of Perl includes a "manual". Use the perldoc command to access it.

        Also, all of the "official" Perl documentation is available online at http://perldoc.perl.org

Re: remove 16 Zeros
by Anonymous Monk on Oct 13, 2015 at 08:55 UTC
      So cool!

        Folk, i need help again :-(

        how do i save this value (pvid) to a sub function in perl? there are keys defined in the sub function and they are pushed and later prints in html:

        push(@{$lsmap_slots{$key}}, $svsa); push(@{$lsmap_slots{$key}}, $physloc); push(@{$lsmap_slots{$key}}, $vtd); push(@{$lsmap_slots{$key}}, $lun); push(@{$lsmap_slots{$key}}, $backing); push(@{$lsmap_slots{$key}}, $bdphysloc); push(@{$lsmap_slots{$key}}, $status);

        how do i push pvid in this sub function?>

        i know, i have to read but Perl is very heavy for me at the Moment.

        Kind Regards

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-16 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found