3 Replies - 5913 Views - Last Post: 15 October 2013 - 01:00 AM

#1 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 159
  • Joined: 08-September 11

Extracting information from an array

Posted 13 October 2013 - 02:54 PM

So I'm trying to copy matched elements from one array to another, so I tried this way, but it doesn't work.
my @all = ("jdoe pt90 102.325.58.20", "jdoe pt92 102.325.58.20", "jdoe pt91 102.325.58.20", "jsmith pt93 102.325.58.20");

my $input = "jdoe";#user input

my @selected = ();#empty array
foreach (@all){#reading all array elements
@selected =  grep (/\b^$input\b/, @all) ;#matching and copying matched elements
}
#printing
foreach my$x (@selected){
print "$x";
}




so any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Extracting information from an array

#2 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Extracting information from an array

Posted 14 October 2013 - 12:57 AM

I can only suspect that the code you posted is not the same as the code you ran. I copied and pasted the posted code directly into a file and it ran perfectly with no modifications, producing the output:
jdoe pt90 102.325.58.20jdoe pt92 102.325.58.20jdoe pt91 102.325.58.20


In what way does it not work for you?

A couple tips, though:

1) If your actual program doesn't already use strict; and use warnings;, I would strongly advise adding them. They're the two best Perl debugging tools around. (Although, in this particular case, neither is relevant since your posted code runs cleanly with both in place. Just mentioning it as general good practice.)

2) The first foreach loop is unnecessary. It causes your program to run the grep once for each item in @all, but grep examines the entire list each time, so you only need to run it once, period.

3) When you print out the results, it might be easier to read if you added a \n after each item, so that they'll appear on separate lines rather than being all run together. Alternately, if you use 5.010; (and are using perl 5.10 or later), you can replace "print" with "say" and get the newlines automatically.

Applying all those tips, and making use of a couple extra Perlisms (postfix flow controls and implicit use of $_), your program transforms into:

#!/usr/bin/env perl    

use strict;
use warnings;
use 5.010;

my @all = ("jdoe pt90 102.325.58.20", "jdoe pt92 102.325.58.20", 
           "jdoe pt91 102.325.58.20", "jsmith pt93 102.325.58.20");
  
my $input = "jdoe"; # user input
  
# matching and copying matched elements
my @selected =  grep (/\b^$input\b/, @all) ;

say for @selected;


Which is exactly equivalent to your posted code, aside from running a little faster (since it only does the grep once) and adding newlines after each item it prints out:
jdoe pt90 102.325.58.20
jdoe pt92 102.325.58.20
jdoe pt91 102.325.58.20


Was This Post Helpful? 0
  • +
  • -

#3 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 159
  • Joined: 08-September 11

Re: Extracting information from an array

Posted 14 October 2013 - 06:04 PM

You are right, it's not the actual code that I use, but it should be the same. I'm getting my array elements from
Last
command and processing to extract all information that are matched.

#!/usr/bin/perl

use warnings;
use strict;

my @all = `last`;

my $input = "username";

my @selected =  grep (/\b^$input\b/, @all) ;

for my $s (@selected){
    print "$s";
}


but I think it's way different, so any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Extracting information from an array

Posted 15 October 2013 - 12:59 AM

Again, copying and pasting your posted code (and changing line 8 to set $input to a valid username instead of the string "username"), it works perfectly when I run it, printing out a list of login sessions for the user whose name is in $input.

So I can only repeat my earlier question: In what way does it not work for you? What output do you get and how does that differ from the output that you want/expect?

As a general debugging step, you might also want to try printing the contents of @all and $input to verify that they contain what you expect them to.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1