Because you are not resetting @array1, which is the one you are printing.

You've pushed two elements into @array1 and then you tell Perl to print both
of them at the end.

As usual, Perl does what you tell it to do.  (-:

Bryan





-----Original Message-----
From: AITHA, BHEEMSEN (SBCSI) [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, February 19, 2002 5:54 PM
To: 'Arnold, Craig'; '[EMAIL PROTECTED]';
[EMAIL PROTECTED]; '[EMAIL PROTECTED]'
Subject: RE: [Perl-unix-users] Help on array


Thanx to Arnold, Dave and Wilson for all your help. Your inputs were really
great. All the suggestions really worked well.

One final question

when I had used the command 
$#array2 = -1 ;

perl supposed to truncate all the elements in the array to nothing. But it
is not happening. It is still storing the previous values.

Here is my code

#!/usr/bin/perl -w
my @array2 ;
@array2=("abc,xyz,pqr");
my @array1 ;
$number=123456 ;
push @array1, sprintf("$number\t%s", join ",", @array2) ;
print @array1;
print "\n" ;
$#array2 = -1 ;
@array2=("qwe,rty,tyu");
push @array1, sprintf("$number\t%s", join ",", @array2) ;
print "\n" ;
print @array1;


Here is the output:

123456          abc,xyz,pqr
123456          abc,xyz,pqr123456       qwe,rty,tyu

I am not able to understand why it keeps printing the previous values.

Thanx for all the help.

-Bheem

-----Original Message-----
From: Arnold, Craig [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, February 19, 2002 7:50 AM
To: AITHA, BHEEMSEN (SBCSI)
Subject: RE: [Perl-unix-users] Please help on GREP command


Your push statement will work pretty much as-is.  All you need to get the
commas between the elements of the second array is a join:

push @array1, sprintf("$number\t%s", join ",", @array2) ;

You could also try:

push @array1, join "\t", $number, @array2 ;

You might get better results from the mail list if you give a broader
description of what you're trying to do and maybe some samples of the code.
Perl can work with any programming style, but it works best when you write
in a Perl style.  In any case, I recommend a book by Joseph Hall and Randal
Schwartz called "Effective Perl Programming."  It has a lot of tips and
tricks that will be very difficult to understand at first, but as you get
more familiar with the Perl way it will make life much easier.

Good luck!

Craig

-----Original Message-----
From: AITHA, BHEEMSEN (SBCSI) [mailto:[EMAIL PROTECTED]]
Sent: Monday, February 18, 2002 1:45 PM
To: 'Arnold, Craig'; '[EMAIL PROTECTED]'
Subject: RE: [Perl-unix-users] Please help on GREP command


Thanx Craig.

That worked. I have another question.

I would like to create an array in the folowing format:


123     abc,xyz,pqr
234     xyz
456     wer,tre
---
---

I want to use a command like the following

push @first_array, <the_number> <tab> <group of words separated by commas
from second_array>;

where the_number = values in 1st column coming from a variable in a loop.
The values in second column are from second_array. abc, xyz, pqr etc.. are
individual elements in second_array. I also don't know how to insert commas
in between the individual elements ( i.e. a comma between abc and xyz and so
on while creating the array)

Thanx..
-Bheem



-----Original Message-----
From: Arnold, Craig [mailto:[EMAIL PROTECTED]]
Sent: Monday, February 18, 2002 1:07 PM
To: AITHA, BHEEMSEN (SBCSI); '[EMAIL PROTECTED]'
Subject: RE: [Perl-unix-users] Please help on GREP command


You appear to be trying to use the command-line version of grep, rather than
a Perl grep.  To get what you appear to be looking for in Perl would require
opening the file:

Script:  grep.pl
        #!perl -w
        use strict ;

        my $fn = "test.txt" ;
        open(INFILE, $fn) or die "Can't open input file: $fn ($!)" ;

        my @match_lines = grep /abc|xyz|pqr/i, <INFILE> ;

        print @match_lines ;
        print scalar(@match_lines) ;
        print "Found it!\n" if @match_lines ;

        close INFILE ;

File:  test.txt
        ABC
        1121
        xyz
        2234
        pqr
        this is a test

Output:
        ABC
        xyz
        pqr
        3
        Found it!

You should avoid using the backtick operator because it is far slower than
the equivalent Perl code.  Also, the shell command (in this case grep) might
not be implemented the same on all platforms - the command you have listed
works on my HP-UX system, but doesn't work on my Win2k or Sun computers.
One last thing - you can do far more with Perl's grep than you can with the
shell grep.  See perldoc -f grep (and map) for more info.

Craig Arnold
Boeing Space & Comm
International Space Station
Guidance, Navigation & Control
Huntington Beach, CA

-----Original Message-----
From: AITHA, BHEEMSEN (SBCSI) [mailto:[EMAIL PROTECTED]]
Sent: Monday, February 18, 2002 11:23 AM
To: '[EMAIL PROTECTED]'
Subject: [Perl-unix-users] Please help on GREP command


Hi listers,

I am using the following command in my perl script. But when I execute the
script I am getting the error 

"Unrecognized file test: -E at test.pl line 9."

grep -E -i -c "abc|xyz|pqr" test.txt ;

Can someone please tell me what I am doing wrong here ?

All I wanted to do is to search for any of the stings abc, xyz or pqr in the
file test.txt.

Thanx..
-Bheem
_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to