Perl: added to an element


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Perl: added to an element
# 1  
Old 05-03-2012
Perl: added to an element

I'm trying to add a string to the end of each element in an array.

I have filled an array using grep:

Code:
for ($i = 0; $i <= $#array} - 1; $i++)
        {
            push (@data,qx(grep '$array[$i]' $file));
        }
}

Now i want to add something to the end of the array. For example if my array look like this:

Code:
@array = ( [abc,123], [def,456],[ghi,789])

I want to add to the end of each array. I.E. ZZZ. So the output would look like this.

Code:
@array = ( [abc,123,ZZZ], [def,456,ZZZ],[ghi,789,ZZZ])

Thanks in advance.
# 2  
Old 05-03-2012
Code:
for (@array){
   push (@{$_},"ZZZ");
}

# 3  
Old 05-03-2012
sorry, I think i had misrepresented my array.

The array looks like this:

Code:
@array = ( "abc,123", "def,456", "ghi,789" )

where abc,123 is the first element.

I would like my output to look like this:

Code:
@array = ( "abc,123,ZZZ", "def,456,ZZZ", "ghi,789,ZZZ" )

# 4  
Old 05-03-2012
Code:
skrynesaver@busybox~ ~$ perl -e '
my @array = ( "abc,123", "def,456", "ghi,789" );
use Data::Dumper;
print Dumper(\@array);'
$VAR1 = [
          'abc,123',
          'def,456',
          'ghi,789'
        ];
skrynesaver@busybox~ ~$ perl -e '
my @array = ( "abc,123", "def,456", "ghi,789" );
for (@array){
   $_.=",ZZZ";
}
use Data::Dumper;
print Dumper(\@array);'
$VAR1 = [
          'abc,123,ZZZ',
          'def,456,ZZZ',
          'ghi,789,ZZZ'
        ];
skrynesaver@busybox~ ~$ perl -e '
my @array = ( "abc,123", "def,456", "ghi,789" );
map {$_.=",ZZZ";}@array;
use Data::Dumper;
print Dumper(\@array);'
$VAR1 = [
          'abc,123,ZZZ',
          'def,456,ZZZ',
          'ghi,789,ZZZ'
        ];

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: How to check whether my array contains element x

Hi All, I am new to perl I am stuck in simple problem I need your help I want to define a subroutine. sub check_if_entity_exists(@array_to_be_checked,$entityName) I have array as http-listener-1 http-listener-2 http-listener-3 http-listener-4 If i send http-listener-3 my... (1 Reply)
Discussion started by: javaholics
1 Replies

2. Shell Programming and Scripting

Print the row element till the next row element appear in a column

Hi all I have file with columns F3 pathway CPS F2 H2 H4 H5 H6 no pathway CMP H7 H8 H9 H10 My expected output is F3 pathway CPS F2 pathway CPS (10 Replies)
Discussion started by: Priyanka Chopra
10 Replies

3. Shell Programming and Scripting

Find if XML element has a matching required element

I want to check if every <Part> element has corresponding <Description> in this sample XML. ....<Lot Of XML> <Inv lineNumber="2"> <Item> ... (4 Replies)
Discussion started by: kchinnam
4 Replies

4. UNIX for Advanced & Expert Users

Perl XML::DOM: How to test if element exists?

Hi, I'm trying to write a script for some xml file handling, but I'm not getting too far with it. I've got the following xml content <?xml version="1.0" encoding="UTF-8"?> <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <Operation name="OPER1"> <Action name="ACTION1">... (2 Replies)
Discussion started by: Juha
2 Replies

5. Shell Programming and Scripting

previous element in the array perl

Hi, How to get previous/next element in the array perl Example @queue = (1, 2 ,3 , 4); I want to get value of 1 and 2, or, 2 and 3, or 3 and 4...etc and compare to value which one is greater to do that I need to get previous and next element of array ? (1 Reply)
Discussion started by: guidely
1 Replies

6. Shell Programming and Scripting

Perl: One action if an element doesn't exist in array

Hello, I want to run one (not multiple) action if an element doesn't exist in array. for example: @array = (1..10); foreach $el (@array) { if ($el != 11) { print "number not found\n"; } } the output of this simple script: number not found (3 Replies)
Discussion started by: ahmed_zaher
3 Replies

7. Shell Programming and Scripting

Perl Foreach add "4" to the end of each element

I wrote the following script configuring cisco router:#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Opsware::NAS::Connect; my @IP = split(/\./,"$tc_device_ip$"); my $O3 = $IP; my($host, $port, $user, $pass) =... (3 Replies)
Discussion started by: ahmed_zaher
3 Replies

8. Shell Programming and Scripting

Perl question - print last element in a hash?

I am modifying someone else's code. There is a foreach statement printing the contents of a hash. Can someone give me an example of printing the last element in a hash? The output currently is A B C D E I want the output to be E (1 Reply)
Discussion started by: streetfighter2
1 Replies

9. Shell Programming and Scripting

Remove an element from an array in PERL

Hello guys, I have the following question. Consider the following code in PERL for($xeAnumber=0; $xeAnumber<@xeAnumber; $xeAnumber++) { if(@xeAnumber==@final_file) { @final_file=@xeTimeStamp; }... (3 Replies)
Discussion started by: chriss_58
3 Replies

10. Shell Programming and Scripting

Perl delete an element from array

Probably I am not seeing it or I am not using the "delete" correctly I had the following codes but it does not work for me #!/bin/perl -w ... @sysFile1 = (a_b, a_c, a_d); @sysFile2 = (a_c, a_e, b_f); foreach $line1 (@sysFile1){ trim(\$line1); (my $tmp1, my $tmp2) = split/_/,... (6 Replies)
Discussion started by: ahtat99
6 Replies
Login or Register to Ask a Question