Re: scoping problem with hash

2006-06-13 Thread Mr. Shawn H. Corey
On Tue, 2006-13-06 at 17:20 +0100, [EMAIL PROTECTED] wrote:
>   print STDERR Dumper(%times);

print STDERR Dumper( \%times );


-- 
__END__

Just my 0.0002 million dollars worth,
   --- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
  Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: scoping problem with hash

2006-06-13 Thread Dermot Paikkos
On 13 Jun 2006 at 12:27, Mr. Shawn H. Corey wrote:

> On Tue, 2006-13-06 at 17:20 +0100, [EMAIL PROTECTED] wrote: > 
>  print STDERR Dumper(%times);
> 
> print STDERR Dumper( \%times );

That seems to have helped the Dumper output but I still haven't 
managed to get the other assignments to stick. The name, morning 
...etc are not in the output only the last assignments that were made 
in the while loop.


$VAR1 = {
  'Thu-08' => {
'aft' => '0:00',
'total' => '5:44',
'home' => '16:09'
  },
  'Wed-07' => {
'aft' => '4:10',
'total' => '8:42',
'home' => '17:51'
  },
  'Tue-06' => {
'aft' => '2:57',
'total' => '7:20',
'home' => '16:18'
  },
  'Fri-09' => {
'aft' => '4:19',
'total' => '8:05',
'home' => '18:37'
  }
};


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: scoping problem with hash

2006-06-13 Thread Mr. Shawn H. Corey
On Tue, 2006-13-06 at 17:20 +0100, [EMAIL PROTECTED] wrote:
> Hi,
> 
> This is a bit of an extension on an earlier post. 
> 
> I am trying to create a data structure from a file (contents below). It is 
> meant to be a hash of 
> hashes but I suspect there is either a typo somewhere or I am hitting some 
> scoping problems. All 
> that is left in the hash is the last data assigned to it. Am I overwritting 
> my earlier assignment?
> 
> Data::Dumper shows the data as this
> ...snip
> 
> $VAR3 = 'Wed-07';
> $VAR4 = {
>   'total' => '8:42',
>   'home' => '17:51'
> };
> $VAR5 = 'Tue-06';
> $VAR6 = {
>   'total' => '7:20',
>   'home' => '16:18'
> };
> $VAR7 = 'Fri-09';
> $VAR8 = {
>   'total' => '8:05',
>   'home' => '18:37'
> };
> 
> 
> I want something like this
> $VAR3{Wed-07} = {
>   name=> Some Name,
>   began   => Tue-06,
>   day => Wed,
>   dom => 07,
>   morning => 09:27,
>   home=> 18:37,
>   total   => 8:42
> };
> 
> Can someone point me in the right direction (again)?
> 
> ### my effort ##
> 
> #!/usr/bin/perl
> 
> 
> use strict;
> use warnings;
> use Data::Dumper;
> 
> 
> my $file = 'myfile.txt';
> 
> my $stuff = read_file($file);
> 
> 
> sub read_file {
> 
> 
>  my $file = shift;
>  open(FH,$file) or die "Can't open $file: $!\n";
>  my %times;
>  my ($name,$begin,$hashkey,$key,$day,$dom,$mon,$time,$hour);
>  while () {
> chomp;
> if ($_ =~ /&N/) {
> ($name) = ($_ =~ /(\w+\s\w+|\w+\s\w+\s\w+|\w+\s\w+-\w+)$/);
> }
> if ($_ =~ /&D/) {
> ($begin) = ($_ =~ /(\w+\s+\d+-\w+-\d+)$/);
> }
> next if ($_ !~ /^(x|j|k|z)/);
> #   daydom   mon  
>time   hour
> ($key,$day,$dom,$mon,$time,$hour) = ($_ =~ 
> /^(\w)\s+(\w+)\s+(\d+)-(\w+)-
> \d+\s+(\d+:\d+):.*(\d+:\d+|-\d+:-\d+)/);
> $hashkey = $day.'-'.$dom;
>   $times{$hashkey}->{name} = $name;
>   $times{$hashkey}->{began} = $begin;
>  if ($key =~ /x/i ) {
> $times{$hashkey} = {
> name=> $name,
> began   => $begin,
> day => $day,
> dom => $dom,
> morning => $time,
> };
> }
>  }
>  elsif ($key =~ /z/) {
>  my ($h,$total) = ($_ =~ /\s+(\d+:\d+)\s+.*\s+(\d+:\d+)\s\$/);
>  $times{$hashkey} = {
> home=> $time,
> total   => $total,
>  };

  $times{$hashkey}{home} = $time;
  $times{hashkey}{total} = $total;

# Your code replaces, not augments


> 
>  }
>   }
>   print STDERR Dumper(%times);
>   return %times;
>  }
> ##
> 
> 
> ## Sample data ##
> 
> &N: Joe Bloggs
> &D: Tue 06-Jun-2006
> %%
> x Tue 06-Jun-2006 08:18:22 2006 2:11 [OKAY] $
> j Tue 06-Jun-2006 12:51:33 2006 4:33 [OKAY] $
> k Tue 06-Jun-2006 13:21:27 2006 0:30 OK+SHL $
> z Tue 06-Jun-2006 16:18:52 2006 2:57 [OKAY] 7:20 $
> ~
> %%
> x Wed 07-Jun-2006 08:39:05 2006 0:44 [OKAY] $
> j Wed 07-Jun-2006 13:11:23 2006 4:32 [OKAY] $
> k Wed 07-Jun-2006 13:41:04 2006 0:30 OK+SHL $
> z Wed 07-Jun-2006 17:51:18 2006 4:10 [OKAY] 8:42 $
> ~
> %%
> x Thu 08-Jun-2006 08:13:54 2006 2:06 [OKAY] $
> j Thu 08-Jun-2006 13:57:55 2006 5:44 [OKAY] $
> k Thu 08-Jun-2006 16:08:42 2006 2:10 OK+SHL $
> z Thu 08-Jun-2006 16:09:03 2006 0:00 [OKAY] 5:44 $
> ~
> %%
> x Fri 09-Jun-2006 09:24:05 2006 1:05 [OKAY] $
> j Fri 09-Jun-2006 13:10:56 2006 3:46 [OKAY] $
> k Fri 09-Jun-2006 14:17:40 2006 1:06 OK+SHL $
> z Fri 09-Jun-2006 18:37:38 2006 4:19 AFTER! 8:05 $
> ~
> %%
> ##
> Dermot Paikkos
> 
> Network Administrator @ Science Photo Library
> Phone: 0207 432 1100 
> Fax: 0207 286 8668
> 
> 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: scoping problem with hash

