Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to substitute array alaments all at once in Perl?

Each element of an array (@myArray) has two space characters in the beginning. I would like to remove these two spaces with one newline character (\n) for each element of the array all at once without looping in the array.

How can I do it?

Current output:
 
 
  2: %SCHEMA  Class constructor function 
3: 
4: %   Copyright 1974-2012
5: %   $Revision: 1.1.4 $  $Date: 2012/03/09 01:24:38 $
6: 
7: %%%% Get handles classes
8: CreatePackage   = find(xxx);
  12: %SCHEMA  Class constructor function 
13: 
14: %   Copyright 1974-2012
15: %   $Revision: 1.1.4 $  $Date: 2012/03/09 01:24:38 $
16: 
17: %%%% Get handles classes
18: CreatePackage   = find(xxx);

Open in new window



Expected output:
2: %SCHEMA  Class constructor function 
3: 
4: %   Copyright 1974-2012
5: %   $Revision: 1.1.4 $  $Date: 2012/03/09 01:24:38 $
6: 
7: %%%% Get handles classes
8: CreatePackage   = find(xxx);
  
12: %SCHEMA  Class constructor function 
13: 
14: %   Copyright 1974-2012
15: %   $Revision: 1.1.4 $  $Date: 2012/03/09 01:24:38 $
16: 
17: %%%% Get handles classes
18: CreatePackage   = find(xxx);

Open in new window

SOLUTION
Avatar of amitnepal
amitnepal
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar
Tolgar

ASKER

Can we do it without using for loop?
Avatar of Tolgar

ASKER

and I only want to replace the first two space characters not all of them in the array.
Avatar of farzanj
Try:

@array = map{s/^\s{2}/\n/} @array;
Avatar of Tolgar

ASKER

This did not work as expected. It replaced all spaces in each string with new line.
You can put s/^<one space><secondspace>//g to replace only first two space characcters.

without using for loop :

my @array=('  a', '  b', '  c');

my @newarray = grep(s/\s^  //g, @array);
Avatar of Tolgar

ASKER

@farzanj: Do you think you might have missed something? It removed everything from the array.
What is your aversion to using a loop.

You should know that both map and grep LOOP over the array.  So, you can either explicitly loop over the array by using a for/foreach loop or implicitly by using grep or map.

s/^\s+/\n/ for @array

Open in new window

Avatar of Tolgar

ASKER

@FishMonger: Actually you are right. I just want it to be in one line.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It should be noted that the map approach is the least efficient.
Avatar of Tolgar

ASKER

So here is the code that I have:

		my @array_new = map { s{<[^>]+>}{}g; $_ } @array;
		@array_new = map{s/^\s{2}/\n/; $_} @array_new;

Open in new window



I expect @array to be the same after these lines. But it changes. Where do I make the mistake?

@farzanj: Now, the array is not empty but nothing has changed. I don't see the new lines when I print  the array as below:

print "\nSee the below:\n @array_new \n";

Open in new window

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my @array = (
    q(<br><br>),
    q( 2: %SCHEMA  Class constructor function
<br>3:
<br>4: %   Copyright 1974-2012
<br>5: <font color="red">%   $Revision: 1.1.4 $  $Date: 2012/03/09 01:24:38 $
</font><br>6:
<br>7: %%%% Get handles classes
<br>8: CreatePackage   = find(xxx);)
);

s/<.+?>//g and s/^\s+/\n/ for @array;

print Dumper \@array;

Open in new window


Output from c:\testing>test.pl
$VAR1 = [
          '',
          '
2: %SCHEMA  Class constructor function
3:
4: %   Copyright 1974-2012
5: %   $Revision: 1.1.4 $  $Date: 2012/03/09 01:24:38 $
6:
7: %%%% Get handles classes
8: CreatePackage   = find(xxx);'
        ];

Open in new window

Avatar of Tolgar

ASKER

@FishMonger: How can I do this substitution without changing the initial array?
Avatar of Tolgar

ASKER

Anyway, I just assigned to another array.
my @new_array = @array;
s/<.+?>//g and s/^\s+/\n/ for @new_array;

Open in new window

Avatar of Tolgar

ASKER

@Fishmonger: When I use print Dumper it shows this new line but if I print it as below it I still see the two space characters and the new line does not appear. Why?

print "@new_array";

Open in new window

Avatar of Tolgar

ASKER

@Fishmonger: Ok I removed the quotes and now I got rid of the extra spaces but the new line still does not show up.

Here is the code that I have:

		my @arrayofmatches_new = @arrayofmatches;
		s/<.+?>//g and s/^\s+/\n\n/ for @arrayofmatches_new;
			print "\nSee the failed section(s) of the code below:\n";
			print @arrayofmatches_new;

Open in new window

Could it be that you need to use Windows line ending?
s/<.+?>//g and s/^\s+/\r\n\r\n/ for @arrayofmatches_new;

Open in new window

Avatar of Tolgar

ASKER

This did not work. But if I can add \n to the beginning of all the elements of the array, I think it would work.

How can I add \n to the beginning of all the elements of the array?

Thanks,
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial