help to print a hash with hash

help to print a hash with hash

am 14.07.2011 14:09:41 von Mohan L

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

Dear All,

I have a hash %region_data which looks like :

$VAR1 = {
'south' => {
'status' => {
'open' => {
'count' => 3
},
'pws' => {
'count' =>
3
},
'wip' => {
'count' => 0
},
'hold' => {
'count' => 1
},
're-open' => {
'count' => 0
},
'pwu' => {
'count' => 0
},
'openesc' => {
'count' => 0
}
},
'total_count' => 7
},
'north' => {
'status' => {
'open' => {
'count' => 3
},
'pws' => {
'count' =>
0
},
'wip' => {
'count' => 0
},
'hold' => {
'count' => 7
},
're-open' => {
'count' => 0
},
'pwu' => {
'count' => 0
},
'openesc' => {
'count' => 0
}
},
'total_count' => 10
},
}

I want to insert the hash values like this in mysql data base

region open pws wip hold re-open pwu openesc total_coun
south 3 3 0 1 0 0 0 7
north 3 0 0 7 0 0 0 10

using below query:
my $query = "INSERT INTO by_region
(region,open,pws,wip,hold,re-open,pwu,openesc,total_count) VALUES
(?,?,?,?,?,?,?,?,?)";

I am trying some thing like to print :

foreach my $line (keys %region_data)
{
print "$line\n";
foreach my $item (keys %{$region_data {$line}})
{
print "$item\n";
}
}

Output :

south
status
total_count
north
status
total_count
west
status
total_count
east
status
total_count

But I don't know how to print next level. I need someone help to point
right direction.

Thanks & Rg
Mohan L

--20cf300fb22363d21904a8066828--

Re: help to print a hash with hash

am 14.07.2011 18:00:24 von rvtol+usenet

On 2011-07-14 14:09, Mohan L wrote:

> I am trying some thing like to print :
>
> foreach my $line (keys %region_data)
> {
> print "$line\n";
> foreach my $item (keys %{$region_data {$line}})
> {
> print "$item\n";
> }
> }
>
> Output :
>
> south
> status
> total_count
> north
> status
> total_count
> west
> status
> total_count
> east
> status
> total_count
>
> But I don't know how to print next level. I need someone help to point
> right direction.

#!/usr/bin/perl
use strict;
use warnings;

my %data = (
south => {
status => {
open => { count => 3 },
pws => { count => 3 },
wip => { count => 0 },
hold => { count => 1 },
're-open' => { count => 0 },
pwu => { count => 0 },
openesc => { count => 0 },
},
total_count => 7,
},

north => {
status => {
open => { count => 3 },
pws => { count => 0 },
wip => { count => 0 },
hold => { count => 7 },
're-open' => { count => 0 },
pwu => { count => 0 },
openesc => { count => 0 },
},
total_count => 10,
},
);

my @items = qw/ open pws wip hold re-open pwu openesc /;

print join( "\t", "region", @items, "total_count" ), "\n";

for my $region ( sort keys %data ) {
my $d = $data{ $region };
print join( "\t", $region,
map( $d->{ status }{ $_ }{ count }, @items ),
$d->{ total_count },
), "\n";
}

__END__


Output:

region open pws wip hold re-open pwu openesc total_count
north 3 0 0 7 0 0 0 10
south 3 3 0 1 0 0 0 7

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: help to print a hash with hash

am 14.07.2011 18:46:29 von Mohan L

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

#!/usr/bin/perl

> use strict;
> use warnings;
>
> my %data = (
>
> south => {
> status => {
> open => { count => 3 },
> pws => { count => 3 },
> wip => { count => 0 },
> hold => { count => 1 },
> 're-open' => { count => 0 },
> pwu => { count => 0 },
> openesc => { count => 0 },
> },
> total_count => 7,
> },
>
> north => {
> status => {
> open => { count => 3 },
> pws => { count => 0 },
> wip => { count => 0 },
> hold => { count => 7 },
> 're-open' => { count => 0 },
> pwu => { count => 0 },
> openesc => { count => 0 },
> },
> total_count => 10,
> },
> );
>
> my @items = qw/ open pws wip hold re-open pwu openesc /;
>
> print join( "\t", "region", @items, "total_count" ), "\n";
>
> for my $region ( sort keys %data ) {
> my $d = $data{ $region };
> print join( "\t", $region,
> map( $d->{ status }{ $_ }{ count }, @items ),
> $d->{ total_count },
> ), "\n";
> }
>
> __END__
>
>
> Output:
>
>
>
Hi Ruud,

