Perl Forum for Beginners

Read column

Mar 14, 2012 10:51 am
Torqued

I have a code which goes like this

#!/usr/bin/perl -w
use strict;

my @column;
while (<DATA>) {
push @column, (split)[0];
print join (' ', @column);
}

__DATA__
1 a b c
2 d e f
3 g h i

But when running this i get the output as : 11 21 2 3
Whereas i am trying to get the output as : 1 2 3

could you please suggest what i am doing wrong here?

Regards.../om

Mar 14, 2012 11:19 am
Timothy adigun
Re: Read column

--e89a8ffbaea326e05b04bb322578
Content-Type: text/plain; charset=ISO-8859-1

Hi Torqued,

On Wed, Mar 14, 2012 at 11:51 AM, Torqued <torque.india@gmail.com> wrote:

> I have a code which goes like this
>
> #!/usr/bin/perl -w
> use strict;
>
> my @column;
>
my $k;

> while (<DATA>) {
> push @column, (split)[0];
> #print join (' ', @column);
>
$k=join (' ', @column);

> }
>
print $k;

> ***OUTPUT**
>
1 2 3


> __DATA__
> 1 a b c
> 2 d e f
> 3 g h i
>
> But when running this i get the output as : 11 21 2 3
> Whereas i am trying to get the output as : 1 2 3
>
> could you please suggest what i am doing wrong here?
>
> Regards.../om
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


Tim
--e89a8ffbaea326e05b04bb322578--
Mar 14, 2012 11:24 am
Timothy adigun
Re: Read column

--047d7b33d85eb9740d04bb32370c
Content-Type: text/plain; charset=ISO-8859-1

OR use:
#!/usr/bin/perl -w
use strict;

my @column;
while (<DATA>) {
push @column, (split)[0];
}
print join " ",@column;

__DATA__
1 a b c
2 d e f
3 g h i

On Wed, Mar 14, 2012 at 12:19 PM, timothy adigun <2teezperl@gmail.com>wrote:

> Hi Torqued,
>
> On Wed, Mar 14, 2012 at 11:51 AM, Torqued <torque.india@gmail.com> wrote:
>
>> I have a code which goes like this
>>
>> #!/usr/bin/perl -w
>> use strict;
>>
>> my @column;
>>
> my $k;
>
>> while (<DATA>) {
>> push @column, (split)[0];
>> #print join (' ', @column);
>>
> $k=join (' ', @column);
>
>> }
>>
> print $k;
>
>> ***OUTPUT**
>>
> 1 2 3
>
>
>> __DATA__
>> 1 a b c
>> 2 d e f
>> 3 g h i
>>
>> But when running this i get the output as : 11 21 2 3
>> Whereas i am trying to get the output as : 1 2 3
>>
>> could you please suggest what i am doing wrong here?
>>
>> Regards.../om
>>
>> --
>> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
>> For additional commands, e-mail: beginners-help@perl.org
>> http://learn.perl.org/
>>
>>
>>
>
>
> --
> Tim
>
>


Tim
--047d7b33d85eb9740d04bb32370c--
Mar 14, 2012 11:37 am
Torqued
Re: Read column

--Apple-Mail-9AF0244B-B415-4FA5-B7B4-224F3A0DE316
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=us-ascii

It worked like charm, but a quick question, why the print is misbehaving und=
er the loop, but once out how it print right result.

Regards.../om

On 14-Mar-2012, at 16:54, timothy adigun <2teezperl@gmail.com> wrote:

> OR use:
> #!/usr/bin/perl -w
> use strict;
>=20
> my @column;
> while (<DATA>) {
> push @column, (split)[0]; =20
> }
> print join " ",@column;
>=20
> __DATA__
> 1 a b c
> 2 d e f
> 3 g h i
>=20
> On Wed, Mar 14, 2012 at 12:19 PM, timothy adigun <2teezperl@gmail.com> wro=
te:
> Hi Torqued,
>=20
> On Wed, Mar 14, 2012 at 11:51 AM, Torqued <torque.india@gmail.com> wrote:
> I have a code which goes like this
>=20
> #!/usr/bin/perl -w
> use strict;
>=20
> my @column;
> my $k;=20
> while (<DATA>) {
> push @column, (split)[0];
> #print join (' ', @column);
> $k=3Djoin (' ', @column);
> }
> print $k;=20
> ***OUTPUT**
> 1 2 3
> =20
> __DATA__
> 1 a b c
> 2 d e f
> 3 g h i
>=20
> But when running this i get the output as : 11 21 2 3
> Whereas i am trying to get the output as : 1 2 3
>=20
> could you please suggest what i am doing wrong here?
>=20
> Regards.../om
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>=20
>=20
>=20
>=20
>=20
> --=20
> Tim
>=20
>=20
>=20
>=20
> --=20
> Tim
>=20

--Apple-Mail-9AF0244B-B415-4FA5-B7B4-224F3A0DE316--




Previous Thread: How to put the match in an array
Next Thread: Install modules from CPAN

Related Forum Topics
Writing a column after column
Hi all,

I need to consolidate columns of data available across different
directories into a single excel csv file. Usually we write to a file
row after row but for the current task I have, it would be convenient
to write the file column after column. Is there a file writing mode
for...
DBI: Add column to mysql table
Hi,

I am using DBI to add a column to my existing table, goodtable. W/ a
phpmyadmin I can add a column w/ a numeric or nonnumeric label. Using
perl DBI, I can only add a column w/ a nonnumeric label, as follows:

$dbh->do("ALTER TABLE goodtable ADD testname VARCHAR( 250 )...
Adding namespace to row and column
Dear all,

I am already having a XML document. In that document i want add
namespace to an element. How to achieve this.

Regards,
Ganesh


Multi-column array
I've been trying to figure out how to print 2 arrays into 2 columns. I
came across this code and it works but gives me a problem in the
output.

*************************BEGIN CODE*********************************************
sub transpose {
map { my $i = $_; [ map $_->[ $i ], @_ ] } 0...
Merge two files with similar column entries
--f46d04308af08fb48304bd320f79
Content-Type: text/plain; charset=ISO-8859-1

Hi ,

I have few files which contains user name and data transfer rate in MBs and
this data is collected for year and for each month report is saved in 12
different files I have to merge all the files to prepare...
Rewriting a single column in an open file... more efficient IO
Hello everyone,

I would like to learn an efficient way to change a single column in a
file that is accessed by an external program after the column is
changed each time. open write close is what I have been using. I
thought that tieing could help speed it up. While I didn't dig in...
How to read from keyboard?
------=_Part_7256_28608499.1204121090558
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi.

I want to read from keyboard some keys [pagedown],
[Home],[End][PageUp][up][left][down][right].

how can I do it?

Thanks in...
.profile not being read
I just installed perl on my Mac following the instructions on the perl.org website. It seems to work, as "perl -v" returns "This is perl 5, version 16, subversion 0 (v5.16.0) built for darwin-2level..."

The thing is that the commands in my .profile file are no longer being executed. The file...
Read and write
$ perl extract_v2.pl try.tex
Useless use of a constant (<) in void context at extract_v2.pl line 25.
Useless use of private variable in void context at extract_v2.pl line 25.
metabolism
toxic2
toxic4

#!/usr/bin/env perl

use strict;
use warnings;
use File::Spec;
use...
Read file attributes
hi,

i need to access the attributes of file.
more precisely, i need to check if the file is digitally signed or not


for example; if i right click on file, then i need to check if the "digital signature" tab is there or not for that specific file and valid certificate is there.

please...