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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl - need help with 2 arrays to hash or 2d array?
# 1  
Old 09-01-2011
perl - need help with 2 arrays to hash or 2d array?

I have 2 arrays:

Code:
@array1 outputs the following:

1
1
1
2


@array2 outputs the following
 
A
B
C
D

How do I merge and print these 2 arrays and get the following logic:

Code:
A - 1
B - 1
C - 1
D - 2

D does not equal 1!

I basically want to find the best way to link these 2 arrays in that order and output them.
# 2  
Old 09-01-2011
Code:
perl -le'
  @a1 = ( 1, 1, 1, 2 );
  @a2 = ( A, B, C, D );
  
  print $a2[$_], " - ", $a1[$_]
    for ( 0 .. @a1 - 1 );
  '

# 3  
Old 09-02-2011
Quote:
Originally Posted by radoulov
Code:
perl -le'
  @a1 = ( 1, 1, 1, 2 );
  @a2 = ( A, B, C, D );
  
  print $a2[$_], " - ", $a1[$_]
    for ( 0 .. @a1 - 1 );
  '

Awesome. Thank you! Smilie
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. 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

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

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

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

6. Shell Programming and Scripting

Perl hash containing arrays

Hi, I am not that good at Perl. But here's what I wanna do. I want to create a hash where the keys would point to different arrays. This is what I have tried to do but in vain :( @arr=(1,2,3); @arr1=(3,2,1); %hashOfLists=(); $hashOfLists{Key1}=@arr."\n"; $hashOfLists{Key2}=@arr1."\n";... (3 Replies)
Discussion started by: King Nothing
3 Replies

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

8. Shell Programming and Scripting

Perl array of arrays

Hi, I am trying to assign an array as a value to one of the array element, I mean I have an array @KS and array @kr. I want array @KS to hold @kr as an element. So I am doin this $KS=@kr; But the value stored is number of elements in the @kr array. Can... (2 Replies)
Discussion started by: eamani_sun
2 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