I have a table in mysql database. I want to insert the values as row.

north 3 0 0 7 0 0 0 10
south 3 3 0 1 0 0 0 7

I want to insert the above (%data) hash values in mysql data base. For
example, the below code works for me. But I don't know how to make the
(%data) hash to (@dbdata) format.

@dbdata=(
["north" ,'3', '0' ,'0', '7' ,'0' ,'0',' 0',' 10'],
["south", '3',' 3' ,'0' ,'1', '0',' 0' ,'0', '7' ],
)
my $connect = DBI->connect($dsn, $username, $password);
my $query = "INSERT INTO by_region (region,open,pws,wip
,hold,reopen,pwu,openesc,total) VALUES (?,?,?,?,?,?,?,?,?)";

my $query_handle = $connect->prepare($query);
for my $datum (@dbdata)
{
$query_handle->execute(@$datum);
}

How to make the above (%data) hash into below reference :

@dbdata=(
["north" ,'3', '0' ,'0', '7' ,'0' ,'0',' 0',' 10'],
["south", '3',' 3' ,'0' ,'1', '0',' 0' ,'0', '7' ],
)

It will solve my problem. I hope this time I am clear.

Thanks & Rg
Mohan L

--0016363b8df048408704a80a460a--

Re: help to print a hash with hash

am 20.07.2011 16:41:10 von Mohan L

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

How to insert below data structure in data base?

#!/usr/bin/perl
>
>> use strict;
>> use warnings;
>>
>> my %data = (
>>
>> south => {
>> status => {
>> open => { count => 3 },
>> pws => { count => 3 },
>> wip => { count => 0 },
>> hold => { count => 1 },
>> 're-open' => { count => 0 },
>> pwu => { count => 0 },
>> openesc => { count => 0 },
>> },
>> total_count => 7,
>> },
>>
>> north => {
>> status => {
>> open => { count => 3 },
>> pws => { count => 0 },
>> wip => { count => 0 },
>> hold => { count => 7 },
>> 're-open' => { count => 0 },
>> pwu => { count => 0 },
>> openesc => { count => 0 },
>> },
>> total_count => 10,
>> },
>> );
>>
>

I am using below code , but I think some thing going wrong here.

my @dbdata=();
for my $region ( sort keys %region_data )
{
my @mydata;
my $d = $region_data{ $region };
@mydata= join( ",", $region,map( $d->{ status }{ $_ }{ count },
@statuses_string ),$d->{ total_count },);
push(@dbdata,@mydata);
}

my $connect = DBI->connect($dsn, $username, $password);
my $query = "INSERT INTO location_wise(region,open,pws,wip
,hold,reopen,pwu,openesc,total) VALUES (?,?,?,?,?,?,?,?,?)";
my $query_handle = $connect->prepare($query);

for my $datum (@dbdata) {
$query_handle->execute(@$datum);
}

DBD::mysql::st execute failed: Column 'open' cannot be null at
../demo.plline 104, line 30.
DBD::mysql::st execute failed: Column 'open' cannot be null at
../demo.plline 104, line 30.
DBD::mysql::st execute failed: Column 'open' cannot be null at
../demo.plline 104, line 30.
DBD::mysql::st execute failed: Column 'open' cannot be null at
../demo.plline 104, line 30.

I am hanging on this issue. Any help will be really appreciated.


