Perl question - looping through an array of hashrefs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl question - looping through an array of hashrefs
# 1  
Old 03-17-2004
Perl question - looping through an array of hashrefs

I have an array of hashrefs that look like the following:

Code:
my @LAYOUT = (
  {SQL_1    =>      "select count (*) FROM prospect
                            WHERE PROCESS_DATE = To_date('INSERT_DATE_HERE', 'mm/dd/yyyy')
                             and tiff_filename is not null
                             AND (COST_CENTER BETWEEN 6 AND 69)
                             and media_source = 'DM'
                             and lead_type = 'Q'
                             and vendor = 'ABC'"},
  {SQL_2     =>      "select count (*)
                             from prospect
                             WHERE PROCESS_DATE = To_date('INSERT_DATE_HERE', 'mm/dd/yyyy')
                             and tiff_filename is not null
                             AND (COST_CENTER BETWEEN 6 AND 69)
                             and media_source = 'TM'
                             and lead_type = 'L'
                             and vendor = 'ABC'"},
  {SQL_3    =>      "select count (*)
                            from prospect
                            WHERE PROCESS_DATE = To_date('INSERT_DATE_HERE', 'mm/dd/yyyy')
                             and tiff_filename is not null
                             AND (COST_CENTER BETWEEN 6 AND 69)
                             and media_source = 'LQ'
                             and lead_type = 'R'
                             and vendor = 'XYZ'"},

and so on...


);

I'm trying to loop through each one and assign scalar variables to the key and the value.

$sql_file should be the sql file name (SQL_1, SQL_2, SQL_3, etc.)
$sql_text should be the sql itself (select count(*)...)

I'm using a foreach loop. I've tried this to assign the scalars, but am not having any luck:

Code:
foreach my $r(@LAYOUT) {
    $sql_file = (keys%$r)->[1];
    $sql_text = (values%$r)->[1];
    print $sql_file;
    print $sql_text;
}

What am I doing wrong or how can I re-write this to ge the right information?

Thanks.
# 2  
Old 03-17-2004
just a question why use an array to hold a single level of hashes.

why dont you just use an anon hash?

that way you can go

while ( ($key, $value) = each %hash ) {
print "$key => $value\n";
}
# 3  
Old 03-19-2004
Quote:
Originally posted by Optimus_P
just a question why use an array to hold a single level of hashes.

why dont you just use an anon hash?

that way you can go

while ( ($key, $value) = each %hash ) {
print "$key => $value\n";
}
I hear ya. Unfortunately, this is a program that I don't call all the shots on. This is how the boss wants it done (I assume he has other plans in mind for the future with it of which an array of hashrefs is one critical part).

Anyhoo, here's how you do it:

Code:
foreach my $r(@LAYOUT) { 
    $sql_file = (keys %$r)[0];  # corrected
    $sql_text = (values %$r)[0]; # corrected
    print $sql_file; 
    print $sql_text; 
}


Last edited by kregh99; 03-19-2004 at 02:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Looping an array of 2d arrays in C

Le sigh... Hopefully this will be the last time I have to ask for help on this topic. For a while now I've been working with a 1d array that holds 2d arrays. For reference you can view here. Now I'm just trying to loop through the elements with the following: #include <stdio.h> void... (3 Replies)
Discussion started by: Azrael
3 Replies

2. UNIX for Dummies Questions & Answers

Looping through the contents of array for output name

Hi all, I am trying to loop through the string contents of an array, to add it during the saving of the output files. I am trying this code to print each column and save it to unique file name, but it doesn't work. Thanks for any help. fnam=(japan usa uk) alldata.dat contained sample data... (1 Reply)
Discussion started by: ida1215
1 Replies

3. UNIX for Dummies Questions & Answers

perl array filling *NOOB question*

First time poster here and I'm pretty much a total noob with UNIX and Perl. So please bear with me. With Perl, I'm trying to fill an array with data that is in a CSV file. I would like to fill the array with only one of the columns in the CSV file. I have a file called data.csv: ... (2 Replies)
Discussion started by: WongSifu
2 Replies

4. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

5. Homework & Coursework Questions

Help with looping question

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I need to write a script that accepts 3 filenames as command line parameters and test if they are executable and... (1 Reply)
Discussion started by: tommynewb
1 Replies

6. UNIX for Dummies Questions & Answers

Help in Array looping and creating multiple lines

hi Gurus, I'm a newbie in scripting please check my script if this is correct. I think there's something wrong with it but I;m not sure. I'm trying to create multiple lines using awk from external xml files but i want to add additonal info in the data manually Since i don't knwo how to... (0 Replies)
Discussion started by: sexyTrojan
0 Replies

7. Shell Programming and Scripting

KSH script not looping through array

Hi All, I'm trying to get a script to loop through an array. The array is basically a list of .zip files. I'd like the script to loop through and unzip the zip files contained in the zip file list. When I run the script, it unzip the first zip file correctly, and then stops Any thoughts? Here's... (2 Replies)
Discussion started by: kelldan
2 Replies

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

9. Shell Programming and Scripting

Looping question

Hi, I have series of data stored in a variable xyz: (between 0 and 100) example: 20 45 98 21..... I need to find if there is/are any occurance of data > 95 Not sure what kind of looping is required to check. Please help. thanks (2 Replies)
Discussion started by: hemangjani
2 Replies

10. UNIX for Dummies Questions & Answers

looping question

I am writing a simple script and want to keep the user in a fuction until they are ready to get out. For some (probably stupid) reason, it doesn't seem to be working. You guys see anything that I'm overlooking? crsd() {until do /home/wcs3611.crsdtmp.sh echo 'run another? \c' ... (1 Reply)
Discussion started by: hedrict
1 Replies
Login or Register to Ask a Question