hi,
I have a array like
my $array = ( "apple","ball","cat","dog","elephant");
how to push some element in the array to the first position.
for example my final array should be
elephant apple ball cat dog
hi,
I have a array like
my $array = ( "apple","ball","cat","dog","elephant");
how to push some element in the array to the first position.
for example my final array should be
elephant apple ball cat dog
homework?
perldoc
push, pop, shift, unshift
I assume, from the "my" keyword, you're using perl
One way to do this would be like this
unshift(@array,pop(@array));
i tried this one to shift the element elephant to first position , but its not working, $_ is showing nothing here
if(grep $_ =~ m/elephant/, @array) {
unshift(@array, $_);
}
after moving the required element to the first position i want to remove that element from previous position.any help is appreciated.
If you want to check the value, you'll need to do something like this:
#! /usr/bin/perl -w
use Data::Dumper;
my @array = ("apple","ball","cat","dog","elephant");
$_ = pop(@array);
if(m/elephant/) {
unshift(@array, $_);
}
print Dumper(@array);
Notice that the m// is implicitly working on the $_ variable.
Hi.
I was reading in Perl Best Practices - O'Reilly Media , and ran across a function first_index that resides in a recommended module, List::MoreUtils. Here are a few possibly-related ideas using such a function to identify an entry, then remove it to one end of the other of an array with the help of in-built functions splice. push, and unshift:
#!/usr/bin/env perl
# @(#) p1 Demonstrate list entry identification, movement.
use warnings;
use strict;
use List::MoreUtils qw( first_index );
my ( $debug, @a, @original, $t1, $t2 );
$debug = 1;
$debug = 0;
my ($want) = "cat";
@original = qw/ pig goat catastrophe cat dog turkey deer /;
@a = @original;
print "\n";
print " For simple string equality, \"cat\", constant, then variable:\n";
print "\n";
print " Constant, just to see a simple example:\n";
print "a (original ) is :@a:\n";
$t1 = first_index { $_ eq "cat" } @a;
print $t1, "\n" if $debug;
$t2 = splice( @a, $t1, 1 );
print $t2, "\n" if $debug;
unshift @a, $t2;
print "a (for unshift) is :@a:\n";
print "\n";
print " Now with variables, a more likely situation:\n";
@a = @original;
print "a (original ) is :@a:\n";
$t1 = first_index { $_ eq $want } @a;
print $t1, "\n" if $debug;
$t2 = splice( @a, $t1, 1 );
print $t2, "\n" if $debug;
unshift @a, $t2;
print "a (for unshift) is :@a:\n";
print "\n";
@a = @original;
print "a (original ) is :@a:\n";
$t1 = first_index { $_ eq $want } @a;
print $t1, "\n" if $debug;
$t2 = splice( @a, $t1, 1 );
print $t2, "\n" if $debug;
push @a, $t2;
print "a (for push ) is :@a:\n";
print "\n";
@a = @original;
print "a (original ) is :@a:\n";
unshift @a, splice( @a, ( first_index { $_ eq $want } @a ), 1 );
print "a (single line) is :@a:\n";
@a = @original;
print "\n";
print " For string match regular expression, /$want/:\n";
print "\n";
print "a (original ) is :@a:\n";
$t1 = first_index { $_ =~ m/$want/ } @a;
print $t1, "\n" if $debug;
$t2 = splice( @a, $t1, 1 );
print $t2, "\n" if $debug;
unshift @a, $t2;
print "a (for unshift) is :@a:\n";
exit(0);
producing:
% ./p1
For simple string equality, "cat", constant, then variable:
Constant, just to see a simple example:
a (original ) is :pig goat catastrophe cat dog turkey deer:
a (for unshift) is :cat pig goat catastrophe dog turkey deer:
Now with variables, a more likely situation:
a (original ) is :pig goat catastrophe cat dog turkey deer:
a (for unshift) is :cat pig goat catastrophe dog turkey deer:
a (original ) is :pig goat catastrophe cat dog turkey deer:
a (for push ) is :pig goat catastrophe dog turkey deer cat:
a (original ) is :pig goat catastrophe cat dog turkey deer:
a (single line) is :cat pig goat catastrophe dog turkey deer:
For string match regular expression, /cat/:
a (original ) is :pig goat catastrophe cat dog turkey deer:
a (for unshift) is :catastrophe pig goat cat dog turkey deer:
cheers, drl