2006-06-13 Thread Mr. Shawn H. Corey
On Tue, 2006-13-06 at 13:08 -0400, Mr. Shawn H. Corey wrote:
>   $times{$hashkey}{home} = $time;
>   $times{hashkey}{total} = $total;
> 
> # Your code replaces, not augments

Consider adding this to your library of useful Perl utilities:

# --
# hset %hash, ( $key => $value, ... );
#   Augment the hash with the key-value pairs.
#   WARNING: This subroutine overwrites the value of any key that
already
#   exists.
sub hset (\%%) {
  my $hash_ref = shift;
  my %kv_pairs = @_;

  for ( keys %kv_pairs ){
$hash_ref->{$_} = $kv_pairs{$_};
  }
}


-- 
__END__

Just my 0.0002 million dollars worth,
   --- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
  Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: scoping problem with hash

2006-06-13 Thread Charles K. Clarkson
Mr. Shawn H. Corey wrote:

:   for ( keys %kv_pairs ){
: $hash_ref->{$_} = $kv_pairs{$_};
:   }

You could use a hash slice there.

@{ %$hash_ref }{ keys %kv_pairs } = values %kv_pairs;


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
Free Market Advocate
Web Programmer

254 968-8328

Don't tread on my bandwidth. Trim your posts.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: scoping problem with hash

2006-06-15 Thread Lawrence Statton
Charles Clarkson wrote:
> @{ %$hash_ref }{ keys %kv_pairs } = values %kv_pairs;

You can excise a little of the snyactic sugar there

@$hash_ref{keys %kv_pairs} = values %kv_pairs;

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g
Computer  software  consists of  only  two  components: ones  and
zeros, in roughly equal proportions.   All that is required is to
sort them into the correct order.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: scoping problem with hash

