Re: Peek a hash without breaking iterator ( Hash::StoredIterator )
by Anonymous Monk on Jun 19, 2016 at 07:16 UTC
|
| [reply] |
|
This works like a charm. Not checked CPAN (don't know why), just googled :)
If anyone can suggest other solutions, interested in them all.
| [reply] |
Re: Peek a hash without breaking iterator
by karlgoethebier (Abbot) on Jun 19, 2016 at 20:05 UTC
|
foreach my $key ( keys %hash ) {
print qq($key => $hash{$key}\n);
my %other_hash = %hash;
}
If i run this, i don't get an infinite loop...
Regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] [d/l] [select] |
|
If i run this, i don't get an infinite loop... Hurricup is writing a debugger, his debugger cannot be rewriting the users code while its running the code, the user isn't going to like that,
| [reply] |
|
| [reply] |
|
|
| [reply] |
Re: Peek a hash without breaking iterator
by choroba (Cardinal) on Jun 21, 2016 at 09:32 UTC
|
Have you checked what the other debuggers do? (i.e. Devel::hdb).
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord
}map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
| [reply] [d/l] |
Re: Peek a hash without breaking iterator
by pme (Monsignor) on Jun 19, 2016 at 07:35 UTC
|
hi hurricup,
Excerpts from the man pages:
each "When called on a hash in list context, returns a 2-element list consisting of the key and value for the next element of a hash."
keys "Called in list context, returns a list consisting of all the keys of the named hash."
values "In list context, returns a list consisting of all the values of the named hash."
As you can see the example below each is executed repeatedly however keys and values are called only once.
use strict;
use warnings;
my %hash = ('Hello' => 'World');
while ( my ( $key, $value ) = each %hash ) {
#my %otherhash = %hash;
print "\n$key=$value";
}
print "\n------------------\n";
foreach ( keys %hash ) {
my %otherhash = %hash;
print "\n$_=$hash{$_}";
}
print "\n------------------\n";
foreach ( each %hash ) {
my %otherhash = %hash;
print "\n$_\n";
}
print "\n------------------\n";
| [reply] [d/l] |
|
I don't get your point. If you are saying how to modify source code - I know it, but can't do. Check the last paragraph of the question.
| [reply] |
Re: Peek a hash without breaking iterator
by anonymized user 468275 (Curate) on Jun 21, 2016 at 08:43 UTC
|
$ perl -e '
> my %h=(qw(a b c d e f));
> { my @each = %h;
> while (my $k = shift @each) {
> my $v = shift @each;
> print "$k => $v\n";
> }}'
c => d
e => f
a => b
Update: you could also create an oo version of each that uses a handler object instead of magic so that it can't break under your circumstances. e.g.
package OOEach;
sub new {
my ($class) = @_;
return bless {}, $class;
}
sub each {
my $self = shift;
$self->{queue} ||= [@_]; # note: will only init once
my $k = shift @{$self->{queue}};
my $v = shift @{$self->{queue}};
return ($k, $v);
}
;
| [reply] [d/l] [select] |
|
Heheh, another one , hurricup is creating the debugger, hes writing the debugger, he cannot change the users code to suit the debugger,
| [reply] |
|
Actually he IS changing the user's code to effect debugging. As I understand it he is effectively inserting a command to copy the hash into the code he is debugging. So in fact a custom implementation of each is still one way to stop the real one breaking.
| [reply] |
|
Re: Peek a hash without breaking iterator
by Cow1337killr (Monk) on Jun 21, 2016 at 21:28 UTC
|
...my debugger for IntelliJ IDEA
So, let me get this straight. You are using IntelliJ IDEA as your Perl development integrated development environment (IDE).
(Excuse me if my question shows that I am behind the times.) | [reply] |
|
| [reply] |
Re: Peek a hash without breaking iterator
by shawnhcorey (Friar) on Jun 19, 2016 at 12:19 UTC
|
| [reply] |
|
This is just an example. Please, read the last paragraph.
| [reply] |
Re: Peek a hash without breaking iterator
by Anonymous Monk on Jun 20, 2016 at 17:08 UTC
|
foreach my $key ( keys %hash ) {
print qq($key => $hash{$key}\n);
my %other_hash = %hash;
}
If i run this, i don't get an infinite loop...
Regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] [d/l] [select] |
Re: Peek a hash without breaking iterator
by anonymized user 468275 (Curate) on Jun 21, 2016 at 08:18 UTC
|
$ perl -e '
> my %h=(qw(a b c d e f));
> { my @each = %h;
> while (my $k = shift @each) {
> my $v = shift @each;
> print "$k => $v\n";
> }}'
c => d
e => f
a => b
| [reply] [d/l] |