how to do this in perl (from unix)
how to do this in perl (from unix)
am 02.09.2011 01:38:18 von Rajeev Prasad
--0-1662050703-1314920298=:48947
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
from linux:
=0Acut -f1,5- -d" " file |grep -v "^0" | sort -n > to_file=
; <======this line: how to=A0achieve this in perl?
=
=0Awill below work,=A0 in perl?
=0Aif ( ! -s $sourcedir/somefile=
){
=A0open(tmpFH,"<","file2");
@tmpAR =3D ;
close(tm=
pFH);
=A0push(@tmpAR2,$tmpAR[0],$tmpAR[5..]); =A0= A0 #=
select on columns 1 and 5 onwards from the original file
=A0my @tmpAR3=
=3D grep {!^[0 ]} @tmpAR2; =A0#omit line with 0 in =
the beginning
=A0@tmpAR3 =3D sort(@tmpAR3); =A0=
=A0# sort=A0on the first column=
}
we can then print tmpAR3 to a file.
=0Aplz. advice.=
thx.
--0-1662050703-1314920298=:48947--
Re: how to do this in perl (from unix)
am 02.09.2011 01:50:56 von Rajeev Prasad
--0-1796588088-1314921056=:46260
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
I tried this
open(tmpFH,"<","somefile");
@tmpAR =3D =
;
close(tmpFH);
push(my @tmpAR2,$tmpAR[0]);
=A0push (@tmpAR2=
,$tmpAR[5..-1]);
=A0my @tmpAR3 =3D grep {!/^0 /} @tmpAR2;
=A0@tmp=
AR3 =3D sort(@tmpAR3);
=A0print @tmpAR3;
=0Abut getting error:=0A=
Argument "" isn't numeric in array element at ./test.pl line 31.
outpu=
t is not coming correct, only first row of original file is coming.....=0A=
=0AFrom: Rajeev Prasad =0ATo: Perl Be=
ginners =0ASent: Thursday, September 1, 2011 6:38 PM=0A=
Subject: how to do this in perl (from unix)
from linux:
=0Acut -f=
1,5- -d" " file |grep -v "^0" | sort -n > to_file; <=====3D=
=3Dthis line: how to=A0achieve this in perl?
=0Awill below work,=
=A0 in perl?
=0Aif ( ! -s $sourcedir/somefile ){
=A0open(tmpFH,"<=
","file2");
@tmpAR =3D ;
close(tmpFH);
=A0push(@tmpAR=
2,$tmpAR[0],$tmpAR[5..]); #select on columns 1 and 5=
onwards from the original file
=A0my @tmpAR3 =3D grep {!^[0 ]} @tmpAR=
2; =A0#omit line with 0 in the beginning
=A0@tm=
pAR3 =3D sort(@tmpAR3); =A0= A0=A0=
=A0# sort=A0on the first column
}
we can then p=
rint tmpAR3 to a file.
=0Aplz. advice. thx.
--0-1796588088-1314921056=:46260--
Re: how to do this in perl (from unix)
am 02.09.2011 02:03:41 von Parag Kalra
Please give a better problem statement like what is the form of data
initially present in the file and what you wish to extract.
You should also post sample file data for both the files (before and after)
Not all people who know Perl might know Unix commands (rare but
possible) and so they may not be able to construct the requirement.
Thanks,
Parag
On Thu, Sep 1, 2011 at 4:38 PM, Rajeev Prasad wrote:
> from linux:
>
> cut -f1,5- -d" " file |grep -v "^0" | sort -n > to_file;Â Â <=3D=
=====3Dthis line: how to achieve this in perl?
>
>
> will below work, in perl?
>
> if ( ! -s $sourcedir/somefile ){
>
> Â open(tmpFH,"<","file2");
> Â @tmpAR =3D ;
> Â close(tmpFH);
>
> Â push(@tmpAR2,$tmpAR[0],$tmpAR[5..]);Â Â Â Â Â =
   #select on columns 1 and 5 onwards from the original file
>
> Â my @tmpAR3 =3D grep {!^[0 ]} @tmpAR2;Â Â Â Â Â =
    #omit line with 0 in the beginning
>
> Â @tmpAR3 =3D sort(@tmpAR3);Â Â Â Â Â Â =C2=
           Â=A0=
     # sort on the first column
> Â }
>
> we can then print tmpAR3 to a file.
>
>
> plz. advice. thx.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how to do this in perl (from unix)
am 02.09.2011 02:15:04 von Rajeev Prasad
--0-1798418641-1314922504=:71520
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
tried further, still no resolution:
=0Afile1 =3D (space seperated valu=
es)=0Aoutputfile =3D (colum1, and 5 onwards)=A0 field seperator would be sp=
ace.
=0Afrom linux:=A0 cut -f1,5- -d" " file |grep -v "^0" | sort -n >=
to_file;
open(tmpFH,"<","file1");
@tmpAR =3D <=
tmpFH>;
close(tmpFH);
foreach $line (@tmpAR) {
=A0@tmpAR2 =
=3D split(/ /,"$line");
=A0$line2 =3D "$tmpAR[0] $tmpAR[5..-1]";
=
=A0push(@tmpAR3,$line2);
};
@tmpAR3 =3D sort(@tmpAR3);
print=
@tmpAR3;
From: Rajeev Prasad =
=0ATo: Rajeev Prasad ; Perl Beginners
rg>=0ASent: Thursday, September 1, 2011 6:50 PM=0ASubject: Re: how to do th=
is in perl (from unix)
=0AI tried this
open(tmpFH,"<","some=
file");
@tmpAR =3D ;
close(tmpFH);
push(my @tmpAR2,$t=
mpAR[0]);
=A0push (@tmpAR2,$tmpAR[5..-1]);
=A0my @tmpAR3 =3D grep=
{!/^0 /} @tmpAR2;
=A0@tmpAR3 =3D sort(@tmpAR3);
=A0print @tmpAR3=
;
=0Abut getting error:=0AArgument "" isn't numeric in array element a=
t ./test.pl line 31.
output is not coming correct, only first row of o=
riginal file is coming.....
From: Rajeev Prasad
neuli@yahoo.com>=0ATo: Perl Beginners =0ASent: Thursday=
, September 1, 2011 6:38 PM=0ASubject: how to do this in perl (from unix)=
from linux:
=0Acut -f1,5- -d" " file |grep -v "^0" | sort -n > t=
o_file; <======this line: how to=A0achieve this in perl?=
=0Awill below work,=A0 in perl?
=0Aif ( ! -s $sourcedir/so=
mefile ){
=A0open(tmpFH,"<","file2");
@tmpAR =3D ;
cl=
ose(tmpFH);
=A0push(@tmpAR2,$tmpAR[0],$tmpAR[5..]);=A0= A0 =
#select on columns 1 and 5 onwards from the original file
=A0my =
@tmpAR3 =3D grep {!^[0 ]} @tmpAR2; =A0#omit line wit=
h 0 in the beginning
=A0@tmpAR3 =3D sort(@tmpAR3); =
# sort=A0on the first=
column
}
we can then print tmpAR3 to a file.
=0Aplz. =
advice. thx.
--0-1798418641-1314922504=:71520--
Re: how to do this in perl (from unix)
am 02.09.2011 02:22:31 von Jim Gibson
On 9/1/11 Thu Sep 1, 2011 4:38 PM, "Rajeev Prasad"
scribbled:
> from linux:
> =A0
> cut -f1,5- -d" " file |grep -v "^0" | sort -n > to_file; <======this li=
ne:
> how to=A0achieve this in perl?
> =A0
> =A0
> will below work,=A0 in perl?
> =A0
> if ( ! -s $sourcedir/somefile ){
>=20
> =A0open(tmpFH,"<","file2");
> =A0@tmpAR =3D ;
> =A0close(tmpFH);
>=20
> =A0push(@tmpAR2,$tmpAR[0],$tmpAR[5..]); =A0 =A0#select on columns 1 and 5
The array @tmpAR contains the lines of the original file, one line per
element. You need to select part of each line. The above line selects whole
lines (lines 0, 5, 6, 7, ...).
If the columns in your original file are separated by whitespace or other
characters that do not appear in the fields, you can use the split function=
:
my @fields =3D split(' ',$tmpAR[0]);
my $newline =3D join(' ',@tmpAR[0,5..$#fields]);
push(@tmpAR2,$newline);
> =A0my @tmpAR3 =3D grep {!^[0 ]} @tmpAR2; =A0#omit line with 0 in the
> beginning
I would try
my @tmpAR3 =3D grep { $_ !=3D /^0/ } @tmpAR2;
>=20
> =A0@tmpAR3 =3D sort(@tmpAR3); =A0= A0 # sort=A0on the first colu=
mn
If you want a numerical sort, you will have to use an explicit sort
comparator:
my @tmpAR4 =3D sort { $a <=3D> $b } @tmpAR3;
> =A0}
>=20
> we can then print tmpAR3 to a file.
If you need more help, please consider:
1. Including a complete, short, runnable program that demonstrates the
problem you are having, and that include data using the file handle.
2. Some indication of what you expect this program to do and why it does
not meet your expectations.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how to do this in perl (from unix)
am 02.09.2011 03:01:13 von Jim Gibson
On 9/1/11 Thu Sep 1, 2011 5:15 PM, "Rajeev Prasad"
scribbled:
> tried further, still no resolution:
Please use complete sentences in your posts. You are asking people to help
you for free, so please give them the courtesy of using proper language.
> =A0
> file1 =3D (space seperated values)
> outputfile =3D (colum1, and 5 onwards)=A0 field seperator would be space.
> =A0
> from linux:=A0 cut -f1,5- -d" " file |grep -v "^0" | sort -n > to_file;
> =A0
> =A0
> =A0open(tmpFH,"<","file1");
> =A0@tmpAR =3D ;
> =A0close(tmpFH);
> =A0foreach $line (@tmpAR) {
> @tmpAR2 =3D split(/ /,"$line");
There is no need to surround $line with double-quotes:
@tmpAR2 =3D split(/ /,$line);
Note that the split will delete the newline character from the last element
of @tmpAR2.
> $line2 =3D "$tmpAR[0] $tmpAR[5..-1]";
The range [5..-1] is empty, since -1 is less than 5. You need to use the
last index value here. Also, since an array slice is an array, it should us=
e
the '@' sigil, not the '$' for scalars. You can also add back the newline:
$line2 =3D "$tmpAR[0] $tmpAR[5..$#tmpAR]\n";
> push(@tmpAR3,$line2);
> =A0};
> =A0@tmpAR3 =3D sort(@tmpAR3);
> =A0print @tmpAR3;
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how to do this in perl (from unix)
am 02.09.2011 03:07:40 von jwkrahn
Rajeev Prasad wrote:
> from linux:
>
> cut -f1,5- -d" " file |grep -v "^0" | sort -n> to_file;<======this line: how to achieve this in perl?
open IN_FH, '<', 'file' or die "Cannot open 'file' because: $!";
open OUT_FH, '>', 'to_file' or die "Cannot open 'to_file' because: $!";
print OUT_FH sort { $a <=> $b } map join( " ", ( split / +/, $_, 5 )[ 0,
-1 ] ), ;
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how to do this in perl (from unix)
am 02.09.2011 03:14:48 von jwkrahn
Jim Gibson wrote:
> On 9/1/11 Thu Sep 1, 2011 5:15 PM, "Rajeev Prasad"
> scribbled:
>>
>> @tmpAR2 = split(/ /,"$line");
>
> There is no need to surround $line with double-quotes:
>
> @tmpAR2 = split(/ /,$line);
>
> Note that the split will delete the newline character from the last element
> of @tmpAR2.
No it will not. The regular expression / / will only match the space
character.
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how to do this in perl (from unix)
am 02.09.2011 03:19:48 von jwkrahn
John W. Krahn wrote:
> Rajeev Prasad wrote:
>> from linux:
>>
>> cut -f1,5- -d" " file |grep -v "^0" | sort -n> to_file;<======this
>> line: how to achieve this in perl?
>
>
> open IN_FH, '<', 'file' or die "Cannot open 'file' because: $!";
> open OUT_FH, '>', 'to_file' or die "Cannot open 'to_file' because: $!";
>
> print OUT_FH sort { $a <=> $b } map join( " ", ( split / +/, $_, 5 )[ 0,
> -1 ] ), ;
Correction! I forgot the grep part:
open IN_FH, '<', 'file' or die "Cannot open 'file' because: $!";
open OUT_FH, '>', 'to_file' or die "Cannot open 'to_file' because: $!";
print OUT_FH sort { $a <=> $b } map /^0/ ? () : join( ' ', ( split / +/,
$_, 5 )[ 0, -1 ] ), ;
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how to do this in perl (from unix)
am 02.09.2011 03:26:31 von Owen
> from linux:
> =A0
> cut -f1,5- -d" " file |grep -v "^0" | sort -n > to_file; <===3D=
===3Dthis
> line: how to=A0achieve this in perl?
Perhaps try something like
system (cut -f1,5- -d" " file |grep -v "^0" | sort -n > to_file);
You may need to replace cut etc with /usr/bin/cut etc
--
Owen
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how to do this in perl (from unix)
am 02.09.2011 06:29:57 von Jim Gibson
At 6:14 PM -0700 9/1/11, John W. Krahn wrote:
>Jim Gibson wrote:
>>On 9/1/11 Thu Sep 1, 2011 5:15 PM, "Rajeev Prasad"
>>scribbled:
>>>
>>> @tmpAR2 = split(/ /,"$line");
>>
>>There is no need to surround $line with double-quotes:
>>
>> @tmpAR2 = split(/ /,$line);
>>
>>Note that the split will delete the newline character from the last element
>>of @tmpAR2.
>
>No it will not. The regular expression / / will only match the
>space character.
Thanks for the correction. I originally suggested that Rajeev use
split(' ',...) and didn't notice that he had used split(/ /,...)
instead until after I had posted.
Beginning Perl programmers should note the difference between ' ' and
/ / for split. In most cases, it doesn't matter. However, the split
pattern ' ' is a special case that splits on any whitespace, which
includes the space character, the tab character, and the newline
character. It also changes the rules on what split does if there are
separator characters at the beginning or end of the string and what
split does if there are consecutive space characters in the string.
See 'perldoc -f split' and search for 'PATTERN of space'.
--
Jim Gibson
Jim@Gibson.org
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how to do this in perl (from unix)
am 02.09.2011 06:54:39 von John Riselvato
--00151747b008a6088e04abee28ad
Content-Type: text/plain; charset=ISO-8859-1
>
>
> >> Beginning Perl programmers should note the difference between ' ' and /
> / for split. In most cases, it doesn't matter. However, the split pattern '
> ' is a special case that splits on any whitespace, which includes the space
> character, the tab character, and the newline character. It also changes the
> rules on what split does if there are separator characters at the beginning
> or end of the string and what split does if there are consecutive space
> characters in the string.
>
> >> See 'perldoc -f split' and search for 'PATTERN of space'.
>
>
Thank you Jim, This is exactly why I signed up for the perl list. To get
great information like this.
--00151747b008a6088e04abee28ad--
Re: how to do this in perl (from unix)
am 02.09.2011 08:05:26 von jwkrahn
Jim Gibson wrote:
> At 6:14 PM -0700 9/1/11, John W. Krahn wrote:
>> Jim Gibson wrote:
>>> On 9/1/11 Thu Sep 1, 2011 5:15 PM, "Rajeev Prasad"
>>> scribbled:
>>>>
>>>> @tmpAR2 = split(/ /,"$line");
>>>
>>> There is no need to surround $line with double-quotes:
>>>
>>> @tmpAR2 = split(/ /,$line);
>>>
>>> Note that the split will delete the newline character from the last
>>> element
>>> of @tmpAR2.
>>
>> No it will not. The regular expression / / will only match the space
>> character.
>
> Thanks for the correction. I originally suggested that Rajeev use
> split(' ',...) and didn't notice that he had used split(/ /,...) instead
> until after I had posted.
>
> Beginning Perl programmers should note the difference between ' ' and /
> / for split. In most cases, it doesn't matter. However, the split
> pattern ' ' is a special case that splits on any whitespace, which
> includes the space character, the tab character, and the newline
> character. It also changes the rules on what split does if there are
> separator characters at the beginning or end of the string
No, just at the beginning.
> and what
> split does if there are consecutive space characters in the string.
>
> See 'perldoc -f split' and search for 'PATTERN of space'.
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how to do this in perl (from unix)
am 02.09.2011 19:15:33 von Rajeev Prasad
--0-647362506-1314983733=:57116
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Thank you John,
=0Athat is almost perfect. only two issues:
=0A1.=
=A0getting error on sort for each line of input, column 5 onwards is text.=
...:
Argument "154 Overload Status\n" isn't numeric in sort at ./test=
..pl line 30, line 208.=0A2.=A0 by chaniging the column number in jo=
in (...[0,-1]) to (...[2,-1]) I am not getting column 2 and 5 in outp=
ut, it still send column 1 and 5 in output file.
=0Aagain, that was co=
ol.=0A-Rajeev
=0Afrom Linux:=0Acut -f1,5- -d" " file |grep -v "^=
0" | sort -n> to_file
to perl:=0Aopen IN_FH,=A0 '<', 'file' =A0 o=
r die "Cannot open 'file' because: $!";=0Aopen OUT_FH, '>', 'to_file' or di=
e "Cannot open 'to_file' because: $!";=0Aprint OUT_FH sort { $a <=3D> $b } =
map /^0/ ? () : join( ' ', ( split / +/, $_, 5 )[ 0, -1 ] ), ;
=
I tried to put my understanding in words:
1. 0. operations starts from=
right most part of statement (statement ends with ;)
2. split = b=
reaks the line (contained in $_)
/ +/ = any number of white space=
$_ = the current line in in-built perl string variable
5=A0=
=3D number of fields string will be split into
3. join =3D joins =
the new resulting string from eariler split
' ' = uses single spac=
e as seperator
[0,-1] = element 0 and -1 of array repersented by (=
split...)
4. map =3D <===============
==================== =====3D=
==not able to understand map (map expr, list )
/^0/ = expressi=
on to check wether line starts with 0
? = if line starts with zero=
then ??? not sure....
() = not sure...
: = not sure...=
5. sort =3D sorts the final array on first column (which is by t=
he way numerical)
6. OUT_FH = writes the outcome to the file_handl=
e.
From: John W. Krahn =0ATo: Perl Beginners
eginners@perl.org>=0ASent: Thursday, September 1, 2011 8:19 PM=0ASubject: R=
e: how to do this in perl (from unix)
John W. Krahn wrote:=0A> Rajeev =
Prasad wrote:=0A>> from linux:=0A>>=0A>> cut -f1,5- -d" " file |grep -v "^0=
" | sort -n> to_file;<======this=0A>> line: how to achieve this=
in perl?=0A>=0A>=0A> open IN_FH, '<', 'file' or die "Cannot open 'file' be=
cause: $!";=0A> open OUT_FH, '>', 'to_file' or die "Cannot open 'to_file' b=
ecause: $!";=0A>=0A> print OUT_FH sort { $a <=3D> $b } map join( " ", ( spl=
it / +/, $_, 5 )[ 0,=0A> -1 ] ), ;
Correction!=A0 I forgot the =
grep part:
open IN_FH,=A0 '<', 'file'=A0 =A0 or die "Cannot open 'file=
' because: $!";=0Aopen OUT_FH, '>', 'to_file' or die "Cannot open 'to_file'=
because: $!";
print OUT_FH sort { $a <=3D> $b } map /^0/ ? () : join(=
' ', ( split / +/, =0A$_, 5 )[ 0, -1 ] ), ;
=0AJohn=0A--=
=0AAny intelligent fool can make things bigger and=0Amore complex... It ta=
kes a touch of genius -=0Aand a lot of courage to move in the opposite=0Adi=
rection.=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- Albert Einstein
-- =0AT=
o unsubscribe, e-mail: beginners-unsubscribe@perl.org=0AFor additional comm=
ands, e-mail: beginners-help@perl.org=0Ahttp://learn.perl.org/
--0-647362506-1314983733=:57116--
Re: how to do this in perl (from unix)
am 02.09.2011 21:12:08 von jwkrahn
Rajeev Prasad wrote:
> Thank you John,
>
> that is almost perfect. only two issues:
>
> 1. getting error on sort for each line of input, column 5 onwards is text...:
> Argument "154 Overload Status\n" isn't numeric in sort at ./test.pl line 30, line 208.
> 2. by chaniging the column number in join (...[0,-1]) to (...[2,-1]) I am not getting column 2 and 5 in output, it still send column 1 and 5 in output file.
>
> again, that was cool.
> -Rajeev
>
>
> from Linux:
> cut -f1,5- -d" " file |grep -v "^0" | sort -n> to_file
> to perl:
> open IN_FH, '<', 'file' or die "Cannot open 'file' because: $!";
> open OUT_FH, '>', 'to_file' or die "Cannot open 'to_file' because: $!";
> print OUT_FH sort { $a<=> $b } map /^0/ ? () : join( ' ', ( split / +/, $_, 5 )[ 0, -1 ] ),;
>
> I tried to put my understanding in words:
> 1. 0. operations starts from right most part of statement (statement ends with ;)
> 2. split = breaks the line (contained in $_)
> / +/ = any number of white space
> $_ = the current line in in-built perl string variable
> 5 = number of fields string will be split into
> 3. join = joins the new resulting string from eariler split
> ' ' = uses single space as seperator
> [0,-1] = element 0 and -1 of array repersented by (split...)
> 4. map =<=========================================not able to understand map (map expr, list )
> /^0/ = expression to check wether line starts with 0
> ? = if line starts with zero then ??? not sure....
> () = not sure...
> : = not sure...
> 5. sort = sorts the final array on first column (which is by the way numerical)
> 6. OUT_FH = writes the outcome to the file_handle.
The flow goes from to map to sort to print.
reads the file lines and passes the list of lines to map.
map has each line in $_. This is matched with /^0/ and if it does match
then return the empty list and if it doesn't match remove columns 2, 3
and 4 and then pass the resulting list to sort
sort does a numerical sort and then passes the sorted list to print.
print prints out the list to the filehandle OUT_FH.
To remove the error message you could do this:
{ no warnings;
print OUT_FH sort { $a <=> $b } map /^0/ ? () : join( ' ', ( split
/ +/, $_, 5 )[ 0, -1 ] ), ;
}
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how to do this in perl (from unix)
am 02.09.2011 22:28:49 von Rajeev Prasad
--0-137239695-1314995329=:42290
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
unfortunately it was not working as i intended.
=0Aso i have new=
code with some help now:=0Ausing $column variable, i can control which col=
umn (from 1 to 4) i want to be printed with column 5.
=0Aopen(IN_FH,"<=
","infile") or die "Cannot open=A0infile because: $!";=0Aopen(OUT_FH,">","o=
utfile") or die "Cannot open outfile because: $!";=0Aforeach $line (=
){
@tmpAR=3Dsplit(/ /,$line,5); =
=A0 #/ +=
/ will split at any number of white spaces...
next if $tmpAR[$c=
olumn] == 0;
push(@tmpAR2,"$tmpAR[$column] $tmpAR[4]");=A0=
=A0 =A0# we can use -1 here as last element of array
}=0Aprint O=
UT_FH sort { (split(/ /,$a))[0] <=3D> (split(/ /,$b))[0] } @tmpAR2;=0Aclose=
IN_FH;=0Aclose OUT_FH;
=0Asample data in infile:=0A1 0 0 0 some=
text=0A0 2 0 3 text....=0A3 1 4 2 text....=0A4 3 1 1 text......
From:=
John W. Krahn =0ATo: Perl Beginners =
=0ASent: Friday, September 2, 2011 2:12 PM=0ASubject: Re: how to do this in=
perl (from unix)
Rajeev Prasad wrote:=0A> Thank you John,=0A> =0A> th=
at is almost perfect. only two issues:=0A> =0A> 1.=A0 getting error on sort=
for each line of input, column 5 onwards is text...:=0A>=A0 =A0 Argument "=
154 Overload Status\n" isn't numeric in sort at ./test.pl line 30,=
=A0 line 208.=0A> 2.=A0 by chaniging the column number in join (...[0,-1]) =
to=A0 (...[2,-1]) I am not getting column 2 and 5 in output, it still send =
column 1 and 5 in output file.=0A> =0A> again, that was cool.=0A> -Rajeev=
=0A> =0A> =0A> from Linux:=0A> cut -f1,5- -d" " file |grep -v "^0" | sort -=
n>=A0 to_file=0A> to perl:=0A> open IN_FH,=A0 '<', 'file'=A0 =A0 or die "Ca=
nnot open 'file' because: $!";=0A> open OUT_FH, '>', 'to_file' or die "Cann=
ot open 'to_file' because: $!";=0A> print OUT_FH sort { $a<=3D>=A0 $b } map=
/^0/ ? () : join( ' ', ( split / +/, $_, 5 )[ 0, -1 ] ),;=0A> =0A> =
I tried to put my understanding in words:=0A> =A0 1. 0. operations st=
arts from right most part of statement (statement ends with ;)=0A> =
=A0 2. split =3D breaks the line (contained in $_)=0A>=A0 / +/ =3D any numb=
er of white space=0A>=A0 $_ =3D the current line in in-built perl string va=
riable=0A>=A0 5 =3D number of fields string will be split into=0A> =
=A0 3. join=A0 =3D joins the new resulting string from eariler split=0A>=A0=
' ' =3D uses single space as seperator=0A>=A0 [0,-1] =3D element 0 and -1 =
of array repersented by (split...)=0A> =A0 4. map=A0 =3D<=====
==================== =====3D=
============not able to understand map (map expr, l=
ist )=0A>=A0 /^0/ =3D expression to check wether line starts with 0=0A>=A0 =
? =3D if line starts with zero then ??? not sure....=0A>=A0 () =3D not sure=
....=0A>=A0 : =3D not sure...=0A> =A0 5. sort=A0 =3D sorts the final a=
rray on first column (which is by the way numerical)=0A> =A0 6. OUT_F=
H =3D writes the outcome to the file_handle.
The flow goes from
> to map to sort to print.
reads the file lines and passes the=
list of lines to map.
map has each line in $_.=A0 This is matched wit=
h /^0/ and if it does match then return the empty list and if it doesn't ma=
tch remove columns 2, 3 and 4 and then pass the resulting list to sort=0A=
=0Asort does a numerical sort and then passes the sorted list to print.=0A=
=0Aprint prints out the list to the filehandle OUT_FH.
=0ATo remove th=
e error message you could do this:
{=A0 no warnings;
=A0 print O=
UT_FH sort { $a <=3D> $b } map /^0/ ? () : join( ' ', ( split / +/, $_, 5 )=
[ 0, -1 ] ), ;
=A0 }
John=0A-- Any intelligent fool=
can make things bigger and=0Amore complex... It takes a touch of genius -=
=0Aand a lot of courage to move in the opposite=0Adirection.=A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 -- Albert Einstein
-- To unsubscribe, e-mail: beg=
inners-unsubscribe@perl.org=0AFor additional commands, e-mail: beginners-he=
lp@perl.org=0Ahttp://learn.perl.org/
--0-137239695-1314995329=:42290--
Re: how to do this in perl (from unix)
am 03.09.2011 08:57:25 von Shlomi Fish
Hi Rajeev,
some comments on your code:
On Fri, 2 Sep 2011 13:28:49 -0700 (PDT)
Rajeev Prasad wrote:
> Â
> unfortunately it was not working as i intended.
> Â
> so i have new code with some help now:
> using $column variable, i can control which column (from 1 to 4) i want t=
o be
> printed with column 5.=20
Add "strict" and "warnings".
> open(IN_FH,"<","infile") or die "Cannot open infile because: $!";
> open(OUT_FH,">","outfile") or die "Cannot open outfile because: $!";
Always use lexical filehandles:=20
[CODE]
open(my $in_fh,"<","infile") or die "Cannot open infile because: $!";
[/CODE]
But it's good that you've used three-args open followed by "or die".
> foreach $line (){
that should be:
while (my $line =3D <$in_fh>) {
With:
1. while instead of a foreach - consumes less memory.
2. Lexically scoped $line variable.
3. space before the "{".
> Â Â Â @tmpAR=3Dsplit(/ /,$line,5);Â Â Â Â =
            =C2=
           Â=A0=
    #/ +/ will
> split at any number of white spaces...
Please call the array with a more meaningful name than @tmpAR. Also declare=
it
using "my" (strict would have warned you about that.).
=20
> next if $tmpAR[$column] == 0;
Where is $column defined? And it should be $column_idx or something.
I personally prefer to label all next/last/redo statements, but some people
here disagree.
> Â Â Â push(@tmpAR2,"$tmpAR[$column] $tmpAR[4]");Â Â =
   # we can use -1 here as
> last element of array
@tmpAR and @tmpAR2? Oh god... Please use more meaningful variable names.
Also, you should use array references here:
[CODE]
push @all_records, [@columns[$column, 4]];
[/CODE]
> }=20
> print OUT_FH sort { (split(/ /,$a))[0] <=3D>
> (split(/ /,$b))[0] } @tmpAR2; close IN_FH;
> close OUT_FH;
This could be easier done using the array of array refs and a map to string=
ify
them.
For more advice, see:
http://perl-begin.org/tutorials/bad-elements/
Sorry for mangling the reply.
Regards,
Shlomi Fish
--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
"Star Trek: We, the Living Dead" - http://shlom.in/st-wtld
Bill Gates, CEO of Microsoft decides to use Richard Stallmanâ=99s Emac=
s as the
basis of his companyâ=99s stateâ=90ofâ=90theâ=90art pro=
duct Microsoft Editing Macrosâ=A2
Enterprise Edition XP .NET Professional.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/