> @dbdata=(
> ["north" ,'3', '0' ,'0', '7' ,'0' ,'0',' 0',' 10'],
> ["south", '3',' 3' ,'0' ,'1', '0',' 0' ,'0', '7' ],
> )
> my $connect = DBI->connect($dsn, $username, $password);
> my $query = "INSERT INTO by_region (region,open,pws,wip
> ,hold,reopen,pwu,openesc,total) VALUES (?,?,?,?,?,?,?,?,?)";
>
> my $query_handle = $connect->prepare($query);
> for my $datum (@dbdata)
> {
> $query_handle->execute(@$datum);
> }
>
>

--0016e6506ce02bd64104a881391f--

Re: help to print a hash with hash

am 20.07.2011 17:53:16 von Shawn Wilson

On Wed, Jul 20, 2011 at 10:41, Mohan L wrote:

>
> DBD::mysql::st execute failed: Column 'open' cannot be null at
> ./demo.plline 104, line 30.
> DBD::mysql::st execute failed: Column 'open' cannot be null at
> ./demo.plline 104, line 30.
> DBD::mysql::st execute failed: Column 'open' cannot be null at
> ./demo.plline 104, line 30.
> DBD::mysql::st execute failed: Column 'open' cannot be null at
> ./demo.plline 104, line 30.
>
> I am hanging on this issue. =A0Any help will be really appreciated.
>

not sure what your table looks like. however, if you want to be able
to not have any data in a column, you need to allow the column to be
null. you should also do something like:
$data =3D $column //=3D "";

also, since your data is already a hash, you could just use dbic and
define a resultset to allow your data to be imported instead of
refactoring it.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: help to print a hash with hash

am 20.07.2011 18:04:18 von Mohan L

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

On Wed, Jul 20, 2011 at 9:23 PM, shawn wilson wrote:

> On Wed, Jul 20, 2011 at 10:41, Mohan L wrote:
>
> >
> > DBD::mysql::st execute failed: Column 'open' cannot be null at
> > ./demo.plline 104, line 30.
> > DBD::mysql::st execute failed: Column 'open' cannot be null at
> > ./demo.plline 104, line 30.
> > DBD::mysql::st execute failed: Column 'open' cannot be null at
> > ./demo.plline 104, line 30.
> > DBD::mysql::st execute failed: Column 'open' cannot be null at
> > ./demo.plline 104, line 30.
> >
> > I am hanging on this issue. Any help will be really appreciated.
> >
>
> not sure what your table looks like. however, if you want to be able
> to not have any data in a column, you need to allow the column to be
> null. you should also do something like:
> $data = $column //= "";
>

for my $datum (@dbdata)
{
print "$datum\n";
}

Output :

east,0,0,2,4,0,0,0,6
north,0,0,7,3,0,0,0,10
south,3,0,1,3,0,0,0,7
west,7,0,0,0,0,0,0,7

But the below code does not work :

for my $datum (@dbdata) {
$query_handle->execute(@$
datum);
}

>
> also, since your data is already a hash, you could just use dbic and
> define a resultset to allow your data to be imported instead of
> refactoring it.
>

How to do that using dbic? any example will help.

Thanks & Rg
Mohan L

--20cf307f3708852e4504a88262b0--

Re: help to print a hash with hash

am 20.07.2011 18:22:47 von Jim Gibson

On 7/20/11 Wed Jul 20, 2011 9:04 AM, "Mohan L"
scribbled:

> On Wed, Jul 20, 2011 at 9:23 PM, shawn wilson wrote:
>
>> On Wed, Jul 20, 2011 at 10:41, Mohan L wrote:
>>
>>>
>>> DBD::mysql::st execute failed: Column 'open' cannot be null at
>>> ./demo.plline 104, line 30.
>>> DBD::mysql::st execute failed: Column 'open' cannot be null at
>>> ./demo.plline 104, line 30.
>>> DBD::mysql::st execute failed: Column 'open' cannot be null at
>>> ./demo.plline 104, line 30.
>>> DBD::mysql::st execute failed: Column 'open' cannot be null at
>>> ./demo.plline 104, line 30.
>>>
>>> I am hanging on this issue. Any help will be really appreciated.
>>>
>>
>> not sure what your table looks like. however, if you want to be able
>> to not have any data in a column, you need to allow the column to be
>> null. you should also do something like:
>> $data = $column //= "";
>>
>
> for my $datum (@dbdata)
> {
> print "$datum\n";
> }
>
> Output :
>
> east,0,0,2,4,0,0,0,6
> north,0,0,7,3,0,0,0,10
> south,3,0,1,3,0,0,0,7
> west,7,0,0,0,0,0,0,7

If that is really the output from the print statement shown above, then you
don't have a rows of column data in your @dbdata array elements, you have an
array of scalar string values that are the concatenated values of the
desired column values joined with commas.

Use the Data::Dumper module to see what is really contained within your data
array:

use Data::Dumper;
....
print Dumper(\@dbdata);


>
> But the below code does not work :
>
> for my $datum (@dbdata) {
> $query_handle->execute(@$
> datum);
> }

That assumes that the elements of @dbdata are references to arrays. Printing
the array using the Data::Dumper module will tell you if that is so.

If the elements are really 'east,0,0,2,4,0,0,0,6' and the like, then you are
probably trying to store that string into the first column and nothing into
the subsequent columns. That might explain why you are getting "column
cannot be null" error messages.

You could split the scalar into columns:

my @row = split(/,/,$dbdata);

but you should probably go back and figure out why @dbdata contains the data
that it does.

If you want more help, please post a short, complete program that
demonstrates the problem you are having, and one that other people can
execute on their own systems.

Thanks.



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: help to print a hash with hash

am 20.07.2011 18:45:09 von Mohan L

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

> > east,0,0,2,4,0,0,0,6
> > north,0,0,7,3,0,0,0,10
> > south,3,0,1,3,0,0,0,7
> > west,7,0,0,0,0,0,0,7
>
> If that is really the output from the print statement shown above, then you
> don't have a rows of column data in your @dbdata array elements, you have
> an
> array of scalar string values that are the concatenated values of the
> desired column values joined with commas.
>

Thanks . Your are correct .

>
> Use the Data::Dumper module to see what is really contained within your
> data
> array:
>
> use Data::Dumper;
> ...
> print Dumper(\@dbdata);
>

-----Output ---
$VAR1 = [
'east,0,0,2,4,0,0,0,6',
'north,0,0,7,3,0,0,0,10',
'south,3,0,1,3,0,0,0,7',
'west,7,0,0,0,0,0,0,7'
];



That assumes that the elements of @dbdata are references to arrays.
> Printing
> the array using the Data::Dumper module will tell you if that is so.
>
> If the elements are really 'east,0,0,2,4,0,0,0,6' and the like, then you
> are
> probably trying to store that string into the first column and nothing into
> the subsequent columns. That might explain why you are getting "column
> cannot be null" error messages.
>
> You could split the scalar into columns:
>
> my @row = split(/,/,$dbdata);
>
> but you should probably go back and figure out why @dbdata contains the
> data
> that it does.
>
> If you want more help, please post a short, complete program that
> demonstrates the problem you are having, and one that other people can
> execute on their own systems.
>
> Thanks.
>
>
I have structure below which I want to insert into MySQL Data base.

my %data = (

south => {
status => {
open => { count => 3 },
pws => { count => 3 },
wip => { count => 0 },
hold => { count => 1 },
're-open' => { count => 0 },
pwu => { count => 0 },
openesc => { count => 0 },
},
total_count => 7,
},

north => {
status => {
open => { count => 3 },
pws => { count => 0 },
wip => { count => 0 },
hold => { count => 7 },
're-open' => { count => 0 },
pwu => { count => 0 },
openesc => { count => 0 },
},
total_count => 10,
},
);

I am using below code to insert database :

my @dbdata=();
for my $region ( sort keys %region_data )
{
my @mydata;
my $d = $region_data{ $region };
@mydata= join( ",", $region,map( $d->{ status }{ $_ }{ count },
@statuses_string ),$d->{ total_count },);
push(@dbdata,@mydata);

}

my $connect = DBI->connect($dsn, $username, $password);
my $query = "INSERT INTO location_wise(region,open,pws,
wip ,hold,reopen,pwu,openesc,total) VALUES (?,?,?,?,?,?,?,?,?)";

my $query_handle = $connect->prepare($query);
for my $datum (@dbdata) {
$query_handle->execute(@$datum);
}

It shows below error :
DBD::mysql::st execute failed: Column 'open' cannot be null at /demo.plline
104, line 30.

From Jim Gibson :

print Dumper(\@dbdata);
$VAR1 = [
'east,0,0,2,4,0,0,0,6',
'north,0,0,7,3,0,0,0,10',
'south,3,0,1,3,0,0,0,7',
'west,7,0,0,0,0,0,0,7'
];

It trying to store that string into the first column and nothing into the
subsequent columns. That is why it shows "column cannot be null" error
messages. Thanks Jim Gibson!!!.

Jim Gibson, I understand the problem, but I not sure where in the below code
I am making mistake .

my @dbdata=();
for my $region ( sort keys %region_data )
{
my @mydata;
my $d = $region_data{ $region };
@mydata= join( ",", $region,map( $d->{ status }{ $_ }{ count },
@statuses_string ),$d->{ total_count },);
push(@dbdata,@mydata);

}

Thanks & Rg
Mohan L

--bcaec547c74b909da704a882f485--

Increment UID Field

am 20.07.2011 19:24:06 von overkill

Greetings,

I'm trying to increment the UID field of the unix password file from an
csv file. I've tried to insert C style increment and it keeps bomping
out. What would be the logic to increment the 5009 to increment by
one? Thanks for any help.

-Overkill

#!/usr/bin/perl

#use strict;
#use warnings;

while () {
chomp;
($first, $last, $username) = split(",");
print "$username:x:5009:4001:$first
$last:/home/$username:/bin/false\n";
}
exit;

__DATA__
"Bob","Ahrary","bahrary"
"Jill","Anderson","janderson"


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: help to print a hash with hash

am 20.07.2011 19:36:33 von Brandon McCaig

Hello:

On Wed, Jul 20, 2011 at 12:45 PM, Mohan L wrote:
> It trying to store that string into the first column and nothing into
> the subsequent columns. That is why it  shows "column cannot be null=
"
> error messages. Thanks Jim Gibson!!!.
>
> Jim Gibson, I understand the problem, but I not sure where in the below
> code I am making mistake .

I'm not sure what you're trying to do there. :P The 'join' seems
to be the root of the problem, but I'm not even going to attempt
to figure it out.

You need to turn a hash into an array (reference). Simplest way
is perhaps a hash slice.

use strict;
use warnings;

use Data::Dumper;

my %data =3D (
south =3D> {
status =3D> {
open =3D> { count =3D> 3 },
pws =3D> { count =3D> 3 },
wip =3D> { count =3D> 0 },
hold =3D> { count =3D> 1 },
're-open' =3D> { count =3D> 0 },
pwu =3D> { count =3D> 0 },
openesc =3D> { count =3D> 0 },
},
total_count =3D> 7,
},

north =3D> {
status =3D> {
open =3D> { count =3D> 3 },
pws =3D> { count =3D> 0 },
wip =3D> { count =3D> 0 },
hold =3D> { count =3D> 7 },
're-open' =3D> { count =3D> 0 },
pwu =3D> { count =3D> 0 },
openesc =3D> { count =3D> 0 },
},
total_count =3D> 10,
},
);

# This will be the data to execute the query with.
my @rows;

for my $region (keys %data)
{
my $status =3D $data{$region}->{status};

# Slice of hash referenced by $status.
my @statuses =3D @{$status}{qw(
open pws wip hold re-open pwu openesc)};

# Map each status hash to the count inside.
my @counts =3D map { $_->{count} } @statuses;

# Create an array reference of the row, with the region name,
# counts, and total.
my $row =3D [$region, @counts, $data{$region}->{total_count}];

push @rows, $row;
}

print Data::Dumper->Dump([\@rows], ['rows']);

__END__

Output:

$rows =3D [
[
'south',
3,
3,
0,
1,
0,
0,
0,
7
],
[
'north',
3,
0,
0,
7,
0,
0,
0,
10
]
];


