-2

I am struggling with this part for my college exercise... I need to read string from a file and put them into different variable... Team, kindly review and please reply in your free moment...

Input File: (test_ts.txt)

Test1--12:45
Test2--1:30

Script:

use strict;
use warnings;

my $filename = "test_ts.txt";
my @name = ();
my @hrs=();
my @mins=();

open(my $fh, $filename)
  or die "Could not open file '$filename' $!";

while (my $row = <$fh>) {
  chomp $row;
  push(@name, $row);
  print "$row\n";
}

Output:

Test1--12:45
Test2--1:30

Expected output:

Test1
Test2

*(Array should have the below values
name[0]=Test1
name[1]=Test2
hrs[0]=12
hrs[1]=1
mins[0]=45
mins[1]=30)*

Tried using Split:

while (my $row = <$fh>) {
  chomp $row;
  $row=split('--',$row);
  print $row;
  $row=split(':',$row);
  print $row;
  push(@name, $row);
  print "$row\n";
}

Output which i got after trying split:

211
211
Naga
  • 347
  • 2
  • 16
  • You've not tried to split the string in the code you show; you've simply read and echoed the input. That's good; now you're ready to move onto the next phase of the problem. The `push` operation is going to copy the row you've just read; you're going to need to do some work before you do that push and the pushes to `hrs` and `mins`. – Jonathan Leffler Apr 28 '14 at 04:23

3 Answers3

2

split returns a list; when you use it in a scalar context like $row = split(..., $row); then:

  1. You only get the number of elements of the array assigned.
  2. You destroy your $row in the input.

You need something more like:

while (my $row = <$fh>)
{
    chomp $row;
    my @bits = split /[-:]+/, $row;
    print "@bits\n";
    push(@name, $bits[0]);
    …other pushes…
    print "$row\n";
}

You will need to learn about scalar and array context sooner or later. In the mean time, assign the result of split to an array.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • **1)** `split` returns a *list*, not an array. **2)** In scalar context, `split` returns the *number of fields* the string has been split into, not the value of the first field. **3)** `split` has no effect on the target string `$row`. – Borodin Apr 28 '14 at 09:50
  • @Borodin; agreed on 1 and 2 — later evening brain death. On 3, agreed that split does not damage `$row`, but the assignment of the result of `split` to `$row` _does_ destroy `$row`. – Jonathan Leffler Apr 28 '14 at 13:21
0

Here is the simple method split the row based on "--" and then time on basis of ":". Hope this help you.

use strict;
use warnings;

my $filename = "test_ts.pl";
my @name = ();
my @hrs=();
my @mins=();

open(my $fh, $filename)
or die "Could not open file '$filename' $!";

while (my $row = <$fh>) {
chomp $row;
my ($a,$b) = split("--", $row);
my ($c, $d) = split (":", $b);
push(@name, $a);
push(@hrs, $c);
push(@mins, $d);
}
print "$name[0]\n";
print "$name[1]\n";
print "$hrs[0]\n";

print "$hrs[1]\n";
print "$mins[0]\n";
print "$mins[1]\n";
Anuradha Singh
  • 157
  • 1
  • 1
  • 6
0

It is sometimes simpler to use a global regular expression than split. This short program works by finding all alphanumeric fields in the target string.

use strict;
use warnings;
use autodie;

open my $fh, '<', 'test_ts.txt';

my (@name, @hrs, @mins);

while (<$fh>) {
  my ($name, $hrs, $mins) = /\w+/g;
  push @name, $name;
  push @hrs, $hrs;
  push @mins, $mins;
  print "$name\n";
}

print "\n";

print "Names:   @name\n";
print "Hours:   @hrs\n";
print "Minutes: @mins\n";

output

Test1
Test2

Names:   Test1 Test2
Hours:   12 1
Minutes: 45 30
Borodin
  • 126,100
  • 9
  • 70
  • 144