Sponsored Content
Full Discussion: Perl Array within an hash
Top Forums Programming Perl Array within an hash Post 302800181 by Skrynesaver on Monday 29th of April 2013 07:04:18 AM
Old 04-29-2013
You are putting a hash ref into an array as the only element with this line
Code:
push @myarray,$perl_data->{'data'};

Instead try examining the structure the JSON decode module returns.
Code:
$perl_data # a hashref
$perl_data->data # an array ref to an array of hashes

so
Code:
for my $host (@{$perl_data->{data}}){
   print "$host->{hostname}\n";
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

hash,array and perl

Hi,i have a code fragment below. %tag = (); #line 1 $tag{'info'} = $datastring; #line 2 $resp = $ua->request( #$ua is a user agent POST 'http://10.2.3.0' , Content_Type => application/x-www-form-urlencoded Content => #line 3 I am not sure of what the code... (3 Replies)
Discussion started by: new2ss
3 Replies

2. Shell Programming and Scripting

perl array question from going through hash

suppose my @{$data1{$callid}}; cotains one two three three five six one two three of random patterns but each item is separated by white space or tab, Below code extract and get rid of the whitespace perfectly so that it shows now like this onetwothree threefivesix... (2 Replies)
Discussion started by: hankooknara
2 Replies

3. Shell Programming and Scripting

PERL, push to hash of array problem

$key = "a"; $value = "hello"; %myhash = {} ; push @{ myHash{$key} }, $hello; print $myHash{$key}."\n"; this script prints "hello" but has following error message. Reference found where even-sized list expected at ./test line 5. can any one help me to fix this problem?? (3 Replies)
Discussion started by: bonosungho
3 Replies

4. Shell Programming and Scripting

Array of hash in perl does not work

Hi , I have an input.txt file that i read node: id= c1, class=nb, cx=100, cy=100, r=10 node: id=c2, class=b, cx=150, cy=130, r=10 node: id=c3, class=nb, cx=50, cy=80, r=10 node: id=c4, class=nb, cx=120, cy=200, r=10 i split over , and = to create a global array and then passed it to a... (6 Replies)
Discussion started by: rsanjay
6 Replies

5. Shell Programming and Scripting

perl - need help with 2 arrays to hash or 2d array?

I have 2 arrays: @array1 outputs the following: 1 1 1 2 @array2 outputs the following A B C D (2 Replies)
Discussion started by: streetfighter2
2 Replies

6. Shell Programming and Scripting

perl Can't coerce array into hash at

Hi guys I have this part of a perl script that returns and odd error if ($args{software}) { print " @DISTFILE_GROUPS $output->{distfile_groups}->{ get_rdist_groups}\n"; and the error is Can't coerce array into hash at i've never seed this error before, any ideas thanks... (0 Replies)
Discussion started by: ab52
0 Replies

7. Shell Programming and Scripting

array of hash - perl

How do I get the unique hashes from an array of hashes? @ar1 = ( {a=>1,b=>2}, {c=>3,d=>4},{a=>1,b=>2});I need : @ar2 = ( {a=>1,b=>2}, {c=>3,d=>4});Thanks. (2 Replies)
Discussion started by: shellwell
2 Replies

8. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

9. Shell Programming and Scripting

Perl : Assigning multile hash values to a single array

I know that @food = %fruit; Works. But how do I assign %fruit and %veggies to @food ? (2 Replies)
Discussion started by: popeye
2 Replies

10. Shell Programming and Scripting

Perl hash of hashes anonymous array

Hello experts. I'm having problems with a snippet of code. I was hoping to get help/advice to correct. A file that this script parses has changed to the point where I can no longer use a scalar, it looks as though I need to create an array for a hash of hashes below. The first output of... (1 Reply)
Discussion started by: timj123
1 Replies
More(3pm)						User Contributed Perl Documentation						 More(3pm)

NAME
Carp::Assert::More - convenience wrappers around Carp::Assert VERSION
Version 1.12 SYNOPSIS
use Carp::Assert::More; my $obj = My::Object; assert_isa( $obj, 'My::Object', 'Got back a correct object' ); DESCRIPTION
Carp::Assert::More is a set of wrappers around the Carp::Assert functions to make the habit of writing assertions even easier. Everything in here is effectively syntactic sugar. There's no technical reason to use assert_isa( $foo, 'HTML::Lint' ); instead of assert( defined $foo ); assert( ref($foo) eq 'HTML::Lint' ); other than readability and simplicity of the code. My intent here is to make common assertions easy so that we as programmers have no excuse to not use them. CAVEATS
I haven't specifically done anything to make Carp::Assert::More be backwards compatible with anything besides Perl 5.6.1, much less back to 5.004. Perhaps someone with better testing resources in that area can help me out here. SIMPLE ASSERTIONS
assert_is( $string, $match [,$name] ) Asserts that $string matches $match. assert_isnt( $string, $unmatch [,$name] ) Asserts that $string does NOT match $unmatch. assert_like( $string, qr/regex/ [,$name] ) Asserts that $string matches qr/regex/. assert_defined( $this [, $name] ) Asserts that $this is defined. assert_nonblank( $this [, $name] ) Asserts that $this is not blank and not a reference. NUMERIC ASSERTIONS
assert_integer( $this [, $name ] ) Asserts that $this is an integer, which may be zero or negative. assert_integer( 0 ); # pass assert_integer( -14 ); # pass assert_integer( '14.' ); # FAIL assert_nonzero( $this [, $name ] ) Asserts that the numeric value of $this is not zero. assert_nonzero( 0 ); # FAIL assert_nonzero( -14 ); # pass assert_nonzero( '14.' ); # pass Asserts that the numeric value of $this is not zero. assert_positive( $this [, $name ] ) Asserts that the numeric value of $this is greater than zero. assert_positive( 0 ); # FAIL assert_positive( -14 ); # FAIL assert_positive( '14.' ); # pass assert_nonnegative( $this [, $name ] ) Asserts that the numeric value of $this is greater than or equal to zero. Since non-numeric strings evaluate to zero, this means that any non-numeric string will pass. assert_nonnegative( 0 ); # pass assert_nonnegative( -14 ); # FAIL assert_nonnegative( '14.' ); # pass assert_nonnegative( 'dog' ); # pass assert_negative( $this [, $name ] ) Asserts that the numeric value of $this is less than zero. assert_negative( 0 ); # FAIL assert_negative( -14 ); # pass assert_negative( '14.' ); # FAIL assert_nonzero_integer( $this [, $name ] ) Asserts that the numeric value of $this is not zero, and that $this is an integer. assert_nonzero_integer( 0 ); # FAIL assert_nonzero_integer( -14 ); # pass assert_nonzero_integer( '14.' ); # FAIL assert_positive_integer( $this [, $name ] ) Asserts that the numeric value of $this is greater than zero, and that $this is an integer. assert_positive_integer( 0 ); # FAIL assert_positive_integer( -14 ); # FAIL assert_positive_integer( '14.' ); # FAIL assert_positive_integer( '14' ); # pass assert_nonnegative_integer( $this [, $name ] ) Asserts that the numeric value of $this is not less than zero, and that $this is an integer. assert_nonnegative_integer( 0 ); # pass assert_nonnegative_integer( -14 ); # pass assert_nonnegative_integer( '14.' ); # FAIL assert_negative_integer( $this [, $name ] ) Asserts that the numeric value of $this is less than zero, and that $this is an integer. assert_negative_integer( 0 ); # FAIL assert_negative_integer( -14 ); # pass assert_negative_integer( '14.' ); # FAIL REFERENCE ASSERTIONS
assert_isa( $this, $type [, $name ] ) Asserts that $this is an object of type $type. assert_nonempty( $this [, $name ] ) $this must be a ref to either a hash or an array. Asserts that that collection contains at least 1 element. Will assert (with its own message, not $name) unless given a hash or array ref. It is OK if $this has been blessed into objecthood, but the semantics of checking an object to see if it has keys (for a hashref) or returns >0 in scalar context (for an array ref) may not be what you want. assert_nonempty( 0 ); # FAIL assert_nonempty( 'foo' ); # FAIL assert_nonempty( undef ); # FAIL assert_nonempty( {} ); # FAIL assert_nonempty( [] ); # FAIL assert_nonempty( {foo=>1} );# pass assert_nonempty( [1,2,3] ); # pass assert_nonref( $this [, $name ] ) Asserts that $this is not undef and not a reference. assert_hashref( $ref [,$name] ) Asserts that $ref is defined, and is a reference to a (possibly empty) hash. NB: This method returns false for objects, even those whose underlying data is a hashref. This is as it should be, under the assumptions that: (a) you shouldn't rely on the underlying data structure of a particular class, and (b) you should use "assert_isa" instead. assert_listref( $ref [,$name] ) Asserts that $ref is defined, and is a reference to a (possibly empty) list. NB: The same caveat about objects whose underlying structure is a hash (see "assert_hashref") applies here; this method returns false even for objects whose underlying structure is an array. SET AND HASH MEMBERSHIP
assert_in( $string, @inlist [,$name] ); Asserts that $string is defined and matches one of the elements of @inlist. @inlist must be an array reference of defined strings. assert_exists( \%hash, $key [,$name] ) assert_exists( \%hash, @keylist [,$name] ) Asserts that %hash is indeed a hash, and that $key exists in %hash, or that all of the keys in @keylist exist in %hash. assert_exists( \%custinfo, 'name', 'Customer has a name field' ); assert_exists( \%custinfo, [qw( name addr phone )], 'Customer has name, address and phone' ); assert_lacks( \%hash, $key [,$name] ) assert_lacks( \%hash, @keylist [,$name] ) Asserts that %hash is indeed a hash, and that $key does NOT exist in %hash, or that none of the keys in @keylist exist in %hash. assert_lacks( \%users, 'root', 'Root is not in the user table' ); assert_lacks( \%users, [qw( root admin nobody )], 'No bad usernames found' ); UTILITY ASSERTIONS
assert_fail( [$name] ) Assertion that always fails. "assert_fail($msg)" is exactly the same as calling "assert(0,$msg)", but it eliminates that case where you accidentally use "assert($msg)", which of course never fires. COPYRIGHT
Copyright (c) 2005 Andy Lester. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. ACKNOWLEDGEMENTS
Thanks to Bob Diss, Pete Krawczyk, David Storrs, Dan Friedman, and Allard Hoeve for code and fixes. perl v5.8.8 2005-10-14 More(3pm)
All times are GMT -4. The time now is 01:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy