1

the strings that I need to regex from is as below:

colour -name red -value 8,0,2 -code 0
colour -name blue -value 9 -code 1
colour -name yellow -value 7,3,2.5 -code 1

The required output is hash of colour name and value

red 8,0,2 blue 9 yellow 7,3,2.5

Piece of code:

#/usr/perl;

my %result = {};
my @word = split ' ', $line ; #$line has each of the line of data that is read from text
$result{$word[2]} = $word[4];

But this is not giving the required output for the values where there are commas. TIA.

required output

ITEMS

red 8,0,2 blue 9 yellow 7,3,2.5

obtained output

ITEMS

red 8 0 2 blue 9 yellow 7 3 2.5

7
  • Just print Dumper of %result to see the content of result hash. use Data::Dumper; #your code; print Dumper(\%result); Commented Jan 8, 2021 at 5:55
  • But when I am writing it into a CSVFILE, the space is taking the output to next cell. I want the whole string red 8,0,2 blue 9 yellow 7,3,2.5 in one single cell of csv. Can you please help me with the write to csv of this output? header should ITEMS and below it in one cell red 8,0,2 blue 9 yellow 7,3,2.5 Commented Jan 8, 2021 at 6:00
  • required output ITEMS red 8,0,2 blue 9 yellow 7,3,2.5 obtained output ITEMS red 8 0 2 blue 9 yellow 7 3 2.5 Commented Jan 8, 2021 at 6:02
  • Do you mean you wanted CSV file with header ITEMS and first cell with content red 8,0,2 blue 9 yellow 7,3,2.5 ? Commented Jan 8, 2021 at 6:08
  • @vkk05 yes correct. please guide Commented Jan 8, 2021 at 6:10

3 Answers 3

3

Your code isn't giving the results you claim. I've fleshed out your code into something I can run (it would have been helpful for you to do that yourself):

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use Data::Dumper;

my %result;

while (my $line = <DATA>) {
  my @word = split ' ', $line ; #$line has each of the line of data that is read from text
  $result{$word[2]} = $word[4];
}

say Dumper \%result;

__DATA__
colour -name red -value 8,0,2 -code 0
colour -name blue -value 9 -code 1
colour -name yellow -value 7,3,2.5 -code 1

And the output I get is this:

$VAR1 = {
          'blue' => '9',
          'yellow' => '7,3,2.5',
          'red' => '8,0,2'
        };

It looks like this is the hash you want. So if you're getting something different, then you need to explain how my code differs from the code that you omitted from your question.

But I can't see any way that splitting a string on spaces will affect the commas in it.

Sign up to request clarification or add additional context in comments.

Comments

3

See if this works for you.

Since in your input file, 3rd & 5th fields are color name and color values respectively, we are storing those contents into an array.

push(@contents, $words[2]);
push(@contents, $words[4]);

After that we are joining the array contents to scalar variable to fit into in a cell.

my $string = join(' ', @contents);

Below is the script:

use strict; 
use warnings;
use Data::Dumper;
use Text::CSV_XS qw( csv );

my (@words, @contents) = [];

while(<DATA>){
    @words = split / /, $_;
    push(@contents, $words[2]);
    push(@contents, $words[4]);
}

my $string = join(' ', @contents);
print $string;

#Write content to CSV file
my $out_file = "out.csv";
open( my $fh, '>:encoding(UTF-8)', $out_file )
    or die "Could not open file '$out_file'";

my $csv = Text::CSV_XS->new;

#Header
$csv->combine( "ITEMS" );   
print $fh ($csv->string,"\n");

#print cell content
$csv->combine( $string );
print $fh ($csv->string,"\n");
close $fh;

__DATA__
colour -name red -value 8,0,2 -code 0
colour -name blue -value 9 -code 1
colour -name yellow -value 7,3,2.5 -code 1

Comments

1

Please refer to the below logic where we traverse through each sentence line by line and extract 2nd and 4th column values. In turn we build the hash.

use Data::Dumper;

$line = 'colour -name red -value 8,0,2 -code 0
colour -name blue -value 9 -code 1
colour -name yellow -value 7,3,2.5 -code 1';

my %result = {};

for my $sentence (split '\n', $line) {
    my @array = split ' ', $sentence;
    my $color = $array[2];
    my $value = $array[4];
    $result{$color} = $value;
}

print Dumper(\%result);

Result

$VAR1 = {                                                                                                               
          'red' => '8,0,2',                                                                                             
          'HASH(0x128ecb8)' => undef,                                                                                   
          'blue' => '9',                                                                                                
          'yellow' => '7,3,2.5'                                                                                         
        };                                                                                                              
            

Though this is a working code please have it for reference and you may need to make further tweaks. I am not a native Perl programmer and kindly bare with the solution :)

1 Comment

I'm pretty sure the OP doesn't want that 'HASH(0x128ecb8)' => undef :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.