Rahul Sharma
Rahul Sharma

Reputation: 190

how to use split? (using perl)

I have a problem with my perl script . I would like to explain first how the script works so that you get an idea of how or what you can fix. the script is used to get data from an other database to an new one . for the example i have taken al the data into plain text en then i need to only take the names for the new database. This proces i can't change.

below you will find the script i have taken the query var for the database out because otherwise you wont see how the data is coming out .

$match = "Name           | Primary Type | Sub Type    | Folder
---------------+--------------+-------------+----------------------
pc108220       | Device       | Workstation | /Devices/Workstations
PC114425       | Device       | Workstation | /Devices/Workstations
pc111206       | Device       | Workstation | /Devices/Workstations
pc111242       | Device       | Workstation | /Devices/Workstations
pc108947       | Device       | Workstation | /Devices/Workstations
pc108420       | Device       | Workstation | /Devices/Workstations
pc109364       | Device       | Workstation | /Devices/Workstations
pc106697       | Device       | Workstation | /Devices/Workstations
pc108844       | Device       | Workstation | /Devices/Workstations
PC106289VM_V32 | Device       | Workstation | /Devices/Workstations
pc108887       | Device       | Workstation | /Devices/Workstations
PC116786       | Device       | Workstation | /Devices/Workstations
pc106662-vme   | Device       | Workstation | /Devices/Workstations
pc108431       | Device       | Workstation | /Devices/Workstations
pc108873       | Device       | Workstation | /Devices/Workstations
pc105664       | Device       | Workstation | /Devices/Workstations
pc108872       | Device       | Workstation | /Devices/Workstations
pc109241       | Device       | Workstation | /Devices/Workstations
PC106662_VMA   | Device       | Workstation | /Devices/Workstations
pc109377       | Device       | Workstation | /Devices/Workstations
pc105372       | Device       | Workstation | /Devices/Workstations
pc108714       | Device       | Workstation | /Devices/Workstations
pc111244       | Device       | Workstation | /Devices/Workstations
pc115097       | Device       | Workstation | /Devices/Workstations
pc109379       | Device       | Workstation | /Devices/Workstations
pc107706-vm7   | Device       | Workstation | /Devices/Workstations
pc112170       | Device       | Workstation | /Devices/Workstations
pc109553       | Device       | Workstation | /Devices/Workstations
pc104597       | Device       | Workstation | /Devices/Workstations
pc106657       | Device       | Workstation | /Devices/Workstations
pc112183       | Device       | Workstation | /Devices/Workstations
pc109261       | Device       | Workstation | /Devices/Workstations
";

  if ($match =~ /pc[0-9][0-9][0-9][0-9][0-9][0-9]/) {
     @fields = split("Name           | Primary Type | Sub Type    | Folder| Device       | Workstation | /Devices/Workstations|", $match);
  $newline = join("", @fields);

  print $newline;
  }

else{
print 'no data found';
}

Upvotes: 0

Views: 97

Answers (2)

tripleee
tripleee

Reputation: 189936

The first argument to split is a regular expression which should match something in the input. The input is split into fields around those matches.

for my $line (split(/\n/, $match) {
  @fields = split(/\|/, $line);
}

would obtain the text between the | separators (you need to backslash it because a bare | is a regex metacharacter) inside the loop. You can trim the spaces around the values by other means.

  @fields = grep { s/^\s+|\s+$//g } split (/\|/, $match);

Upvotes: 3

ssr1012
ssr1012

Reputation: 2589

local $/; $_ = <DATA>; $match=$_;
my @splitline = split/\n/, $match;
foreach my $Sngline(@splitline)
{
    print "Name: $&\n", if($Sngline=~m/^pc([^\s]*)\s/g)
}

Upvotes: 3

Related Questions