CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Regular Expression With Perl

 



john_oracle
New User

Jan 16, 2016, 2:16 AM

Post #1 of 3 (1789 views)
Regular Expression With Perl Can't Post

Hi,
I have a file in which i need to add, modify, delete some values.

For example :-
john.txt has the following contents

[groups]
group1 = user1 user2 user3
group2 = user4 user5
group3 = user1 user2 user3

Now i have the following scenarios.
1. To Add new values in group2 alone
2. To Remove one value in group3 alone
3. To Add a new group and values for that group.

Please let me know how can i do in per regular expressions or with anyother which will be most helpful i perl.


BillKSmith
Veteran

Jan 16, 2016, 4:45 AM

Post #2 of 3 (1776 views)
Re: [john_oracle] Regular Expression With Perl [In reply to] Can't Post

I cannot help you without more information.

How do you get the data that specifies the "scenarios"?

What have you tried? How did it go wrong?

Must the groups be in the 'right' order? Same question for users within in group.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Jan 16, 2016, 4:52 AM

Post #3 of 3 (1776 views)
Re: [john_oracle] Regular Expression With Perl [In reply to] Can't Post

Hmm, you could do it with regular expressions, but this is probably not the most practical way.

I would read the file and load the data into an appropriate data structure, probably a hash of arrays (HoA), where the keys would be the group names and the values arrays of users. Then do all the changes that need to be done on this HoA, and, once the changes are completed, write back the data to the file.

A quick example at the command line, just to give you an idea on how to do it and of the resulting data structure:

Code
$ echo 'group1 = user1 user2 user3 
> group2 = user4 user5
> group3 = user1 user2 user3 ' | perl -MData::Dumper -ne 'chomp; my ($name, $users) = split / = /;
> $user_hash{$name} = [split / /, $users];
> END{print Dumper \%user_hash};
> '
$VAR1 = {
'group2' => [
'user4',
'user5'
],
'group1' => [
'user1',
'user2',
'user3'
],
'group3' => [
'user1',
'user2',
'user3'
]
};


If you need more help, plus show the code you've been using and explain where it does not fit your needs.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives