Maxim has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have try to debug my script but I always got an error message: Can't use string ("250") as an ARRAY ref while "strict refs". I am tring to change a value in the element tags "conditionning".
Here the code
use strict; use CGI ':standard'; use XML::Simple; use Data::Dumper; #Variables my $newval = '200'; my $var1; my $oldval = '200'; my $prodid = '8080'; my $temps; print header; my $arrays = [qw/content subcategory packaging_qty recipe descript +ion product paragraph comments channel brand conditionning product_id + heading photo/]; my $cat = (<DATA>); my $category = XMLin( $cat, keyattr => 1, forcearray => $arrays); print Dumper($category); print "<html><body>Yes work\n"; foreach my $subcategory (@{$category->{'subcategory'}}) { foreach my $product (@{$subcategory->{'product'}}) { foreach my $product_id (@{$product->{'product_id'}}) { if ($product_id eq $prodid) { foreach my $conditionning (@{$product->{'condition +ning'}}) { foreach my $content (@{$conditionning->{'conte +nt'}}) { #$temps = $conditionning; if ($content eq $oldval) { $content = $newval; XMLout($category, xmldecl => '<?xml version="1.0"?><?xml-stylesheet ty +pe="text/xsl" href="Adminproduct.xsl"?>', rootname =>'category', outp +utfile =>('C:\Program Files\Apache Group\Apache2\htdocs\legastronome\ +XML\Admin\test9.xml')); print"<br><h3>All done!</h3>"; print "</body></html>\n"; exit 1; } else { print "$temps" } } } } } } }
Here some data
<category name="Coffee"> <heading>150 years of know how at the service of a same strategy: +Taste</heading> <photo>images/bag_legal.jpg</photo> <subcategory name="Prestige"> <photo>images/prestige.jpg</photo> <product>Coffee <brand>33</brand> <channel>D</channel> <channel>A</channel> <conditionning unit="gr">250</conditionning> <description>aaaa</description> <packaging_qty>12</packaging_qty> <product_id>8080</product_id> <recipe>/recipes/coffee1.html</recipe> </product> </subcategory> </category>
I have check my data structure it seems to be fine. I don't know what the problem.
Thanks in advance.
Maxim

Janitored by Arunbear - moved readmore tags outside the code block

Replies are listed 'Best First'.
Re: Can't use string as an ARRAY
by PodMaster (Abbot) on Oct 02, 2004 at 11:13 UTC
    Hi. You should use diagnostics to get a better explanation of warnings/errors (or you could just read perldoc perldiag).

    Dumper outputs

    $VAR1 = { 'name' => 'Coffee' };
    , which is obviously not what you expected $category to be. This is because my $cat = (<DATA>); read only 1 line from DATA (perldoc -f readline). You should try my $cat = join '', <DATA>;.

    update: After making the above change I get Can't use string ("250") as an ARRAY ref while "strict refs" in use at foo.pl line 34. and line 34 is foreach my $content (@{$conditionning->{'content'}}).
    Basically, $category->{'subcategory'}[0]{'product'}[0]{'conditionning'}[0]{'content'} is '250' (just like <conditionning unit="gr">250</conditionning>) and not an array.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Thanks to help me. I understand now. I would like if you can give me a full example please. I will try what you just gave me :).
      Thank you very much
      I understand you wrote. What I want is replace "250" by "500". I don't know how to change it.
      Thanks
        I don't think you do. This is one way
        $category->{'subcategory'}[0]{'product'}[0]{'conditionning'}[0]{'conte +nt'} = 500;

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Can't use string ("250") as an ARRAY ref while "strict
by TedPride (Priest) on Oct 03, 2004 at 22:26 UTC
    I'll probably get burned at the stake for suggesting this, but if all you want to do is change a single value in the page, wouldn't it be better to use a regex?
    my $tag = 'conditionning'; my $newval = '200'; my $data = join('', <DATA>); $data =~ s/(<$tag[^>]*>)[^<]*(<\/$tag[^>]*>)/$1$newval$2/gi; print $data; __DATA__ <category name="Coffee"> <heading>150 years of know how at the service of a same strategy: +Taste</heading> <photo>images/bag_legal.jpg</photo> <subcategory name="Prestige"> <photo>images/prestige.jpg</photo> <product>Coffee <brand>33</brand> <channel>D</channel> <channel>A</channel> <conditionning unit="gr">250</conditionning> <description>aaaa</description> <packaging_qty>12</packaging_qty> <product_id>8080</product_id> <recipe>/recipes/coffee1.html</recipe> </product> </subcategory> </category>