Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

String or array?

by sschneid (Deacon)
on Sep 15, 2005 at 18:47 UTC ( [id://492375]=perlquestion: print w/replies, xml ) Need Help??

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

I have the following, which works:
my ($user); # This isn't defined explicity in production code, I'm just doing it h +ere to # illustrate what I (could) get back. $user->{'host'} = '*'; unless ($user->{'host'} > 1) { my $hList = $user->{'host'}; delete $user->{'host'}; push @{$user->{'host'}}, $hList; }
$user->{'host'} needs to be an array, even if it contains only one item. The code I have works, but gives out an error:
Can't use string ("*") as an ARRAY ref while "strict refs" in use at t +est.pl line 16.
...which is understandable, since it's not an array, it's a string.

Can someone suggest a better snippet for what I'm doing? Do I need to create a temporary ref to test on?

Thanks.
-s.

Replies are listed 'Best First'.
Re: String or array?
by friedo (Prior) on Sep 15, 2005 at 18:54 UTC
    Why do you set $user->{host} to '*' if you are just deleting it and making it an array ref later? Perhaps I'm missing something, but why not do $user->{host} = [ '*' ];

    Update:

    depending on what the query returns, it's either a string or an array. I need to act on what's given.

    In that case, you can use ref to figure out what you have:

    my $thing = function_that_returns_arrayref_or_string; if( ref $thing eq 'ARRAY' ) { $user->{host} = $thing; } else { $user->{host} = [ $thing ]; }
Re: String or array?
by ikegami (Patriarch) on Sep 15, 2005 at 18:52 UTC
    $user->{'host'} = [ '*' ]; # An array of one item
      Yeah... the problem is (maybe I should have specified) that what's put in $user->{'host'} isn't defined by me explicitly -- depending on what the query returns, it's either a string or an array. I need to act on what's given.

      -s.

        I figured out what you meant just before you posted. I was about to update my post with the following when your reply appeared:

        my ($user); $user->{'host'} = '*'; $user->{'host'} = [ $user->{'host'} ] if not ref $user->{'host'};

        ref

Re: String or array?
by borisz (Canon) on Sep 15, 2005 at 18:57 UTC
    my $user; $user->{'host'} = '*'; # scalar one element # if $user->{host} is not a reference create a reference to a array $user->{host} = [ $user->{host} ] unless ref $user->{host};
    or
    $user->{host} = [ '*' ];
    Boris

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://492375]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found