HTH.


--=20
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software rg>

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Increment UID Field

am 20.07.2011 19:42:03 von Rob Coops

--90e6ba308d763e0f2404a883c1bd
Content-Type: text/plain; charset=UTF-8

On Wed, Jul 20, 2011 at 7:24 PM, Overkill wrote:

> Greetings,
>
> I'm trying to increment the UID field of the unix password file from an csv
> file. I've tried to insert C style increment and it keeps bomping out.
> What would be the logic to increment the 5009 to increment by one? Thanks
> for any help.
>
> -Overkill
>
> #!/usr/bin/perl
>
> #use strict;
> #use warnings;
>
> while () {
> chomp;
> ($first, $last, $username) = split(",");
> print "$username:x:5009:4001:$first $last:/home/$username:/bin/**
> false\n";
> }
> exit;
>
> __DATA__
> "Bob","Ahrary","bahrary"
> "Jill","Anderson","janderson"
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
> #!/usr/bin/perl

use strict;
use warnings;

my $userid = 5009;
while () {
chomp;
($first, $last, $username) = split(",");
print "$username:x:$userid:4001:$first $last:/home/$username:/bin/**
false\n";
$userid++;
}
exit;

__DATA__
"Bob","Ahrary","bahrary"
"Jill","Anderson","janderson"

That should do the trick... you could go for a: print "$username:x:" .
$userid++ . ":4001:$first $last:/home/$username:/bin/**false\n"; as well if
you like to keep it shorter. or change $userid++; to $userid +1; or even
$userid = $userid +1; depending on your personal preference. The result is
adding 1 to the $userid variable which is what you are looking to do :-)

Regards,

Rob

--90e6ba308d763e0f2404a883c1bd--

Re: Increment UID Field

am 20.07.2011 20:11:09 von overkill

Rob, great ideas and worked like a charm. Thanks again,
Overkill.

On 07/20/2011 01:42 PM, Rob Coops wrote:
> On Wed, Jul 20, 2011 at 7:24 PM, Overkill wrote:
>
>> Greetings,
>>
>> I'm trying to increment the UID field of the unix password file from an csv
>> file. I've tried to insert C style increment and it keeps bomping out.
>> What would be the logic to increment the 5009 to increment by one? Thanks
>> for any help.
>>
>> -Overkill
>>
>> #!/usr/bin/perl
>>
>> #use strict;
>> #use warnings;
>>
>> while () {
>> chomp;
>> ($first, $last, $username) = split(",");
>> print "$username:x:5009:4001:$first $last:/home/$username:/bin/**
>> false\n";
>> }
>> exit;
>>
>> __DATA__
>> "Bob","Ahrary","bahrary"
>> "Jill","Anderson","janderson"
>>
>>
>> --
>> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
>> For additional commands, e-mail: beginners-help@perl.org
>> http://learn.perl.org/
>>
>>
>> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $userid = 5009;
> while () {
> chomp;
> ($first, $last, $username) = split(",");
> print "$username:x:$userid:4001:$first $last:/home/$username:/bin/**
> false\n";
> $userid++;
> }
> exit;
>
> __DATA__
> "Bob","Ahrary","bahrary"
> "Jill","Anderson","janderson"
>
> That should do the trick... you could go for a: print "$username:x:" .
> $userid++ . ":4001:$first $last:/home/$username:/bin/**false\n"; as well if
> you like to keep it shorter. or change $userid++; to $userid +1; or even
> $userid = $userid +1; depending on your personal preference. The result is
> adding 1 to the $userid variable which is what you are looking to do :-)
>
> Regards,
>
> Rob
>


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Increment UID Field

am 20.07.2011 21:53:06 von Shlomi Fish

Hi Overkill,

On Wed, 20 Jul 2011 13:24:06 -0400
Overkill wrote:

> Greetings,
>=20
> I'm trying to increment the UID field of the unix password file from an=20
> csv file. I've tried to insert C style increment and it keeps bomping=20
> out. What would be the logic to increment the 5009 to increment by=20
> one? Thanks for any help.
>=20
> -Overkill
>=20

don't parse CSV using regular expressions - use Text::CSV :

http://perl-begin.org/uses/text-parsing/

Regards,

Shlomi Fish

> #!/usr/bin/perl
>=20
> #use strict;
> #use warnings;
>=20
> while () {
> chomp;
> ($first, $last, $username) =3D split(",");
> print "$username:x:5009:4001:$first=20
> $last:/home/$username:/bin/false\n";
> }
> exit;
>=20
> __DATA__
> "Bob","Ahrary","bahrary"
> "Jill","Anderson","janderson"
>=20
>=20



--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
What Makes Software Apps High Quality - http://shlom.in/sw-quality

The prefix â€=9CGod Saidâ€=9D has the extraordinary logical propert=
y of converting any
statement that follows it into a true one.

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/

Re: Increment UID Field

am 20.07.2011 23:08:57 von Rob Coops

--00163630ebe32d8a6c04a886a592
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On Wed, Jul 20, 2011 at 9:53 PM, Shlomi Fish wrote=
:

> Hi Overkill,
>
> On Wed, 20 Jul 2011 13:24:06 -0400
> Overkill wrote:
>
> > Greetings,
> >
> > I'm trying to increment the UID field of the unix password file from an
> > csv file. I've tried to insert C style increment and it keeps bomping
> > out. What would be the logic to increment the 5009 to increment by
> > one? Thanks for any help.
> >
> > -Overkill
> >
>
> don't parse CSV using regular expressions - use Text::CSV :
>
> http://perl-begin.org/uses/text-parsing/
>
> Regards,
>
> Shlomi Fish
>
> > #!/usr/bin/perl
> >
> > #use strict;
> > #use warnings;
> >
> > while () {
> > chomp;
> > ($first, $last, $username) =3D split(",");
> > print "$username:x:5009:4001:$first
> > $last:/home/$username:/bin/false\n";
> > }
> > exit;
> >
> > __DATA__
> > "Bob","Ahrary","bahrary"
> > "Jill","Anderson","janderson"
> >
> >
>
>
>
> --
> ------------------------------------------------------------ -----
> Shlomi Fish http://www.shlomifish.org/
> What Makes Software Apps High Quality - http://shlom.in/sw-quality
>
> The prefix â€=9CGod Saidâ€=9D has the extraordinary logical prope=
rty of converting
> any
> statement that follows it into a true one.
>
> 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/
>
>
>
I'm willing to dispute that. :-)

For the simple reason that as long as you control the input to the CVS file
and can be certain that no UTF8 characters or any other mess sneaks in the
simple split using the /,/ regex will be faster and much simpler then the
Text::CVS module though in any situation where you do not control the input
or in the future might not have control over the input you should definitel=
y
use it.

Based on the current data set however this module is pure overkill...

There is a fine line between a module being useful and being over kill if
you are creating a script that you might want to use in the future or share
with colleagues etc... then you should definitely use modules as usually
they cover all kinds of corner cases that you are unlikely to even think of
let alone cover in your code. But there are situations where that is simply
not needed. A simple example is a script I once wrote for an ISP to deal
with a cable company reorganizing their network, the input though not
controlled by me was bound by a lot of rules and I could without any worrie=
s
assume all kinds of things about my input based on the business rules
governing the naming of nodes bundles and locations in the network. Of
course these situations are not very common but in case you do encounter
such a situation it can be very handy to ignore the modules and go for a
much more limited but much more strict way of working that throws an error
on every corner case that should not happen based on the business rules. (a=
s
long as you are prepared to get called out of bed at 3 at night when an
operator finds informs you that due to a decission made a little change in
the naming conventions and your script is rolling over and playing dead
because it was not informed about this...)

In general I do agree wit the usage of modules since in the end the modules
normally have seen many iterations and usually cover much more situations
the the once you can think of based on your typical working situaiton...
which means you are much more certain your script will be able to survive
even if in a few years form now you company moves to China and wants to use
chineese names rather then latin names; which would most likely break your
current script by the way :-)

Regards,

Rob

--00163630ebe32d8a6c04a886a592--