Need Help with putting Hashes in a two-dimensional array 
Author Message
 Need Help with putting Hashes in a two-dimensional array

Hi

Can anybody please help me? Please consider this

---
$v1 =
"&BASKETZEILE1=BZ_STATUS=0&&BZ_AR_INTNUM=1&&BZ_AR_PREIS=46100&&BZ_MENGE=100&&BZ_RABATT=00&&BZ_RABATTBETRAG=00&&BZ_TOTAL=46100"

."&BASKETZEILE2=BZ_STATUS=0&&BZ_AR_INTNUM=3&&BZ_AR_PREIS=11570000&&BZ_MENGE=200&&BZ_RABATT=00&&BZ_RABATTBETRAG=00&&BZ_TOTAL=23140000"

---

I'd llike to split that into a twodimensional array. Each cell should be

a hash. I tried the following (which is not even part of the solution)


for ($i=0; $i <= $#basket_rows; $i++) {
   ($basket_rows[$i]) = split (/&&/, $basket_rows[$i]);

Quote:
}

---

This does not work. I want to access the data with two methods somehow
like this:

---
$v1 = $basket_rows[1]{'BZ_STATUS'};   # only the value of this key
$v2  = $basket_rows[1][2];   # the entire key=value pair
---

Please, can anybody MAILl me the correct splitting code and the correct
referencing for accessing or printing the values of the name=value
paris?

Many thanky in advance.

Martin Baur



Sat, 27 Oct 2001 03:00:00 GMT  
 Need Help with putting Hashes in a two-dimensional array
[posted & mailed]

Quote:

> I'd llike to split that into a twodimensional array. Each cell should be

> a hash. I tried the following (which is not even part of the solution)

> ---



Since your data is actually started by '&BASKETZEILE1=',
'&BASKETZEILE2=', etc. I have modified the regex to include the numbers
and '='.



Quote:

> for ($i=0; $i <= $#basket_rows; $i++) {
>    ($basket_rows[$i]) = split (/&&/, $basket_rows[$i]);
> }

No need for a C-style for loop here.  See below.

Quote:
> ---

> This does not work. I want to access the data with two methods somehow
> like this:

> ---
> $v1 = $basket_rows[1]{'BZ_STATUS'};   # only the value of this key
> $v2  = $basket_rows[1][2];   # the entire key=value pair
> ---

You will not be able to access your data in both of these ways from the
same variable.  The first implies a hash reference stored in
$basket_rows[1], the second an array ref stored in $basket_rows[1].  You
can only store one reference in there.  If you want to store both a
hashref and an arrayref in $basket_rows[1] then you will have to store
them in an intermediate aggregate and store a reference to that in
$basket_rows[1].

I am using an anonymous hash as the intermediate aggregate here:



        $row = {


        };
    }

Quote:
> Please, can anybody MAILl me the correct splitting code and the correct
> referencing for accessing or printing the values of the name=value
> paris?

They're accessed as above, but with the extra level in between.  And
there's no need to shout.  A lot of courtesy copies are sent from clpm
to those with valid email addresses.

    $v1 = $basket_rows[1]{Hash}{'BZ_STATUS'};  
          # only the value of this key

    $v2 = $basket_rows[1]{Array}[2];  
          # the entire key=value pair

The whole thing can be printed quite easily with the right module.

    use Data::Dumper;

--
Rick Delaney



Wed, 31 Oct 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Weird Hash w/ n-dimensional two-dimensional array !!!!!!

2. Associative array of a two dimensional array

3. help: multi-dimensional hash from flat array

4. multi-dimensional arrays with hashes help please

5. 2 dimensional array - Help needed

6. Need help with multi-dimensional arrays was:tie

7. two dimensional hashes

8. 2-dimensional array into 1 dimensional array?

9. URGENT: Hash in Hash, Array in Array, Array in Hash, Hash in Array

10. Reading into a two-dimensional array from STDIN

11. two-dimensional arrays

12. how to build two dimensional array?

 

 
Powered by phpBB® Forum Software