2006-06-16 Thread Paul Johnson
On Tue, Jun 13, 2006 at 12:06:02PM -0700, Lawrence Statton wrote:

> Charles Clarkson wrote:
> > @{ %$hash_ref }{ keys %kv_pairs } = values %kv_pairs;
> 
> You can excise a little of the snyactic sugar there
> 
> @$hash_ref{keys %kv_pairs} = values %kv_pairs;

%hash = (%hash, %kv_pairs);

hmmm, tradeoffs ...

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: scoping problem with hash

2006-06-16 Thread Beginner
On 16 Jun 2006 at 12:15, Paul Johnson wrote:

> On Tue, Jun 13, 2006 at 12:06:02PM -0700, Lawrence Statton wrote:
> 
> > Charles Clarkson wrote:
> > > @{ %$hash_ref }{ keys %kv_pairs } = values %kv_pairs;
> > 
> > You can excise a little of the snyactic sugar there
> > 
> > @$hash_ref{keys %kv_pairs} = values %kv_pairs;
> 
> %hash = (%hash, %kv_pairs);
> 
> hmmm, tradeoffs ...
> 

Would anyone care to explain what any of these varations do? Are they all 
slices?

And while your at it you could also explain how to sort the reference by the 
hashkey when  
$hashkey looks like 'Mon-05'.  

$hashref{$hashkey}{name} = $name;  

I only ask because everyone seems eager to demonstate the perl prowess.  

Dp.



Dermot Paikkos

[EMAIL PROTECTED]
Network Administrator @ Science Photo Library
Phone: 0207 432 1100 
Fax: 0207 286 8668


Re: scoping problem with hash

2006-06-16 Thread John W. Krahn
Beginner wrote:
> On 16 Jun 2006 at 12:15, Paul Johnson wrote:
> 
>>On Tue, Jun 13, 2006 at 12:06:02PM -0700, Lawrence Statton wrote:
>>
>>>Charles Clarkson wrote:
@{ %$hash_ref }{ keys %kv_pairs } = values %kv_pairs;
>>>You can excise a little of the snyactic sugar there
>>>
>>>@$hash_ref{keys %kv_pairs} = values %kv_pairs;
>>%hash = (%hash, %kv_pairs);
>>
>>hmmm, tradeoffs ...
> 
> Would anyone care to explain what any of these varations do? Are they all 
> slices?

@{ %$hash_ref }{ keys %kv_pairs } = values %kv_pairs;

Which should really be:

@{ $hash_ref }{ keys %kv_pairs } = values %kv_pairs;

But the '%' is allowed for backward compatibility, and:

@$hash_ref{ keys %kv_pairs } = values %kv_pairs;

Do exactly the same thing - they add the keys and values of %kv_pairs to
$hash_ref (using a hash slice.)


%hash = ( %hash, %kv_pairs );

Creates a list of all the keys and values of %hash and %kv_pairs and assigns
that list to %hash overwriting its previous contents.



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: scoping problem with hash

2006-06-16 Thread Mr. Shawn H. Corey
On Fri, 2006-16-06 at 12:15 +0200, Paul Johnson wrote:
> On Tue, Jun 13, 2006 at 12:06:02PM -0700, Lawrence Statton wrote:
> 
> > Charles Clarkson wrote:
> > > @{ %$hash_ref }{ keys %kv_pairs } = values %kv_pairs;
> > 
> > You can excise a little of the snyactic sugar there
> > 
> > @$hash_ref{keys %kv_pairs} = values %kv_pairs;
> 
> %hash = (%hash, %kv_pairs);
> 
> hmmm, tradeoffs ...

When I wrote the original subroutine, I wanted to avoid things like
this. After all, push can be replaced in a similar manner:

  push @list, $item;
  @list = (@list, $item);

However, you have inspired a new version:
# --
# hset %hash, ( $key => $value, ... );
#   Augment the hash with the key-value pairs.
#   WARNING: This subroutine overwrites the value of any key that
already
#   exists.
sub hset (\%%) {
  my $hash_ref = shift;

  die "odd number of items in hash\n" if @_ % 2;

  while( @_ ){
my $key = shift @_;
$hash_ref->{$key} = shift @_;
  }
}


-- 
__END__

Just my 0.0002 million dollars worth,
   --- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
  Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]