Perl : Arrays : where are these spaces coming from


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : Arrays : where are these spaces coming from
# 1  
Old 12-03-2013
Perl : Arrays : where are these spaces coming from

Example data

Quote:
admin:show status

Host Name : somehost
Date : Mon Dec 02, 2013 17:38:47
Time Zone : IST
Locale : en_US
Product Ver : CTS 1.9.6(2) P1
System State : Run Normal
Boot Slot : 2
Uptime: 5:38pm up 7 days, 7:38, load average: 0.64, 0.45, 0.43

The sub and array in question
Code:
sub parse_status {
 my $status_info = shift;
 my @info;
 push @info,"$1\n" if $status_info =~ /(System State : [^\n]+)/;
 push @info, "System_Uptime : $1\n" if $status_info =~ /Uptime:.+?up (.+?), load/;
 push @info, "$1\n" if $status_info =~ /(Product Ver  : [^\n]+)/;


 #print "$1\n" if $status_info =~ /(System State : [^\n]+)/;
 #print "System_Uptime : $1\n" if $status_info =~ /Uptime:.+?up (.+?), load/;
 #print "$1\n" if $status_info =~ /(Product Ver  : [^\n]+)/;
 #print "\n\n";

print "@info\n";
}


the output using an array -NOTICE THE SPACES BEFORE System_Uptime and Product Ver. I cant figure out where they are coming from. EDITED. The spaces dont show up when using the QUOTE tags so I used the code tags.


Code:
Parse show status
--------------------
System State : Run Normal
 System_Uptime :  2:16
 Product Ver  : CTS 1.9.6(2) P1



The output using print

Quote:
Parse show status
--------------------
System State : Run Normal
System_Uptime : 2:16
Product Ver : CTS 1.9.6(2) P1
Thanks in advance for the help !
# 2  
Old 12-03-2013
There is a built-in output field separator which inserts a single space between your array elements. It is controlled by the variable $" so you can change it. See my example below:
Code:
$ cat print_arr.pl
#!/bin/perl

@arr = ('this', 'is', 'the', 'life');

print "@arr\n";

$" = "";
print "@arr\n";

$" = "\n";
print "@arr\n";

$ perl print_arr.pl 
this is the life
thisisthelife
this
is
the
life
$

By default spaces are printed between the words "this is the life", By changing $" to "" (null string) no spaces are printed. By changing $" to "\n" (the linefeed character) each word is printed on a different line. Check out the perlvar man page for more information.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 3  
Old 12-09-2013
Thank you !!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop through array of arrays of string with spaces

Hi I'm trying to loop through an array that contains other arrays and these arrays consist of strings with spaces. The problem is that I can't seem to preserve the spacing in the string. The string with spaces are either divided into multiple items if I change IFS to \n or all the elements of... (4 Replies)
Discussion started by: kidmanos
4 Replies

2. Shell Programming and Scripting

perl: compare two arrays

Hi friends, I want to compare two arrays and find matched one using perl? Also, I want to delete unmatched one. Plz suggest me solution (1 Reply)
Discussion started by: Renesh
1 Replies

3. Shell Programming and Scripting

Arrays in perl

Hi all, I have a log file which has logs. I am reading logs line by line into perl arrays. I want to print all the arrays elements starting from 8(word) to end of the line. print array......array to a new file. and I have to do it in perl as res of the program in perl. Please help me on... (9 Replies)
Discussion started by: firestar
9 Replies

4. Shell Programming and Scripting

Perl Compare 2 Arrays

Hello, Consider the following 2 arrays: Array1 = qw(Fa0/0 Fa0/1 Fa0/2 Fa0/3); Array1 = qw(Fa0/1 Fa0/2 Fa0/3 Fa0/4); I want to compare the following 2 arrays as follows: Take specific action when elements of Array1 that doesn't exist in Array2 (in my example: Fa0/0). Take another... (4 Replies)
Discussion started by: ahmed_zaher
4 Replies

5. Shell Programming and Scripting

compare 2 arrays in perl

Hi Im supposed to compare lines in a file : KB0005 1019 T IFVATVPVI 0.691 PKC YES KB0005 1036 T YFLQTSQQL 0.785 PKC YES KB0005 1037 S FLQTSQQLK 0.585 DNAPK YES KB0005 1045 S KQLESEGRS 0.669 PKC YES KB0005 1045 S KQLESEGRS 0.880 unsp YES KB204320 1019 T IFVATVPVI 0.699 PKC YES ... (7 Replies)
Discussion started by: karla
7 Replies

6. Shell Programming and Scripting

perl arrays

Hi I need some help using arrays in perl. I have an array say var and a variable var1. I want to check if the var1 is present in the array. How do I check that ? my @var = 1...10; my $var1 =5; if ( $var1 in @var ) { ....... } else { ....... } Something like above. Can some... (2 Replies)
Discussion started by: ammu
2 Replies

7. Shell Programming and Scripting

Perl - Compare 2 Arrays

Hi all, I have the following script where the contents of file1 and file2 would be something like this: file1: 56790,0,0,100998765 89756,0,0,100567876 867645,1,3,678777654 file2: 56790,0,0,100998765 65776,0,0,4766457890 +5896,0,0,675489876 What I then want to do is check if... (4 Replies)
Discussion started by: Donkey25
4 Replies

8. Shell Programming and Scripting

perl functions and arrays

Hi, First I will tell my objective of this function (function one). I have a table for ex: id passwd name -- ------ ----- 1 fdhgfs werwer 2 fsdfs sdfsdf 3 sdfs sdfsdf 4 fdsfs dssdf . . . . . . The id, passwd and name are the arguments for another function say two. (1 Reply)
Discussion started by: mercuryshipzz
1 Replies

9. Shell Programming and Scripting

SQL Data with Spaces into Arrays

Hi Simple thing has been driving me nuts. I have used the following code is ksh scripts to get data from Oracle table and then display it, allowing user to select one of the data options returned... REP_DATA=`sqlplus -s ${WMSDB} <<EOF SET SERVEROUTPUT ON FEEDBACK OFF... (0 Replies)
Discussion started by: mikem22
0 Replies

10. Programming

perl arrays

hello ppl, i'm coding a perl script and i have the following situation: @array1 = ("test1", "test2", "test3"); @array2 = ("something1", "something2", "something1"); $var1 = "with_one_of_the_array1_values"; $var2 = "with_one_of_the_array2_values"; what i want to do is to compare $var1... (2 Replies)
Discussion started by: crashnburn
2 Replies
Login or Register to Ask a Question