出力結果の解析方法と注意点

このQ&Aのポイント
  • 出力結果の解析方法と注意点
  • 出力結果が 'HASH(0x197fa1c)' のような形式で表示された場合、解析方法を紹介します。
  • 表示されたキー値が未初期化の値である場合があります。注意が必要です。
回答を見る
  • ベストアンサー

HASH(0xほげほげ)

出力が”HASH(0x197fa1c)”のような場合、どう解析してけばいいでしょうか? --------------------------------------------------------------------- #!C:\Perl\bin\perl.exe -w use strict; use Win32::OLE::OPC; #変数↓ my $items_stringP1 = "PLC0"; #OPCServer のデバイス名 my $items_stringP2 = "D"; #MELSECのデバイス名称 my $items_stringP3 = 0; #先頭アドレス my $maxcount = 1; #点数(5) #変数↑ my $opcintf = Win32::OLE::OPC->new('OPC.Automation', 'Takebishi.Dxp'); my $group = $opcintf->OPCGroups->Add('grp'); my $items = $group->OPCItems; my $items_stringP3_Min = $items_stringP3; my $count = 0; while ($count < $maxcount){ my $items_string=$items_stringP1.".".$items_stringP2.$items_stringP3; $items_stringP3 ++; $count ++; $items->AddItem($items_string, $opcintf); } print "********************"."\n"; print $items ."\n"; print "********************"."\n"; my $key; foreach ( keys %$items) { print "キー値 : $key\n"; } --------------------------------------------------------------------- 出力結果:↓ ******************** Win32::OLE::OPC::Items=HASH(0x197fa1c) ******************** Use of uninitialized value $key in concatenation (.) or string at C:\testPerl\OPC\OPC08.pl line 38. キー値 : Use of uninitialized value $key in concatenation (.) or string at C:\testPerl\OPC\OPC08.pl line 38. キー値 : Use of uninitialized value $key in concatenation (.) or string at C:\testPerl\OPC\OPC08.pl line 38. キー値 :

  • x001
  • お礼率6% (3/49)
  • Perl
  • 回答数4
  • ありがとう数11

質問者が選んだベストアンサー

  • ベストアンサー
  • kmee
  • ベストアンサー率55% (1857/3366)
回答No.4

#2の繰り返しになります。 > %$itemsのキーを取得するのはではないのですね。 は正しいのですが、その取得したキーを「使っていない」のです。 http://perldoc.jp/docs/perl/5.20.1/perlsyn.pod#Foreach32Loops マニュアル(の和訳)です。 > foreach ループは 通常のリスト値に対しての繰り返しを行い、変数 VAR に リストの要素を繰り返し一回に一つずつセットします。 > VAR が省略された場合には、$_ に値が設定されます。 とあります。 foreach ( keys %$items) は、この 「VAR が省略された場合」に相当します。 ということは、リスト値 keys %$items から取り出した値は $_ の設定されています。 その $_ に設定した値を使わずに、「 $key 」を出力しようとすれば、全然違うものが出力されるのは当り前です。 また、この前にある my $key ; という文と、このforeach は「無関係」 です。 修正するなら、実行ブロック内では $_ を使うか、foreachに使う変数として$keyを指定するか、です。 my $key; で宣言しただけでは、 $key の中身は「初期化していない値(uninitialized value)」です。 Perlの標準動作では、uninitialized valueを使って"キー値 : $key\n" 等としても、エラーになりません。 ですが、 perlに-wオプションを付けると、警告を出すようになります。 その警告が、Use of uninitialized value $key ~ です。

その他の回答 (3)

  • Tacosan
  • ベストアンサー率23% (3656/15482)
回答No.3

#2 に書かれていることは理解できていますか?

  • kmee
  • ベストアンサー率55% (1857/3366)
回答No.2

