Results 1 to 10 of 11
while (my $row = $sth->fetchrow_arrayref) {
$var->{"@$row[0]"}=" ";
}
Can anyone help me understanding above mentioned.
i) As per my knowledge $row is taking ARRAY Refernce from the database
ii) ...
- 01-30-2008 #1Linux Newbie
- Join Date
- Jan 2008
- Posts
- 114
Meaning of $var->{"@$row[0]"}=" "; ???
while (my $row = $sth->fetchrow_arrayref) {
$var->{"@$row[0]"}=" ";
}
Can anyone help me understanding above mentioned.
i) As per my knowledge $row is taking ARRAY Refernce from the database
ii) @$row[0] is containing the value of 0th index of the array, testted the same.
but I am not able understand the meaning of below mentioned, as it looks like the we are trying to initialize the value to hash, don't know????
my $var->{"@$row[0]"}=" ";
- 01-30-2008 #2
$var is a hash reference, and this code assigns "" to the reference with the key set to the contents of the first column of the returned row from the database. So if your table looks like this:
Table1
--------
Foo
Bar
Baz
It would be equivalent to assigning the following to $var:
Code:$var = {Foo => '', Bar => '', Baz => ''}Flies of a particular kind, i.e. time-flies, are fond of an arrow.
Registered Linux User #408794
- 01-30-2008 #3Linux Newbie
- Join Date
- Jan 2008
- Posts
- 114
Hey thanks man, very old issue you solved it.
- 01-30-2008 #4
That makes sense to me, but I'm confused about the presence of the "@". That would be dereferencing $row into an array, but once you add the [0], it becomes a bit wonky?
I guess this is interpreted as an array slice containing one element, and as a result, it gets put into the string as just that one element?
The appropriate code, in my mind, would be:
Am I making sense, or am I missing something?Code:$var->{$row->[0]} = " ";DISTRO=Arch
Registered Linux User #388732
- 01-30-2008 #5Linux Newbie
- Join Date
- Jan 2008
- Posts
- 114
$row is becoming an array as at the runtime fetchrow_arrayref fetching the next row of data and returning it as a reference to an array of field values.
So for intializing the value to the key, which is the 0th element of the array we have to write in {"@$row[0]"}=" "
or if you print @{$row[0]} it'll give you the value of 0th element.
- 01-31-2008 #6
$row[0] returns the 0th element from the array called row. We want to dereference the scalar $row into an array, and take the 0th element from that. So @{$row[0]} is taking the 0th element of the array called row, and dereferencing it to be an array.
And apparently @$row[0] works. I'm just trying to figure out what it actually means. As I say, the way that you usually access elements of an arrayref is $ref->[0].DISTRO=Arch
Registered Linux User #388732
- 01-31-2008 #7Linux Newbie
- Join Date
- Jan 2008
- Posts
- 114
As I told you that $row[0] is storing the ARRAY Reference of value not a value.
So for retrieving the value from array ref we use this syntax @$row[0] or @{$row[0]}.
I hope so...
Javasnob- am I right --or -- you can put more light
- 01-31-2008 #8
Wait...you're saying that $row is an arrayref, and every element of that array is also an arrayref? That doesn't make too much sense...
$row should be an arrayref, every element of which is the value in its row for its corresponding column. The reason that you get all of the values is because you are looping.DISTRO=Arch
Registered Linux User #388732
- 02-01-2008 #9
$row is an array reference. $row[0] would refer to the first element of @row, which we are not considering here. @$row[0] refers to a slice of one element (the first element) of the array referenced by $row, because @ has higher precendence than [], and the whole thing is interpolated in a string, so the list of one element becomes a regular scalar value. $row->[0] is NOT an array reference; it should be a simple datatype like integer or string.
Flies of a particular kind, i.e. time-flies, are fond of an arrow.
Registered Linux User #408794
- 02-01-2008 #10
I get that $row->[0] is not an arrayref, but rather a scalar. I'm just confused as to why the slice is necessary. At the end of the day, my approach and the approach used above do the same thing, only that way involves taking an array slice of a single value and forcing it to a scalar, while mine just grabs a scalar.
I guess I'm just confused on the slicing, and I'm waiting to hear that it is unnecessary.DISTRO=Arch
Registered Linux User #388732


Reply With Quote