gavintokyo 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: Match into scalar
by shmem (Chancellor) on Jun 08, 2007 at 09:13 UTC
    Ahem. Your code (formatted)
    my $call = `mminfo -r 'volume,state,written,%used,space,pool,location, +ssinsert' -q 'ssinsert <= three week ago`; foreach ($call) { if ( $call =~ /Daily\S+\s/) { print "Daily true $_ .\n"; } else { print "not found\n "; } }

    raises a question. What is your backtick mminfo command giving back? a bunch of lines? If so, they are a single long string which you assign to $call. Then you iterate over that string, which gets aliased as $_ inside the loop, do a match and output just all the stuff your backtick command returned. The foreach loop is executed once.

    Want to iterate over the lines your mminfo yields? Break that string up with split. Then your code would look like

    my @calls = split /\r?\n/, `mminfo -r 'volume,state,written,%used,spac +e,pool,location,ssinsert' -q 'ssinsert <= three week ago`; foreach $call (@calls) { if ( $call =~ /Daily\S+\s/) { print "Daily true $call .\n"; } else { print "not found\n "; } }

    <update> You can also say @calls = `cmd`, the output of cmd will be split with $/ (see perlvar).</update>

    Another way to do that is using a pipe open:

    my $cmd = 'mminfo -r 'volume,state,written,%used,space,pool,location,s +sinsert' -q 'ssinsert <= three week ago'; open CMD, "$cmd |" or die "Can't run '$cmd': $!\n"; while (<CMD>) { if (/Daily\S+\s/) { print "Daily true $_"; } else { print "not found\n "; } }

    Use captures if you don't want the whole line but part of it, e.g.

    if (/Daily(\S+)\s/) { print "Daily true $1\n"; }

    See perlre, perlretut.

    And append updates to your post, don't change the original content without a proper note, since you render the replies already given to you useless. Read the PerlMonks FAQ, section Posting on PerlMonks, there How do I change/delete my post?.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Match into scalar
by blazar (Canon) on Jun 08, 2007 at 08:44 UTC
    I Trying to do a match on a scalar and put only the matching words into another scalar,

    I'm skipping your code because it doesn't seem to bear any relation to your actual question. However I notice an issue here... you talk about "matching words" which is a plural and putting them into "another scalar" which is a singular. Now, this is somewhat odd. Of course you may join the matched words on spaces and assign the resulting string to a scalar. Otherwise just use an array instead. For example, suppose you want to match "words" beginning with "a":

    my @matched = $str =~ /\ba\w+\b/g;

    or, as hinted above:

    my $matched = join ' ', $str =~ /\ba\w+\b/g;

    Else, I don't know how to help you further without you giving us any further detail.

Re: Match into scalar
by johngg (Canon) on Jun 08, 2007 at 08:58 UTC
    It looks like you are assigning all of the resulting lines of the mminfo command into a single scalar instead of an array. Try changing this line

    my $call = `mminfo -r 'volume,state,written,%used,space,pool,location, +ssinsert' -q 'ssinsert <= three week ago`;

    to

    my @call = `mminfo -r 'volume,state,written,%used,space,pool,location, +ssinsert' -q 'ssinsert <= three week ago`;

    and change the loop so that you are matching against each line returned which will be in $_, not $call.

    foreach ( @call ) { if ( /Daily\S+\s/ ) { print "Daily true $_ .\n"; } else { print "Not found\n"; } }

    I hope this is of use.

    Cheers,

    JohnGG

    Update: The OP seems to be a work in progress with silent updates occurring willy-nilly. The code problems I addressed in this reply no longer appear in the OP.

Re: Match into scalar
by GrandFather (Saint) on Jun 08, 2007 at 07:06 UTC
Re: Match into scalar
by naikonta (Curate) on Jun 08, 2007 at 07:47 UTC
    Are you aware that you are posting to the Seekers of Perl Wisdom? According to the docs:
    If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this is the place to ask.
    Well, I can see you seem to have a problem, but you don't have a question, so I don't know what kind of solution do you expect. Because you probably know what you mean, but I don't.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Match into scalar
by shandor (Monk) on Jun 08, 2007 at 18:36 UTC
    It looks like you're trying to separate tapes by pool. You could try something like this:
    my @daily_pool = `mminfo -av -r 'volume, savetime' -q 'pool=DailySecOf +fSite' -t '3 weeks ago'`; my @monthly_pool = `mminfo -av -r 'volume, savetime' -q 'pool=MonthlyO +ffSite' -t '3 weeks ago'`;
    There is a great Listserv forum for NetWorker where you can get all sorts of help with NetWorker and how to use programming languages around it. Check out these listserv archives for more information.
Re: Match into scalar
by Moron (Curate) on Jun 08, 2007 at 12:50 UTC
    use Data::Dumper; my $schema = { 'Daily' => [], 'Monthly' => [], 'Exception' => [] }; LINE: for my $line ( `mminfo etc.` ) { for my $case ( grep !/Exception/, keys %$schema ) { if ( $line =~ /\s$case/ ) { push @{$schema -> { $case }}, $line; next LINE; } } push @{$schema -> { 'Exception' }}, $line; } print Dumper $schema;
    __________________________________________________________________________________

    ^M Free your mind!