Question about Arrays

  • Thread starter Guenther Sohler
  • Start date
G

Guenther Sohler

I'd like to code a little bit more complicated construct, but I dont get
it done


Suppose i have following

my %database;

$database{"fruits"}=("Apple","Pear");

add_fruits(\$database);

sub add_fruits(my $databaseptr)
{
push databaseptr .. fruits "Banana";
}

I dont get the proper characters. Perl always complains.
Can anybody point me out ?
rds
 
J

Josef Moellers

Guenther said:
I'd like to code a little bit more complicated construct, but I dont get
it done

Hm, how much "complicated" do you want your code to be.
I prefer simple code ;-)
Suppose i have following

my %database;

$database{"fruits"}=("Apple","Pear");

add_fruits(\$database);

sub add_fruits(my $databaseptr)
{
push databaseptr .. fruits "Banana";
}

I dont get the proper characters. Perl always complains.
Can anybody point me out ?

Use proper Perl!
The above is not Perl as I know it.
 
P

perlistpaul

Sorry I don't get a clear picture of your explanation
But I think you may want to push Apple, Pear to one array
 
G

Guenther Sohler

Sorry, I was mistaken:


my %database;
$database{"fruits"}=("Apple","Pear");

add_fruits(\$database);

sub add_fruits
{
my $databaseptr=shift;
push ... databaseptr .. fruits , "Banana";
# C Code would by: database-> fruits += "Banana";
}


.... shall re replaced by the correct characters eg, @, $, %

Sorry, I dont get it by myself
 
J

Josef Moellers

Guenther said:
Sorry, I was mistaken:


my %database;
$database{"fruits"}=("Apple","Pear");

add_fruits(\$database);

sub add_fruits
{
my $databaseptr=shift;
push ... databaseptr .. fruits , "Banana";
# C Code would by: database-> fruits += "Banana";
}


... shall re replaced by the correct characters eg, @, $, %

Sorry, I dont get it by myself

It's still not valid Perl, but I think I get the picture:

#! /usr/bin/perl

use warnings;
use strict;

my %database;

$database{"fruits"}=["Apple","Pear"];
# Note the angle brackets making this an anonymous array rather than a
# list

add_fruits(\%database);
# Note the % sign rather than the $ sign, you want the reference of a
# hash

sub add_fruits
{
my $databaseptr=shift;
push @{$databaseptr->{"fruits"}}, "Banana";
}

print join(",", @{$database{"fruits"}}), "\n";

HTH,

Josef
 
M

Mihail

Guenther said:
Sorry, I was mistaken:


my %database;
$database{"fruits"}=("Apple","Pear");

add_fruits(\$database);

sub add_fruits
{
my $databaseptr=shift;
push ... databaseptr .. fruits , "Banana";
# C Code would by: database-> fruits += "Banana";
}


... shall re replaced by the correct characters eg, @, $, %

Sorry, I dont get it by myself

Learn Perl.

#!/usr/bin/perl
use strict;
use warnings;

my %database;
my @fruits_array = qw(Apple Pear);

$database{'fruits'} = \@fruits_array;

add_fruits(\%database);

foreach (@{ $database{'fruits'} })
{
print $_ . "\n";
}


sub add_fruits
{
my %hash = %{ $_[0] };
push @{ $hash{'fruits'} }, 'Banana';
}
 
D

Dave Weaver

Guenther Sohler said:
I'd like to code a little bit more complicated construct, but I dont get
it done

Suppose i have following

my %database;

$database{"fruits"}=("Apple","Pear");

That doesn't do what you think it's doing - a scalar variable
($database{fruits}) cannot hold an array, but it can hold a
reference to an array:

$database{fruits] = [ 'Apple', 'Pear' ];

In your version of the code $database{fruits} ends up with the single
value "Pear".
add_fruits(\$database);

You have no variable called $database - you have %database.

add_fruits(\%database);

If you had used the warnings and strict pragmas, perl would have told
you this.
sub add_fruits(my $databaseptr)

This is not how you pass parameters to Perl subroutines. Why did you
think it was?

sub add_fruits {
my ($databaseptr) = @_;

{
push databaseptr .. fruits "Banana";

That isn't even vaguely Perl - I don't know why you thought that line
might work.
}

I dont get the proper characters. Perl always complains.

When perl "complains", did you not think the content of the complaint
would provide us with useful information?

String found where operator expected at /tmp/y line 8, near "fruits
"Banana""
(Do you need to predeclare fruits?)
syntax error at /tmp/y line 8, near "fruits "Banana""
Execution of /tmp/y aborted due to compilation errors.

And that is beacuse line 8, as I said above, isn't even vaguely Perl.

Can anybody point me out ?

You never stated what it is you are trying to achieve, but I'm
guessing you want to do something like this:

#!/usr/bin/perl
use strict;
use warnings;

my %database;

$database{fruits} = [ 'Apple', 'Pear' ];

add_fruits(\%database);

use Data::Dumper;
print Dumper \%database;

sub add_fruits {
my ($db_ref) = @_;

push @{ $db_ref->{fruits} }, 'Banana';
}
 
U

Uri Guttman

JM> $database{"fruits"}=["Apple","Pear"];
JM> # Note the angle brackets making this an anonymous array rather than a
JM> # list

s/angle/square/ ;

useful nit to pick IMO :)

uri
 
B

Big and Blue

Guenther said:
sub add_fruits
{
my $databaseptr=shift;
push ... databaseptr .. fruits , "Banana";
# C Code would by: database-> fruits += "Banana";

Then you have a strange C compiler.

+= is an integer operation.

"Banana" is a char * (pointer to a string).

Whereas I expect some C compilers would let you do the increment by
converting the pointer to an integer first I doubt very much whether it
would produce any reasonable result.
 
J

Josef Moellers

Big said:
Then you have a strange C compiler.

+= is an integer operation.

"Banana" is a char * (pointer to a string).

Whereas I expect some C compilers would let you do the increment by
converting the pointer to an integer first I doubt very much whether it
would produce any reasonable result.

It's more likely he was talking about C++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top