ちなみにエラーになっている理由は 「Use of uninitialized value $key 」(初期化されていない値 $key を使っている) からです。 my $key; と宣言しただけで、値を代入していません foreach ( keys %$items) { と、ループ用変数の指定無しにforeachを使うと、 $_ が使われます。$keyを勝手に使ったりはしません。

x001
質問者

補足

HASH(0x****)のキーとバリューを調べたいのですが、方法がわかりません。 %$itemsのキーを取得するのはではないのですね。

  • Tacosan
  • ベストアンサー率23% (3656/15482)
回答No.1

「解析」って, 具体的には何をしたいんでしょうか?

x001
質問者

補足

HASH(0x****)のキーとバリューを調べたいのですが、方法がわかりません

関連するQ&A

  • <Perl>参照配列の出力に失敗する。

    <Perl>参照配列の出力に失敗する。 お世話になります。 配列の出力部で以下のエラーが出力されます。 Use of uninitialized value in print at test2.pl line 12. -----コーディングは以下の通りです。----- #!C:\perl use strict; use warnings; my @l = (); #----------- #GetDataへCSVファイル名と、格納用配列を渡す #----------- my $cnt = &GetData("test.csv", \@l); print "COUNT -> ".$cnt; for(my $i=0; $i < $cnt; $i++){ print $l[$i]; } ################################################################## # 概   要:指定したCSVファイルをオープンしCSVデータを配列に取得する。 # パラメータ:ファイル名, CSVデータ格納用配列 # 戻 り 値:データ取得件数 ################################################################## sub GetData { my ($f, @bf) = @_; my $rcnt = 0; print "FILE NAME -> ".$f."\n"; if ( open(FP, "<${f}") ){ print "FILE OPEN -> success.\n"; @bf = split(/,/, <FP>); close(FP); $rcnt = @bf; print "CSV GET COUNT -> ".$rcnt."\n"; } return $rcnt; } -----実行結果は以下の通りです。----- D:\>perl test.pl FILE NAME -> test.csv FILE OPEN -> success. CSV GET COUNT -> 5 Use of uninitialized value in print at test2.pl line 12. Use of uninitialized value in print at test2.pl line 12. Use of uninitialized value in print at test2.pl line 12. Use of uninitialized value in print at test2.pl line 12. COUNT -> 5 -----CSVファイルの内容は以下の通りです。(ファイル名:test.csv)----- あいうえお,かきくけこ,さしすせそ,たちつてと,なにぬねの 配列の要素数が取れているので、配列内にデータは格納されているとは思っています。 出力方法をどのように正せばよいがご教示お願い致します。

    • ベストアンサー
    • Perl
  • DBMオープン時の警告

    既存のDBMファイルをオープンする処理で、 以下のような警告メッセージが出るのですが、 何が悪いのか、見当がつきません。 すみませんが、分かる方がいらっしゃいましたら、教えてください。 どの値が、なぜ、初期化されていないのでしょうか? Use of uninitialized value in dbmopen at b.pl line 4. Use of uninitialized value in null operation at b.pl line 4. オープン処理をするスクリプトは以下です。 #!/usr/bin/perl -w use strict; my %all = (); dbmopen %all, "all", undef or die "DBM open error"; foreach (keys %all) { print $_, "?t", $all{$_}, "?n"; } dbmclose %all; exit;

    • ベストアンサー
    • Perl
  • Perl MSSQLに接続してSQLのVer情報を

    すみません。素人です。 Perl MSSQLに接続してSQLのバージョン情報を取得したいです。 コードを実行すると下のようなエラーが発生します。 Can't use string ("1") as a HASH ref while "strict refs" in use at C:\testPerl\test.pl SQLServer Mangement Studioでクエリー(select @@VERSION)を実行すると下のような情報を取得できます。 ******** (列名なし) Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) Jul 9 2008 14:43:34 Copyright (c) 1988-2008 Microsoft Corporation Workgroup Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1) ********* どうも取得したデータ構造の指定がまずいように思いますが、どうすれば取得できますでしょうか? コード ------------------- use strict; use warnings; use Data::Dumper; use Win32::OLE; use Data::Dumper::Concise; use Win32::OLE::variant; use Win32::OLE::Const 'Microsoft ActiveX Data Objects 2.0 Library'; # DBサーバー名とDB名設定 my $server = "localhost"; my $db = ""; my $id = "sa"; my $ps = "testSa"; # エラー時に本処理を中止し、Perlがエラーメッセージを出力し、本プロセスが終了する。 Win32::OLE->Option(Warn => 3); # SQL Server 認証設定 my $connStr = "Provider=sqloledb;". "Data Source=$server;". "Initial Catalog=$db;". "User ID=$id;". "Password=$ps;"; # DB接続 my $objDB = Win32::OLE->new("ADODB.Connection"); $objDB->Open($connStr); $objDB->{Errors}->{Count} and die "cannot connect '$connStr'"; my $rs = Win32::OLE->new("ADODB.Recordset"); $rs->Open("select \@\@VERSION", $objDB); print "取りあえずなんか出力しようと試みる----------1\n"; print "$rs->{Fields}"."\n"; print "$rs->{Fields}->{Count}"."\n"; print "$rs->{Fields}->{Count}->{Item}->{1}"."\n"; while(!$rs->EOF ){ print "$rs->{Fields}->{Count}->{Value}"."$rs->{Fields}->{Item}->{Value}\n"; $rs->MoveNext(); } print "取りあえずなんか出力しようと試みる----------1\n"; ## DB切断 $objDB->Close();

  • hashをスカラー変数に渡す

    最近、perlを勉強し始めたのですが下記のようなサンプルコードを 見たことがあります。 hashをスカラー変数「$test」に渡している。そして「%$test」でhashに変換してeachで回している。最初から「%test」のようにしてeachで回せばよいと思うのですが実際の業務とかでhashをスカラー変数で受け取るようなことってありえるのでしょうか? my $test = { a=>'test_a', b=>'test_b', c=>'test_c' }; while( my ($key, $value) = each %$test ) { print $key ." : " . $value . "\n"; }

    • ベストアンサー
    • Perl
  • getc を使用した標準入力の取り扱いについて

    みなさま,お世話になります. 現在,perl を使用した簡単なスクリプトを作っていますが,期待する動作を得られず困っています.どなたか,アドバイスをいただけると大変助かります. よろしくお願いいたします. 【問題】 標準入力へ改行を含む複数行文字列を与え,perl スクリプト側で文字列処理をするような内容を考えていますが,行の文字数が大きい場合は正常に処理がされません. -------------------------------------------- ( Perl スクリプト) test.pl -------------------------------------------- #!/usr/bin/perl sub add_elements{ my($p) = @_; my $c, $str; my $i = 0; my $lf_count = 0; for ($i = 0; $i < length($p); $i++) { $c = substr($p, $i, 1); if (my $ascii = ord($c) == 10){ $lf_count++; print "+++ Debug\t Line Feed here\n"; } else { print "+++ Debug\t [$c]\t"; print ord($c); print "\n"; } } return $str; } sub row_count{ my($p) = @_; $p = $p =~ s/\n//g; print "row count :: "; print $p; print "\n"; return $p; } sub set_string{ my $strp; while(my $c = getc STDIN){ $strp .= $c; } print "set string: \n"; print $strp; print "\n"; return $strp; } my $source = &set_string; my $output; my $rcnt = &row_count($source); open(OUTFILE, ">> hoge.out"); if ($rcnt < 25) { print "+++ Debug\t Line Nums under 25.\n"; $output .= add_elements($source); } else { print "+++ Debug\t Line Nums over 25.\n"; } print OUTFILE $output; print OUTFILE "\n"; close OUTFILE; -------------------------------------------- -------------------------------------------- ( 入力用テキスト) source -------------------------------------------- A 1234567 B 12345678 C 123456789 D 1234567890 E 12345678901 -------------------------------------------- この Perl スクリプトに cat source の結果をパイプで入力して確認しました. 【テスト結果】 $ cat ./source | ./test.pl set string: A 1234567 B 12345678 C 123456789 D 123456789 row count :: 3 +++ Debug Line Nums under 25. +++ Debug [A] 65 +++ Debug [ ] 32 +++ Debug [1] 49 +++ Debug [2] 50 +++ Debug [3] 51 +++ Debug [4] 52 +++ Debug [5] 53 +++ Debug [6] 54 +++ Debug [7] 55 +++ Debug Line Feed here +++ Debug [B] 66 +++ Debug [ ] 32 +++ Debug [1] 49 +++ Debug [2] 50 +++ Debug [3] 51 +++ Debug [4] 52 +++ Debug [5] 53 +++ Debug [6] 54 +++ Debug [7] 55 +++ Debug [8] 56 +++ Debug Line Feed here +++ Debug [C] 67 +++ Debug [ ] 32 +++ Debug [1] 49 +++ Debug [2] 50 +++ Debug [3] 51 +++ Debug [4] 52 +++ Debug [5] 53 +++ Debug [6] 54 +++ Debug [7] 55 +++ Debug [8] 56 +++ Debug [9] 57 +++ Debug Line Feed here +++ Debug [D] 68 +++ Debug [ ] 32 +++ Debug [1] 49 +++ Debug [2] 50 +++ Debug [3] 51 +++ Debug [4] 52 +++ Debug [5] 53 +++ Debug [6] 54 +++ Debug [7] 55 +++ Debug [8] 56 +++ Debug [9] 57 これを見る限り,&set_string の中で 4 行目("D" で始まる行)の最後が読み取れていないことは判明しています.また,&set_string では getc STDIN で一文字ずつ読み取り,変数に append しています. 大変初歩的な質問かもしれませんが,ご教示いただければと思います. よろしくお願いいたします.

    • ベストアンサー
    • Perl
  • jcode.plのバグでしょうか?KCctchでの質問です。

    PerlのプログラムをKCatch.pmで調べていましたが。 次のような表示がされました。 jcode.plのバグでしょうか?プログラムに問題があるのでしょうか? お教えいただければ幸いです。 ../data/clients.txtが開けません。 Catch: Thu Jun 21 08:54:31 2001 -------------------------------------------------------------------------------- [jcode.pl:366:warn] Use of uninitialized value at jcode.pl line 366. > sub max { $_[ $[ + ($_[$[] < $_[$[+1]) ]; } [jcode.pl:362:warn] Use of uninitialized value at jcode.pl line 362. > $code = ('euc', undef, 'sjis')[($sjis<=>$euc) + $[ + 1]; -------------------------------------------------------------------------------- anquit.cgi with Perl 5.00502 for freebsd

    • ベストアンサー
    • Perl
  • perl print文で日本語表示するには

    perlのプログラムで use utf8が宣言されていない時(test1.pl)は、 print文で日本語が正しく表示されるのですが、 use utf8が宣言されている時(test2.pl)は、 print文で日本語が正しく表示されないかエラーになります。 encode('utf-8', $string)やencode('cp932', $string) も試してみましたがうまくいきませんでした。 どのようにすればよいでしょうか。 ---test1.pl------------------------------------------------ #!C:/perl/bin/perl use strict; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year += 1900; $mon += 1; my $string="$year年$mon月$mday日 $hour時$min分$sec秒\n"; print $string; ----------------------------------------------------------- 実行結果 OK 2018年12月26日 9時36分6秒 ----------------------------------------------------------- ---test2.pl------------------------------------------------ #!C:/perl/bin/perl use strict; use utf8; use Encode; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year += 1900; $mon += 1; my $string="$year年$mon月$mday日 $hour時$min分$sec秒\n"; print $string; #print encode('utf-8', $string); #print encode('cp932', $string); --------------------------------------------------- 実行結果 NG (PC1 Windows7 ActivePerl) Wide character in print at D:\test\test2.pl line 11. Global symbol "$year年" requires explicit package name (did you forget to declare "my $year年"?) at D:\test\test2.pl line 8. Global symbol "$mon月" requires explicit package name (did you forget to declare "my $mon月"?) at D:\test\test2.pl line 8. Global symbol "$mday日" requires explicit package name (did you forget to declare "my $mday日"?) at D:\test\test2.pl line 8. Global symbol "$hour時" requires explicit package name (did you forget to declare "my $hour時"?) at D:\test\test2.pl line 8. Global symbol "$min分" requires explicit package name (did you forget to declare "my $min分"?) at D:\test\test2.pl line 8. Global symbol "$sec秒" requires explicit package name (did you forget to declare "my $sec秒"?) at D:\test\test2.pl line 8. Execution of D:\test\test2.pl aborted due to compilation errors. ----------------------------------------------------- 実行結果 NG (PC2 Windows7 ActivePerl) Malformed UTF-8 character (unexpected continuation byte 0x94, with no precedingstart byte) at C:\test\test2.pl line 8. (略) Malformed UTF-8 character (unexpected continuation byte 0x95, with no precedingstart byte) at C:\test\test2.pl line 8. 2018 N12 26 9 5 4 b ----------------------------------------------------- 期待している実行結果 2018年12月26日 9時36分6秒 ----------------------------------------------------- よろしくお願いします。

    • ベストアンサー
    • Perl
  • エクセルのセルからのデータ取り込み

    取り込んだセルのデータを使いたいのですが。。。 特定のセルの指定方法がわるのでしょか? ここが悪いと思ってますが、 ↓ print "$arrary[0][0]\n"; -------------------------------------------------------------- use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; # die on errors... my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel # application or open new my $Book = $Excel->Workbooks->Open("C:\\DOCUMENTS\\test.xls"); # Excelのファイルをオープン my $Sheet = $Book->Worksheets(1); # ワークシート1を選択 my $array = $Sheet->Range("A8:B9")->{'Value'}; # 中身を取り出す $Book->Close; print "$arrary[0][0]\n";

  • 変数の表示の仕方(改行の扱い)

    perl初心者です。よろしくお願いします。 ひとつの変数で、print関数での表示とテキストエリアでの表示を しようとしているのですが、改行の扱いに困っています。 テキストエリアで<br>を表示させないようにすると、print関数の 方の改行がなくなるし、print関数の方に改行させると、テキスト エリアで<br>が表示されるし・・・。 print関数で改行、テキストエリアでは<br>が出ずに改行という 風にしたいのですが、どなたかご教授していただけませんか? よろしくお願いします。 #!/usr/local/bin/perl use strict; #データファイル my $datafile='./data.dat'; #ライブラリの読み込み require './jcode.pl'; my $input; #フォームデータの読み込み if ($ENV{REQUEST_METHOD} eq "POST"){ read (STDIN,$input,$ENV{CONTENT_LENGTH}); }else{ $input=$ENV{QUERY_STRING}; } #フォームデータのデコード my %form = (); my @pairs = split(/&/, $input); foreach my $pair ( @pairs ){ my( $name, $value ) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("H2", $1)/eg; #SJISコード変換 &jcode::convert(\$value, 'sjis'); $value =~ s/\r\n/<br>/g; $value =~ s/\r/<br>/g; $value =~ s/\n/<br>/g; $form{$name} = $value; } #データのオープン open(DAT,"+<$datafile"); flock(DAT, 2); #ファイル読み込み my $line = <DAT>; for ( split /\t/ , $line ) { my( $key , $value ) = $_ =~ /^([^=]*)=(.*)$/; #フォームデータの上書き $form{$key} ||= $value; } #データの書き込みとクローズ seek(DAT,0,0); $line = ''; while (my($key,$value) = each %form) { $line .= "${key}=${value}\t"; } print DAT $line; close(DAT); &header; print $form{A}; print "<br>"; print $form{B}; print "<br>"; print $form{C}; print "<br>"; print <<EOM; <b>[内容確認]</b> <form action="new.cgi" method="POST"> <INPUT TYPE="text" SIZE="27" name="A" value="$form{A}"><br> <INPUT TYPE="text" SIZE="90" name="B" value="$form{B}"><br> <TEXTAREA name="C" ROWS="2" COLS="27">$form{C}</TEXTAREA><br> <input type="submit" value="上記内容で送信"> </form> <br> </body> </html> EOM exit; #----------------------------------------------------------------------------------- # HTMLヘッダー #----------------------------------------------------------------------------------- 割愛

    • ベストアンサー
    • Perl
  • threads を使ったマルチスレッドのエラー

    threads のモジュールを使ってマルチスレッドのテストをすると下のようなエラーが 発生します。 何が原因なんでしょうか? Usage: threads->create(function, ...) at C:\testPerl\test9\test0006.pl line 11. use strict; use threads; use Data::Dumper; no strict "refs"; my $countM; for ($countM = 0; $countM < 5; $countM++){ my $test = "test".$countM; ${"thd".$countM}= threads->new(\&mtest($test)); ${"thd".$countM}->join; } print "test end.\n"; sub mtest { my $name = @_; print "$name"."\n"; threads->yield(); }