moderated >> (@_) = reverse(@_)

by korovyev » Tue, 11 Jan 2005 03:48:18 GMT

Hello,

sub swap { (@_) = reverse(@_); }
my $one = "I am one";
my $two = "I am two";
swap($one,$two);
warn "one is '$one'";
warn "two is '$two'";

The code above is a quote from Greg London's "Impationt Perl".
According to the book the output should be

> one is 'I am two'
> two is 'I am one'

The output I get (perl v5.8.6) is as follows:

> one is 'I am one'
> two is 'I am two'

How do you fix it?

moderated >> (@_) = reverse(@_)

by Jean-Pierre Vidal » Tue, 11 Jan 2005 05:45:01 GMT


Le Mon, 10 Jan 2005 11:48:18 -0800, Alexander Korovyev a rit?


($one,$two) = swap($one,$two);


but the following does the same

my $one = "I am one";
my $two = "I am two";
($one,$two) = ($two,$one);
warn "one is '$one'";
warn "two is '$two'";

JPV

moderated >> (@_) = reverse(@_)

by Hans-Georg Rist » Tue, 11 Jan 2005 06:49:04 GMT


The perlsub manual page tells (perl v5.8.4):

"Any arguments passed in show up in the array @_. Therefore, if you called a
function with two arguments, those would be stored in $_[0] and $_[1]. The
array @_ is a local array, but its elements are aliases for the actual
scalar parameters. In particular, if an element $_[0] is updated, the
corresponding argument is updated (or an error occurs if it is not
updatable). [...] Assigning to the whole array @_ removes that aliasing,
and does not update any arguments."

See the last sentence, that's what you did:
you assigned to the whole array @_.

Change your subroutine to:

sub swap {
( $_[0], $_[1] ) = reverse(@_);
}

and you'll notice the difference.

HTH,
HG

moderated >> (@_) = reverse(@_)

by John W. Krahn » Tue, 11 Jan 2005 08:00:57 GMT


What you are seeing is defined behaviour:

perldoc perlsub
[snip]
Any arguments passed in show up in the array @_. Therefore, if you
called a function with two arguments, those would be stored in $_[0]
and $_[1]. The array @_ is a local array, but its elements are aliases
for the actual scalar parameters. In particular, if an element $_[0]
is updated, the corresponding argument is updated (or an error occurs
if it is not updatable). If an argument is an array or hash element
which did not exist when the function was called, that element is
created only when (and if) it is modified or a reference to it is
taken. (Some earlier versions of Perl created the element whether or
not the element was assigned to.) Assigning to the whole array @_
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
removes that aliasing, and does not update any arguments.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



The usual way to swap two values in perl is:

( $one, $two ) = ( $two, $one );



John
--
use Perl;
program
fulfillment

moderated >> (@_) = reverse(@_)

by Brian McCauley » Wed, 12 Jan 2005 02:25:46 GMT


Or, more generally:

