Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read list into variables 2

Status
Not open for further replies.

vodkadrinker

Technical User
May 16, 2002
163
0
0
GB
I use perl but still tend to switch to ksh as I know it better.

I would like to read a list into variables, I see I can do a hashed array but I only see examples of a key plus a value.

Is there a simple way to read a list in say like "who" output into variables that I can use?

user1 pts/0 28 Feb 12:52 (10.10.10.1)
user2 pts/1 03 Mar 00:02 (10.10.10.2)
user3 pts/2 10 Mar 22:45 (10.10.10.3)
user4 pts/3 16 Mar 06:56 (10.10.10.4)
user5 pts/4 24 Mar 23:52 (10.10.10.5)

Many thanks in advanced VD

 
Not really sure what you are trying to do. If you need to tie a user to values, then hash is the way to go.

For example:
key = user1 and value = "pts/0 28 Feb 12:52 (10.10.10.1)" or value = subset of data post a simple parsing of the fields.
 
I was trying to be able to reference each sub value in the list. So in my example I would have user tty day month time and ip.

In ksh I could do:-

who|while read user tty day month time ip

Then recall each variable.

Hope this explains what I am trying to do, I am trying to use Perl more often but keep finding my slipping back to what I know.

Thanks VD
 
In case you have no multiple instances of user in your data, you would do something like this:
Code:
while(<F>){
  @fields=split;
  $users{$fields[0]}=@fields[1..5];
}
$user2_month=$users{'user2'}[2];

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
You need to set the hash value to an array reference
Perl:
use strict;
use warnings;
use Data::Dumper;

my %users;

while (<DATA>) {
  chomp;
  my ($user, @flds) = split(/\s{2,}/);
  $users{$user} = \@flds;
}

print Dumper(%users);

__DATA__
user1     pts/0       28 Feb 12:52     (10.10.10.1)
user2     pts/1       03 Mar 00:02     (10.10.10.2)
user3     pts/2       10 Mar 22:45     (10.10.10.3)
user4     pts/3       16 Mar 06:56     (10.10.10.4)
user5     pts/4       24 Mar 23:52     (10.10.10.5)
This gives you a data stucture that looks like this:
Code:
$VAR1 = 'user5';
$VAR2 = [
          'pts/4',
          '24 Mar 23:52',
          '(10.10.10.5)'
        ];
$VAR3 = 'user1';
$VAR4 = [
          'pts/0',
          '28 Feb 12:52',
          '(10.10.10.1)'
        ];
$VAR5 = 'user4';
$VAR6 = [
          'pts/3',
          '16 Mar 06:56',
          '(10.10.10.4)'
        ];
$VAR7 = 'user3';
$VAR8 = [
          'pts/2',
          '10 Mar 22:45',
          '(10.10.10.3)'
        ];
$VAR9 = 'user2';
$VAR10 = [
           'pts/1',
           '03 Mar 00:02',
           '(10.10.10.2)'
         ];
Note: I've had to split on 'two or more spaces' to allow for the gaps in the date column; not all data is this easy to parse. I've also used the __DATA__ feature to supply the data, but you would be better off using
Perl:
my @data = qx{'who'};
chomp @data;

foreach (@data) {
  my ($user, @flds) = split(/\s{2,}/);
  $users{$user} = \@flds;
}
to obtain the output of real commands on your system...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Aargh! Franco, you beat me to it by 9 minutes. Perhaps I should be less verbose next time... [smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top