Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Unable to obtain information from array

by Frits (Novice)
on Feb 14, 2017 at 09:49 UTC ( [id://1181964]=perlquestion: print w/replies, xml ) Need Help??

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

Dear all, this one is doing my head in and I'm stuck on grabbing the correct information. I'm using expect because I have to jump between different servers to obtain my information. When the script accesses is run from a sun solaris build device to a solaris build server this is working fine. When I'm doing the same method towards a Redhat Linux server. (So same commands) I'm getting an unexpected output in my array. The thing that I'm trying to accomplish is to get and check the md5 value of a file.

use strict; use Expect; use Term::ReadKey; use MIME::Lite; use File::Grep qw( fgrep fmap fdo ); use Time::Local; sub domainmd5 { $exp->clear_accum(); my ($exp, $domainexp, $pathfile, $file) = @_; my (@md5subout, $md5subout); $exp->expect( $timeout, [qr/$domainexp/ => sub {my $exp1md = shift +; $exp1md->send ("md5sum $pathfile\r");} ], ); $exp->expect( $timeout, [qr/$domainexp/ => sub {my $exp1md = shift +; @md5subout = $exp1md->exp_before(); } ] ); #chomp (@md5subout); print "element0::::::::\n ---- $md5subout[ +0]----\nEND element0 ::::::::\n\n"; print "element1::::::::\n ---- $md5subout[ +1]----\nEND element1 ::::::::\n\n"; print "element2::::::::\n ---- $md5subout[ +2]----\nEND element2 ::::::::\n\n"; my @greparray = grep ( /$file/, @md5subout ); print "GREPArray::::::::\n ---- @greparray +----\nEND ARRAY ::::::::\n\n"; my @splitarray = split (/ +/,$greparray[0]); print "SPLITArray::::::::\n ---- @splitarr +ay----\nEND ARRAY ::::::::\n\n"; my $md5subout = $splitarray[0]; print "Calculated at domainmd5::::-$md5sub +out-\n"; return ($md5subout); }

output in the array @md5subout is:

element0:::::::: in 734cc5ef88d4c8183a4cf1a16ac414c8 /c800-universalk9-mz.SPA.155-3.M5.bi +n [v790944@pdcd---- END element0 :::::::: element1:::::::: ---- ---- END element1 :::::::: element2:::::::: ---- ---- END element2 :::::::: GREPArray:::::::: in 734cc5ef88d4c8183a4cf1a16ac414c8 /tftpboot/out/c800-universalk9-mz.SP +A.155-3.M5.bin [v790944@pdcd---- END ARRAY :::::::: SPLITArray:::::::: in 734cc5ef88d4c8183a4cf1a16ac414c8 /tftpboot/out/c800-universalk9-mz.SPA +.155-3.M5.bin [v790944@pdcd---- END ARRAY :::::::: Calculated at domainmd5::::-SPLITArray:::::::: in 734cc5ef88d4c8183a4cf1a16ac414c8 /tftpboot/out/c800-universalk9-mz.SPA +.155-3.M5.bin [v790944@pdcd---- END ARRAY :::::::: Calculated at domainmd5::::-1a]~%-

The command run on the server shows the following output: As you can see the output is only in element0 and all is in one line within the array. Yet when printing this to the terminal I'm seeing more lines so there are special characters with the array. The only thing that is required for this part of the script is the md5 value of the file. At last you can see that the "Calculated value at domainmd5" shows the output: -1a]~%. This is not even in the array. Anyone an idea?

Replies are listed 'Best First'.
Re: Unable to obtain information from array
by Lotus1 (Vicar) on Feb 14, 2017 at 15:16 UTC
    sub domainmd5 { $exp->clear_accum(); my ($exp, $domainexp, $pathfile, $file) = @_;

    First you call a method on the global $exp then you immediately define a lexical variable with the same name and assign something to it that was passed into the function. It's hard to know what you are trying to do. How about providing a complete program. Also, Data::Dumper might be better for a simplified example to show what you are getting back.

Re: Unable to obtain information from array
by Frits (Novice) on Feb 16, 2017 at 15:05 UTC

    got this fixed by doing the following

    sub domainmd5 { my ($exp, $domainexp, $pathfile, $file) = @_; my (@md5subout, $md5subout); $exp->clear_accum(); $exp->expect( $timeout, [qr/$domainexp/ => sub {my $exp1md = shift +; $exp1md->send ("md5sum $pathfile\r");} ], ); $exp->expect( $timeout, [qr/$domainexp/ => sub {my $exp1md = shift +; @md5subout = $exp1md->exp_before(); } ] ); my @splitarray = split (/[\r\n]+/, $md5subout[0]); ### Multiple \ +r and \n detected in the output. my @greparray = grep ( /$file/, @splitarray ); my @foundmd5 = split(/ +/, $greparray[0]); $md5subout = @foundmd5[0]; print "Hier md5: $md5subout\n"; return ($md5subout); }
      my (@md5subout, $md5subout); ... $md5subout = @foundmd5[0];

      Using the same name for an array and a scalar works but is not considered a good practice since it is so confusing. You will get warnings from @foundmd5[0] since accessing an array element is a scalar and you are using '@'.

      Scalar value @foundmd5[0] better written as $foundmd5[0] at ...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found