Re: god im a noob
From: Martien Verbruggen (mgjv_at_tradingpost.com.au)
Date: 11/20/03
- Next message: Default_at_User011011101101.net: "Re: god im a noob"
- Previous message: Eric J. Roode: "Re: Protecting Source code of a perl script"
- In reply to: Default_at_User011011101101.net: "god im a noob"
- Next in thread: Default_at_User011011101101.net: "Re: god im a noob"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 19 Nov 2003 23:47:52 GMT
On Wed, 19 Nov 2003 23:12:45 GMT,
Default@User011011101101.net <Default@User011011101101.net> wrote:
> please help...
> i've just read the perllol and perlreftut perldocs...
> and two things are really bugging me.
>
> first why dos the foreach go beyond the curly barckets?
What exactly do you mean by that? The loop variable of a foreach is
localised to the block.
> second why cant i assign a has containing ref's to a dbm hash?
References are internal "pointers" to other data structures inside of
perl, i.e. in memory. A DBM hash is stored on disk. How do you propose
that reference would be stored on disk so that on the next run of perl
it would map to the correct locations in perls data storage area?
There are modules available that help you with storing complex data
structures on disk. The MLDBM modules provide a tied hash (DBM-like)
environment that deals with complex data structures. I suggest you
have a look at those.
> use strict;
> use warnings;
> my $database = "Numbers and Colors";
> verify_data_entry();
>
> #Subroutines#
> sub data_entry
> {
> print "\nEnter a number: ";
> chomp (my $number = <STDIN>); $number =~ s/\W.*//;
> print "\nEnter some colors (ctrl-z when finished):\n";
> chomp (my @colors_in = <STDIN>);
> foreach my $item (@colors_in)
> {$item =~ s/\W.*//; $item = ucfirst (lc($item));}
> my $aref_1 = [@colors_in];
> my %numbers_colors = ($number, $aref_1);
Each invocation of this subroutine has its own %numbers_colors
hash.
> verify_data_entry();
verify_data_entry calls this subroutine, which calls
verify_data_entry, which calls data_entry, which calls
verify_data_entry, which calls....
When you enter something that isn't 'y', the callstack unwinds, and
the second half of this subroutine is executed for each original
invocation.
> print "\n$database has been updated with the following entries.\n";
> foreach my $key (sort keys %numbers_colors)
For each invocation, the "private" %numbers_colors hash will have
exactly one entry, which is printed out.
> {
> my @colors_out1 = @{$numbers_colors{$key}};
> my $colors_out2 = join ', ', sort @colors_out1;
> print "$key\t$colors_out2\n";
> }
> print "\nWhy does this print between each key?\n"; # why ?!
Then this is printed out, and you exit from _this_ particular
invocation of the subroutine. falling back to the next one.
In other words, there is nothing wrong with foreach, but with your
program logic. Do something like this (pseudo code)
my %colours;
while (1)
{
last if not read_user_confirmation_y_n;
read_single_user_datum into %colours
}
save_hash
foreach key in %colours
print the key and data
Martien
--
|
Martien Verbruggen |
Trading Post Australia | Hi, Dave here, what's the root password?
|
- Next message: Default_at_User011011101101.net: "Re: god im a noob"
- Previous message: Eric J. Roode: "Re: Protecting Source code of a perl script"
- In reply to: Default_at_User011011101101.net: "god im a noob"
- Next in thread: Default_at_User011011101101.net: "Re: god im a noob"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|