sub swap {
@_[ 0 .. $#_ ] = reverse(@_);
}

Similar Threads

1. Reverse hyperlink

I would like to create a cell where a user can enter a value (1 thru 4096), 
the entered value will jump them to the corresponding row. Can this be done?

or should I simply create a worksheet that performs a lookup to another sheet?

2. Reverse Hyperlink - change mailto to text

3. Reverse arrow for hyperlinks

Version: 2008
Operating System: Mac OS X 10.6 (Snow Leopard)
Processor: Intel

I need to find a 'reversing arrow' that I can add to the toolbar to click on after using a hyperlink. <br>
You can do this in Windows but I cannot find a similar facility when using my macbook. <br>
I use longish word docs that often have links to other parts of the document. How do I get back to the point at which I activated the hyperlink, apart from scrolling back laboriously? <br>
Thanks for any solutions.

4. Listbox selection reverse

5. Tk-HistEntry 0.42 patch to reverse display of items

It irked me that Tk::HistEntry 0.42 displays the history items with the
most recent item on the bottom of the popup listbox.  I would expect
the
items to be in reverse order (latest at the top).  So, I implemented a
-showlastfirst option that does what I want.

I'm probably not the only person who wants this behavior, so I'm
including a patch containing my changes at the end of this message.

I would appreciate any comments on these changes.

Thanks,

Bradford C. Smith


Index: HistEntry.pm
===================================================================
RCS file: /scm/cvs-root/CPSptkwid/Tk-HistEntry/HistEntry.pm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HistEntry.pm	12 Apr 2005 14:37:02 -0000	1.1
+++ HistEntry.pm	15 Apr 2005 16:57:29 -0000	1.2
@@ -385,8 +385,11 @@
     if ($Tk::VERSION >= 800) {
 	$w->SUPER::Populate($args);
     } else {
+        my @myopts = qw(
+            -auto -command -dup -bell -limit -match -case
-showlastfirst
+        );
 	my $saveargs;
-	foreach (qw(-auto -command -dup -bell -limit -match -case)) {
+	foreach (@myopts) {
 	    if (exists $args->{$_}) {
 		$saveargs->{$_} = delete $args->{$_};
 	    }
@@ -413,6 +416,8 @@
        -match   => ['PASSIVE',  'match',   'Match',   0],
        -case    => ['PASSIVE',  'case',    'Case',    1],
        -history => ['METHOD'],
+       -showlastfirst =>
+                   [qw/PASSIVE showLastFirst ShowLastFirst/, undef],
       );

 ## Delegation does not work with the new BrowseEntry --- it seems to
me
@@ -429,35 +434,40 @@
 sub get    { shift->Subwidget('entry')->get   (@_) }
 sub insert { shift->Subwidget('entry')->insert(@_) }

+# $w->listboxRefresh
+#
+# Reload listbox widget (if any) with history items.
+sub listboxRefresh {
+        my ($w) = @_;
+
+        $w->_listbox_method(qw/delete 0 end/);
+        my @hist = $w->history;
+        return unless @hist;
+        my $lastfirst = $w->cget('-showlastfirst');
+        @hist = reverse(@hist) if $lastfirst;
+        $w->_listbox_method(qw/insert end/, @hist);
+        my $select_idx = $lastfirst ? 0 : 'end';
+        # make sure latest value is visible, selected and active
+        $w->_listbox_method('see', $select_idx);
+        $w->_listbox_method('selectionSet', $select_idx);
+        $w->_listbox_method('activate', $select_idx);
+}
+
 sub historyAdd {
     my($w, $string) = @_;
-    my($inserted, $spliced) = $w->SUPER::historyAdd($string,
-spliceinfo => 1);
-    if (defined $inserted) {
-	if ($spliced) {
-	    $w->history([ $w->SUPER::history ]);
-	} else {
-	    $w->_listbox_method("insert", 'end', $inserted);
-	    # XXX Obeying -limit also for the array itself?
-	    if (defined $w->cget(-limit) &&
-		$w->_listbox_method("size") > $w->cget(-limit)) {
-		$w->_listbox_method("delete", 0);
-	    }
-	}
-	$w->_listbox_method("see", 'end');
-	return $inserted;
-    }
-    undef;
+    my($inserted) = $w->SUPER::historyAdd($string);
+    # refresh listbox if history has been changed
+    $w->listboxRefresh if defined $inserted;
+    return $inserted;
 }
 *addhistory = \&historyAdd;

 sub history {
     my($w, $history) = @_;
-    if (defined $history) {
-	$w->_listbox_method("delete", 0, 'end');
-	$w->_listbox_method("insert", 'end', @$history);
-	$w->_listbox_method("see", 'end');
-    }
-    $w->SUPER::history($history);
+    my @ret = $w->SUPER::history($history);
+    # refresh listbox if history has been changed
+    $w->listboxRefresh if $history;
+    return @ret;
 }

 1;
@@ -538,8 +548,16 @@

 =item B<-case>

-If set to true a true value, then be case sensitive on
+If set to a true value, then be case sensitive on
 auto-completion. Defaults to 1.
+
+=item B<-showlastfirst>
+
+This option only applies to Tk::HistEntry.
+If it is set to a true value,
+history items will be displayed in reverse order in the listbox
+(i.e. most recent at the top).
+Default value is undef.
 
 =back

6. a tool to reverse-engineer a schema - Perl DBI

7. Howto reverse SortByValue with Tie::IxHash

Hi,

I have a problem using Tie::IxHash.
My question is how can I sort the hash
by value in descending order.

I tried this to get the descending order,
but doesn't seem to work:

__BEGIN__
my %hash =(
            '1-1' => 3,
            '2-3' => 2,
            '2-2' => 1,
            '1-2' => 6,
            '1-3' => 3
          );
my $t = Tie::IxHash->new(%hash);
$t->SortByValue;  # ascending, ok!

$t->$hash{$b}<=>$hash{$a}SortByValue; #tried this but doesn't work

__END__

Can anybody advice how to go about this?
Thanks beforehand.

Regards,
Edward WIJAYA
SINGAPORE

8. Help with ARP scan/reverse DNS script - Perl