Array of hash in perl does not work


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array of hash in perl does not work
# 1  
Old 02-20-2010
Array of hash in perl does not work

Hi ,

I have an input.txt file that i read

Code:
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 hash and then created a hash to get it in the following format
id => c1, class => nb etc

However when I try to access the hash using $hsh{id} I don't get anything back. But I iterated over it in hte following fashion and it seems to print the contents.
Code:
  while ( (my $k,my $v) = each %hsh ) {
      print "$k => $v\n";
      #assigning the key-value pair to a scalar
      #$rec->{$k} = $value;
      
  }

What am I doing wrong?
Also i am trying to add the hashes to an array @globalArr but ti does not seem to work either .
p.s i have attached my file .pl and input.txt for reference

Last edited by pludi; 02-20-2010 at 04:34 PM.. Reason: code tags, please..
# 2  
Old 02-20-2010
Quote:
Originally Posted by rsanjay
...
...when I try to access the hash using $hsh{id} I don't get anything back. ...
What am I doing wrong?
...
Hmm.. not sure where you got id from.

Code:
$ 
$ perl -le '%hsh = qw(c1 nb c2 b c3 nb c4 nb); print $hsh{c2}'
b
$

tyler_durden
# 3  
Old 02-20-2010
if you read the input.txt file. I am trying to read each line and create a key value pair of all the
[p]id= c1, class=nb, cx=100, cy=100, r=10
[/p]

I want to create a hash which will have each comma seperated pairs as a key value pair .

so if you look into the code i.e .pl file that i have attached my hash is not getting built .

i am trying to build my hash by first in the above line by using the split function on , and then removing the = with a global replace.putting them in an array and then assigning it into an hash .
# 4  
Old 02-20-2010
I suppose you need something like this:

Code:
perl -le'
  push @AoH, {/(\w+)\s*=\s*(\w+)/g} while <>;
  END {
      for $h (@AoH) {
          print map { $_, " => ", $h->{$_}, "\n" } sort keys %$h;
      }
  }' input.txt

With your input.txt it produces:

Code:
% perl -le'
  push @AoH, {/(\w+)\s*=\s*(\w+)/g} while <>;
  END {
      for $h (@AoH) {
          print map { $_, " => ", $h->{$_}, "\n" } sort keys %$h;
      }
  }' input.txt 
class => nb
cx => 100
cy => 100
id => c1
r => 10

class => b
cx => 150
cy => 130
id => c2
r => 10

class => nb
cx => 50
cy => 80
id => c3
r => 10

class => nb
cx => 120
cy => 200
id => c4
r => 10


from => c1
id => l1
to => c2

from => c1
id => l2
to => c3

from => c1
id => l3
to => c4

# 5  
Old 02-20-2010
Hi,

I am relatively new to perl . Is it right of me to ask you if you could kindly explain what does the second line do ?

and i created a .pl file of just this code and the cursor was just blinking . so could you just write the entire piece of code that you used to create a hash map from the input.txt

and one more thing is i need to create a data structure that holds key-value pair of all these lines and then i must be able to access the values of each line through that one data structure .

Thank you,
Sanjay
# 6  
Old 02-21-2010
That is a pretty nifty line indeed.
Code:
push @AoH, {/(\w+)\s*=\s*(\w+)/g} while <>;

It pushes a hash ref onto the array @AoH once per line of the input file, something like this, in effect, once the regex gets all matches from the line:
Code:
$hashref = {qw/id c1 class nb cx 100 cy 100 r 10/};
push @AoH $hashref;

The regex part,
Code:
 /(\w+)\s*=\s*(\w+)/g

gets the key and values in $1 and $2 and builds a temporary array to go inside the {} of the anonymous hash. The g at the end makes sure it gets all 5 pairs of key/values.

You can then access the data in @AoH like this:
Code:
# to get the first lines data:
print $AoH[0]{id},   "\n";
print $AoH[0]{class},"\n";
print $AoH[0]{cx},   "\n";
print $AoH[0]{cy},   "\n";
print $AoH[0]{r},    "\n";

The while <> just reads from the filenames on the command line. You'd replace that with something like
Code:
open(FILE,"<input.txt") or die "open failed: $!\n";

then use while <FILE>

--Kordaff--
# 7  
Old 02-21-2010
Well explained!
Thank you, Kordaff!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Programming

Perl Array within an hash

Hi All I have been using a curl code to output an hash that looks like this $VAR1 = { 'data'... (5 Replies)
Discussion started by: ab52
5 Replies

4. 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

5. 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

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

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

8. 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

9. 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

10. 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
Login or Register to Ask a Question